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.
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
(including the quotes) if Praat.exe is indeed in the folder
On the Mac, the executable is hidden inside the
app file, so you open a Terminal window and type something like
On Linux, you type into the Terminal something like
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
"C:\Program Files\Praat.exe" --open data/hello.wav data/hello.TextGrid
at least if your current directory (see the Console's
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
On Windows, when you type
"C:\Program Files\Praat.exe" --open "my script.praat"
Praat will start up, opening the script
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
script or above between
Files. This is because the script languages of the Console or Terminal use spaces for separating commands and arguments.
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
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.
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
writeInfoLine: "She: """, first_text$, """"
appendInfoLine: "He: """, second_text$, """"
synth1 = Create SpeechSynthesizer: "English (Great Britain)", "Female1"
Play text: first_text$
Create Sound as pure tone: "beep", 1, 0.0, beep_duration,
... 44100, 440, 0.2, 0.01, 0.01
synth2 = Create SpeechSynthesizer: "English (America)", "Male1"
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.
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:
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:
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"')
If you specify neither
--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.
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 -
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')
© ppgb, October 31, 2015