Scripting 6.9. Calling from the command line

Previous sections of this tutorial have shown you how to run a Praat script from the Script window. However, you can also call a Praat script from the command line (text console) instead. Information that would normally show up in the Info window, then goes to stdout, and error messages go to stderr. You cannot use commands in your script that create windows, such as View & Edit. Before describing how to achieve this (from section 4 below on), we first describe how the normal Praat, with its usual Objects and Picture (and perhaps Info) window, can be started from the command line.

1. Starting Praat from the command line

Before seeing how a Praat script can be called from the command line, you should first know that just calling Praat from the command line just starts up Praat with its usual GUI (Graphical User Interface), i.e. with its two windows. For instance, on Windows you can start the Command Prompt window (the "Console"), and type

"C:\Program Files\Praat.exe"

(including the quotes) if Praat.exe is indeed in the folder C:\Program Files.

On the Mac, the executable is hidden inside the app file, so you open a Terminal window and type something like

/Applications/Praat.app/Contents/MacOS/Praat

On Linux, you type into the Terminal something like

/usr/bin/praat

2. Calling Praat to open data files

On Windows, you can open Praat with a sound file and a TextGrid file by typing

"C:\Program Files\Praat.exe" --open data\hello.wav data\hello.TextGrid

or

"C:\Program Files\Praat.exe" --open data/hello.wav data/hello.TextGrid

at least if your current directory (see the Console's cd and dir commands) contains the folder data and that folder contains those two files. Praat will start up, and shows the two files as a Sound and a TextGrid object in the list. If Praat was already running when you typed the command, the two files are added as objects to the existing list in Praat.

On the Mac, you do

/Applications/Praat.app/Contents/MacOS/Praat --open data/hello.wav data/hello.TextGrid

and on Linux

/usr/bin/praat --open data/hello.wav data/hello.TextGrid

3. Calling Praat to open a script

On Windows, when you type

"C:\Program Files\Praat.exe" --open "my script.praat"

Praat will start up, opening the script my script.praat in a script window. If Praat was already running when you typed the command, the script window will appear within the already running instantiation of Praat.

On the Mac, you do

/Applications/Praat.app/Contents/MacOS/Praat --open "my script.praat"

and on Linux

/usr/bin/praat --open "my script.praat"

Note that on all three platforms, you have to supply quotes around the file name if that file name contains one or more spaces, as here between my and script or above between Program and Files. This is because the script languages of the Console or Terminal use spaces for separating commands and arguments.

4. Calling Praat to run a script

Now we are ready to discuss how to run Praat without a GUI.

On Windows, when you type

"C:\Program Files\Praat.exe" --run "my script.praat"

Praat will execute the script my script.praat without showing Praat's GUI, i.e. without showing its usual two windows. In fact, any output that would normally go to the Info window, will now go directly to the Console window in which you typed the command. If Praat was already running when you typed the command, its windows will not be affected. In fact, the GUI-instantiation of Praat and the Console-instantiation can run simultaneously without them noticing each other's existence; moreover, multiple Console-instantiations of Praat can run simultaneously, each in their own Console.

On the Mac, you type

/Applications/Praat.app/Contents/MacOS/Praat --run "my script.praat"

and on Linux

/usr/bin/praat --run "my script.praat"

What happens on all platforms is that the Console or Terminal starts up Praat, then Praat executes the script, and then Praat closes itself.

5. Calling Praat to run a script with arguments

Consider the following script:

form Test command line calls
    sentence First_text I love you
    real Beep_duration 0.4
    sentence Second_text Me too
endform

writeInfoLine: "She: """, first_text$, """"
appendInfoLine: "He: """, second_text$, """"

synth1 = Create SpeechSynthesizer: "English", "f1"
Play text: first_text$
Create Sound as pure tone: "beep", 1, 0.0, beep_duration,
... 44100, 440, 0.2, 0.01, 0.01
Play
Remove
synth2 = Create SpeechSynthesizer: "English", "m1"
Play text: second_text$

When you run this script from within Praat, it writes two lines to the Info window and plays first a female voice speaking the first sentence, then a beep, and then a male voice speaking the second sentence. To make this happen from the Windows command line instead, you type

"C:\Program Files\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"

In the Mac terminal, you type

/Applications/Praat.app/Contents/MacOS/Praat --run testCommandLineCalls.praat "I love you" 0.4 "Me too"

and in the Linux terminal, you do

/usr/bin/praat --run testCommandLineCalls.praat "I love you" 0.4 "Me too"

Note that each argument that contains one or more spaces has to be put within quotes, on all three platforms. As with runScript, Praat will not present a form window, but simply run the script with the arguments given on the command line (see Scripting 6.1. Arguments to the script).

What then happens on all three platforms is that a console instantiation of Praat writes the two lines to the Console window and plays the three sounds.

6. Calling Praat from other programs such as Python

You can run the above script from several programming languages, not just from a Console or Terminal. In Python, for instance, you can do it using the same syntax as you would use in the Console or Terminal:

import os
os.system ('"C:\\Program Files\\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"')

Note that you have to double the backslashes!

A disadvantage of the os.system method is that you have to use quotes within quotes. A somewhat cleaner approach is:

import subprocess
subprocess.call(['C:\\Program Files\\Praat.exe', '--run', 'testCommandLineCalls.praat', 'I love you', '0.4', 'Me too'])

This way you specify the arguments directly, with quotes only because they are all strings, but without having to worry about spaces. And perhaps even more importantly, this syntax makes it easy to use variables as arguments, as in:

first_line = 'I love you'
second_line = 'me too'
subprocess.call(['C:\\Program Files\\Praat.exe', '--run', 'testCommandLineCalls.praat', first_line, '0.4', second_line])

Many other programs beside Python have a system-like command, so that you can run a command like

system ('"C:\\Program Files\\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"')

7. What happens if I specify neither --open nor --run?

If you specify neither --open nor --run, Praat's behaviour is not guaranteed. If you type something like

praat testCommandLineCalls.praat "I love you" 0.4 "Me too"

into a Console or Terminal window by hand, Praat will typically run the script. Also, the --run option can probably be left out from the Python call above. However, if you redirect the output of Praat to a file or pipe, you cannot typically leave out the --run option; if you do, Praat may start its GUI and open the file rather than run it.

8. Running Praat interactively from the command line

On the Mac and Linux, you have the possibility of running the program interactively from the command line:

> /usr/bin/praat -

You can then type in any of the fixed and dynamic commands, and commands that handle object selection, such as selectObject. This method also works in pipes:

> echo "Report memory use" | /usr/bin/praat -

9. Calling Praat from a web server

If you call Praat from a web server, you typically do not want to read and write its preferences and buttons files. To achieve this, you use the --no-pref-files command line option before the script name:

system ('/users/apache/praat --run --no-pref-files /user/apache/scripts/computeAnalysis.praat 1234 blibla')

10. All command line options

--open
Interpret the command line arguments as files to be opened in the GUI.
--run
Interpret the command line arguments as a script file name and its arguments.
--no-pref-files
Ignore the preferences file and the buttons file at start-up, and don't write them when quitting (see above).
--no-plugins
Don't activate the plugins at start-up.
--pref-dir=/var/www/praat_plugins
Set the preferences directory to /var/www/praat_plugins (for instance). This can come in handy if you require access to preference files and/or plugins that are not in your home directory.
--version
Print the Praat version.
--help
Print this list of command line options.
-a, --ansi
On Windows: use ISO Latin-1 encoding instead of the Console's native UTF-16 Little Endian encoding. This is not recommended, but might be necessary if you want to use Praat in a pipe or with redirection to a file.

Links to this page


© ppgb, October 31, 2015