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 folder (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

Again, if Praat was already running when you typed the command, the two files are added as objects to the existing list in Praat.

Note that if you want to send messages or files to a running Praat, the best way (on all platforms) is to use praat --send (see below) or sendpraat (see Scripting 8. Controlling Praat from another program).

To always start up a new instance of Praat, use --new-open instead of --open.

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 in the background

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 (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
Play
Remove
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.

The path to the script file as well as to infile, outfile and folder arguments will be taken relative to the current working directory of the terminal window. For instance, the following example from Scripting 6.1. Arguments to the script will run the script /Users/miep/research/usefulScripts/playFile.praat, which will play the sound file /Users/miep/research/project19/sounds/sound3.wav:

cd /Users/miep/research/project19
/usr/bin/praat --run ../usefulScripts/playFile.praat sounds/sound3.wav

6. Calling Praat to run a script in the GUI

You can send a script to a running Praat. Praat will then execute it:

"C:\Program Files\Praat.exe" --send testCommandLineCalls.praat "I love you" 0.4 "Me too"
/Applications/Praat.app/Contents/MacOS/Praat --send testCommandLineCalls.praat "I love you" 0.4 "Me too"
/usr/bin/praat --send testCommandLineCalls.praat "I love you" 0.4 "Me too"

This works the same way as --run, except that --send runs in Praat’s Graphical User Interface. If Praat is already running, then that instance of Praat will execute your script. If Praat is not running yet, then a new GUI instance of Praat will start up and execute your script. To always start up a new instance of Praat, use --new-send instead of --send.

See also sendpraat (see Scripting 8. Controlling Praat from another program).

7. 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"')

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

If you specify neither --open nor --run nor --send, 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.

“Why this strange behaviour?”, you may ask. The reason is that several platforms (e.g. Windows) use this same mechanismm when you double-click a file, or drop a file on an application icon. For instance, when you double-click hello.wav or doSomething.praat, or drop hello.wav or doSomething.praat on the Praat icon, you expect Praat to open that sound file or script, not to run it. However, when you double-click or drop doSomething.praat, Windows will send Praat a message as if you had typed praat doSomething.praat into a Console window. This means that in this case Praat will have to interpret praat doSomething.praat as an indication that you want to open that file, not run it. Praat will try to be a bit smart, though: when receiving the message praat doSomething.praat from the operating system, Praat will try to figure out whether you meant to run or open the file. If Praat detects that you typed praat doSomething.praat into the Console by hand, Praat will run this script; if not, then Praat will open this script; this includes double-clicking and dropping (appropriately), but may also include some indirect invocations such as through a shell script or redirection or pipe. After all, if Praat is in doubt, wrongly assuming that you want to open the script is safer than wrongly assuming that you want to run the script.

9. 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 -

10. 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')

On Windows, you will often want to specify --utf8 as well, because otherwise Praat will write its output to BOM-less UTF-16 files, which many programs do not understand.

11. All command line switches and options

Switches:

--open
Interpret the command line arguments as files to be opened in an existing or new GUI.
--new-open
Start a new GUI and interpret the command line arguments as files to be opened.
--run
Interpret the command line arguments as a script file name and its arguments, to run without a GUI.
--send
Interpret the command line arguments as a script file name and its arguments, to run in an existing or new GUI.
--new-send
Start a new GUI and interpret the command line arguments as a script file name and its arguments.
--version
Print the Praat version.
--help
Print this list of command line options.

Options:

--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 folder 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 folder.
-8, --utf8
Write the output (e.g. of writeInfo) in UTF-8 encoding. This is the default encoding on MacOS and Linux, but on Windows the default is the Console’s native UTF-16 Little Endian (i.e. the Console understands UTF-16 always, whereas it understands UTF-8 only if you type chcp 65001 first). If you pipe to Windows programs that understand UTF-8 rather than UTF-16, or if you want to redirect the output to a UTF-8 file, use this option.
-a, --ansi
Write the output (e.g. of writeInfo) in ISO-Latin 1 ("ANSI") encoding. This is not recommended, because it potentially loses information (characters above U+00FF will show up as "?"), but it might be necessary if you want to use Praat in a pipe with programs that do understand ANSI but do not understand UTF-8 or UTF-16, or if you want to redirect the output to an ANSI-encoded file.
-u, --utf16
Write the output (e.g. of writeInfo) in UTF-16 Little Endian encoding, without Byte Order Mark. This format is the default on Windows, but you can use it to write the output to a UTF-16LE-encoded file on any platform.
--trace
Switch tracing on at start-up.
--hide-picture
Hide the Picture window at start-up.

Links to this page


© ppgb 20220122