| 1 |
<html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 |
<title>Scripting 6.9. Calling from the command line</title> |
| 3 |
<style> |
| 4 |
td { padding-left: 5pt; padding-right: 5pt; } |
| 5 |
th { padding-left: 5pt; padding-right: 5pt; } |
| 6 |
code { white-space: pre-wrap; } |
| 7 |
dd { white-space: pre-wrap; } |
| 8 |
</style> |
| 9 |
</head><body bgcolor="#FFFFFF"> |
| 10 |
|
| 11 |
<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 |
Scripting 6.9. Calling from the command line |
| 13 |
</b></font></table></table> |
| 14 |
<p>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 <i>stdout</i>, and error messages go to <i>stderr</i>. You cannot use commands in your script that create windows, such as <b>View & Edit</b>. 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.</p> |
| 15 |
<h2>1. Starting Praat from the command line</h2> |
| 16 |
<p>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</p> |
| 17 |
<code> "C:\Program Files\Praat.exe"<br></code> |
| 18 |
<p>(including the quotes) if Praat.exe is indeed in the folder <code><font size=+1>C:\Program Files</font></code>.</p> |
| 19 |
<p>On the Mac, the executable is hidden inside the <code><font size=+1>app</font></code> file, so you open a Terminal window and type something like</p> |
| 20 |
<code> /Applications/Praat.app/Contents/MacOS/Praat<br></code> |
| 21 |
<p>On Linux, you type into the Terminal something like</p> |
| 22 |
<code> /usr/bin/praat<br></code> |
| 23 |
<h2>2. Calling Praat to open data files</h2> |
| 24 |
<p>On Windows, you can open Praat with a sound file and a TextGrid file by typing</p> |
| 25 |
<code> "C:\Program Files\Praat.exe" --open data\hello.wav data\hello.TextGrid<br></code> |
| 26 |
<p>or</p> |
| 27 |
<code> "C:\Program Files\Praat.exe" --open data/hello.wav data/hello.TextGrid<br></code> |
| 28 |
<p>at least if your current folder (see the Console’s <code><font size=+1>cd</font></code> and <code><font size=+1>dir</font></code> commands) contains the folder <code><font size=+1>data</font></code> 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.</p> |
| 29 |
<p>On the Mac, you do</p> |
| 30 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --open data/hello.wav data/hello.TextGrid<br></code> |
| 31 |
<p>and on Linux</p> |
| 32 |
<code> /usr/bin/praat --open data/hello.wav data/hello.TextGrid<br></code> |
| 33 |
<p>Again, if Praat was already running when you typed the command, the two files are added as objects to the existing list in Praat.</p> |
| 34 |
<p>Note that if you want to send messages or files to a running Praat, the best way (on all platforms) is to use <code><font size=+1>praat --send</font></code> (see below) or <i>sendpraat</i> (see <a href="Scripting_8__Controlling_Praat_from_another_program.html">Scripting 8. Controlling Praat from another program</a>).</p> |
| 35 |
<p>To always start up a new instance of Praat, use <b><code><font size=+1>--new-open</font></code></b> instead of <code><font size=+1>--open</font></code>.</p> |
| 36 |
<h2>3. Calling Praat to open a script</h2> |
| 37 |
<p>On Windows, when you type</p> |
| 38 |
<code> "C:\Program Files\Praat.exe" --open "my script.praat"<br></code> |
| 39 |
<p>Praat will start up, opening the script <code><font size=+1>my script.praat</font></code> 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.</p> |
| 40 |
<p>On the Mac, you do</p> |
| 41 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --open "my script.praat"<br></code> |
| 42 |
<p>and on Linux</p> |
| 43 |
<code> /usr/bin/praat --open "my script.praat"<br></code> |
| 44 |
<p>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 <code><font size=+1>my</font></code> and <code><font size=+1>script</font></code> or above between <code><font size=+1>Program</font></code> and <code><font size=+1>Files</font></code>. This is because the script languages of the Console or Terminal use spaces for separating commands and arguments.</p> |
| 45 |
<h2>4. Calling Praat to run a script in the background</h2> |
| 46 |
<p>Now we are ready to discuss how to run Praat without a GUI.</p> |
| 47 |
<p>On Windows, when you type</p> |
| 48 |
<code> "C:\Program Files\Praat.exe" --run "my script.praat"<br></code> |
| 49 |
<p>Praat will execute the script <code><font size=+1>my script.praat</font></code> 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.</p> |
| 50 |
<p>On the Mac, you type</p> |
| 51 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --run "my script.praat"<br></code> |
| 52 |
<p>and on Linux</p> |
| 53 |
<code> /usr/bin/praat --run "my script.praat"<br></code> |
| 54 |
<p>What happens on all platforms is that the Console or Terminal starts up Praat, then Praat executes the script, and then Praat closes itself.</p> |
| 55 |
<h2>5. Calling Praat to run a script with arguments</h2> |
| 56 |
<p>Consider the following script:</p> |
| 57 |
<code> form: "Test command line calls"<br></code> |
| 58 |
<code> sentence: "First text", "I love you"<br></code> |
| 59 |
<code> real: "Beep duration", "0.4"<br></code> |
| 60 |
<code> sentence: "Second text", "Me too"<br></code> |
| 61 |
<code> endform<br></code> |
| 62 |
<code> <br></code> |
| 63 |
<code> writeInfoLine: "She: """, first_text$, """"<br></code> |
| 64 |
<code> appendInfoLine: "He: """, second_text$, """"<br></code> |
| 65 |
<code> <br></code> |
| 66 |
<code> synth1 = Create SpeechSynthesizer: "English (Great Britain)", "Female1"<br></code> |
| 67 |
<code> Play text: first_text$<br></code> |
| 68 |
<code> Create Sound as pure tone: "beep", 1, 0.0, beep_duration,<br></code> |
| 69 |
<code> ... 44100, 440, 0.2, 0.01, 0.01<br></code> |
| 70 |
<code> Play<br></code> |
| 71 |
<code> Remove<br></code> |
| 72 |
<code> synth2 = Create SpeechSynthesizer: "English (America)", "Male1"<br></code> |
| 73 |
<code> Play text: second_text$<br></code> |
| 74 |
<p>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</p> |
| 75 |
<code> "C:\Program Files\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 76 |
<p>In the Mac terminal, you type</p> |
| 77 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --run testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 78 |
<p>and in the Linux terminal, you do</p> |
| 79 |
<code> /usr/bin/praat --run testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 80 |
<p>Note that each argument that contains one or more spaces has to be put within quotes, on all three platforms. As with <b>runScript</b>, Praat will not present a form window, but simply run the script with the arguments given on the command line (see <a href="Scripting_6_1__Arguments_to_the_script.html">Scripting 6.1. Arguments to the script</a>). 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.</p> |
| 81 |
<p>The path to the script file as well as to <code><font size=+1>infile</font></code>, <code><font size=+1>outfile</font></code> and <code><font size=+1>folder</font></code> arguments will be taken relative to the current working directory of the terminal window. For instance, the following example from <a href="Scripting_6_1__Arguments_to_the_script.html">Scripting 6.1. Arguments to the script</a> will run the script <code><font size=+1>/Users/miep/research/usefulScripts/playFile.praat</font></code>, which will play the sound file <code><font size=+1>/Users/miep/research/project19/sounds/sound3.wav</font></code>:</p> |
| 82 |
<code> cd /Users/miep/research/project19<br></code> |
| 83 |
<code> /usr/bin/praat --run ../usefulScripts/playFile.praat sounds/sound3.wav<br></code> |
| 84 |
<h2>6. Calling Praat to run a script in the GUI</h2> |
| 85 |
<p>You can send a script to a running Praat. Praat will then execute it:</p> |
| 86 |
<code> "C:\Program Files\Praat.exe" --send testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 87 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --send testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 88 |
<code> /usr/bin/praat --send testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 89 |
<p>This works the same way as <code><font size=+1>--run</font></code>, except that <code><font size=+1>--send</font></code> 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 <code><font size=+1>--new-send</font></code> instead of <code><font size=+1>--send</font></code>.</p> |
| 90 |
<p>If you use</p> |
| 91 |
<code> "C:\Program Files\Praat.exe" --send-or-form testCommandLineCalls.praat<br></code> |
| 92 |
<code> /Applications/Praat.app/Contents/MacOS/Praat --send-or-form testCommandLineCalls.praat<br></code> |
| 93 |
<code> /usr/bin/praat --send-or-form testCommandLineCalls.praat<br></code> |
| 94 |
<p>then Praat will present the above form that asks for the three arguments (if you use <code><font size=+1>--send-or-form</font></code> with a script that has no <code><font size=+1>form</font></code> in it, it will work the same as <code><font size=+1>--send</font></code>). This switch is useful if you want to implement a Praat connection for interfacing with an IDE such as Microsoft® Visual Studio Code™. To always start up a new instance of Praat, use <code><font size=+1>--new-send-or-form</font></code> instead of <code><font size=+1>--send-or-form</font></code>.</p> |
| 95 |
<p>See also <i>sendpraat</i> (see <a href="Scripting_8__Controlling_Praat_from_another_program.html">Scripting 8. Controlling Praat from another program</a>).</p> |
| 96 |
<h2>7. Calling Praat from other programs such as Python</h2> |
| 97 |
<p>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:</p> |
| 98 |
<code> import os<br></code> |
| 99 |
<code> os.system ('"C:\\Program Files\\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"')<br></code> |
| 100 |
<p>Note that you have to double the backslashes!</p> |
| 101 |
<p>A disadvantage of the <code><font size=+1>os.system</font></code> method is that you have to use quotes within quotes. A somewhat cleaner approach is:</p> |
| 102 |
<code> import subprocess<br></code> |
| 103 |
<code> subprocess.call(['C:\\Program Files\\Praat.exe', '--run', 'testCommandLineCalls.praat', 'I love you', '0.4', 'Me too'])<br></code> |
| 104 |
<p>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:</p> |
| 105 |
<code> first_line = 'I love you'<br></code> |
| 106 |
<code> second_line = 'me too'<br></code> |
| 107 |
<code> subprocess.call(['C:\\Program Files\\Praat.exe', '--run', 'testCommandLineCalls.praat', first_line, '0.4', second_line])<br></code> |
| 108 |
<p>Many other programs beside Python have a <code><font size=+1>system</font></code>-like command, so that you can run a command like</p> |
| 109 |
<code> system ('"C:\\Program Files\\Praat.exe" --run testCommandLineCalls.praat "I love you" 0.4 "Me too"')<br></code> |
| 110 |
<h2>8. What happens if I specify neither --open nor --run nor --send?</h2> |
| 111 |
<p>If you specify neither <code><font size=+1>--open</font></code> nor <code><font size=+1>--run</font></code> nor <code><font size=+1>--send</font></code>, Praat's behaviour is not guaranteed. If you type something like</p> |
| 112 |
<code> praat testCommandLineCalls.praat "I love you" 0.4 "Me too"<br></code> |
| 113 |
<p>into a Console or Terminal window by hand, Praat will typically run the script. Also, the <code><font size=+1>--run</font></code> 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 <code><font size=+1>--run</font></code> option; if you do, Praat may start its GUI and <i>open</i> the file rather than run it.</p> |
| 114 |
<p>“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 <code><font size=+1>hello.wav</font></code> or <code><font size=+1>doSomething.praat</font></code>, or drop <code><font size=+1>hello.wav</font></code> or <code><font size=+1>doSomething.praat</font></code> on the Praat icon, you expect Praat to <i>open</i> that sound file or script, not to <i>run</i> it. However, when you double-click or drop <code><font size=+1>doSomething.praat</font></code>, Windows will send Praat a message <i>as if</i> you had typed <code><font size=+1>praat doSomething.praat</font></code> into a Console window. This means that in this case Praat will have to interpret <code><font size=+1>praat doSomething.praat</font></code> as an indication that you want to <i>open</i> that file, not <i>run</i> it. Praat will try to be a bit smart, though: when receiving the message <code><font size=+1>praat doSomething.praat</font></code> from the operating system, Praat will try to figure out whether you meant to <i>run</i> or <i>open</i> the file. If Praat detects that you typed <code><font size=+1>praat doSomething.praat</font></code> into the Console by hand, Praat will <i>run</i> this script; if not, then Praat will <i>open</i> 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 <i>open</i> the script is safer than wrongly assuming that you want to <i>run</i> the script.</p> |
| 115 |
<h2>9. Running Praat interactively from the command line</h2> |
| 116 |
<p>On the Mac and Linux, you have the possibility of running the program interactively from the command line:</p> |
| 117 |
<code> > /usr/bin/praat -<br></code> |
| 118 |
<p>You can then type in any of the fixed and dynamic commands, and commands that handle object selection, such as <b>selectObject</b>. This method also works in pipes:</p> |
| 119 |
<code> > echo "Report memory use" | /usr/bin/praat -<br></code> |
| 120 |
<h2>10. Calling Praat from a web server</h2> |
| 121 |
<p>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 <b><code><font size=+1>--no-pref-files</font></code></b> command line option before the script name:</p> |
| 122 |
<code> system ('/users/apache/praat --run --no-pref-files /user/apache/scripts/computeAnalysis.praat 1234 blibla')<br></code> |
| 123 |
<p>On Windows, you will often want to specify <b><code><font size=+1>--utf8</font></code></b> as well, because otherwise Praat will write its output to BOM-less UTF-16 files, which many programs do not understand.</p> |
| 124 |
<h2>11. All command line switches and options</h2> |
| 125 |
<p>Switches:</p> |
| 126 |
<dl> |
| 127 |
<dt><b>--open</b> |
| 128 |
<dd>Interpret the command line arguments as files to be opened in an existing or new GUI.</dd> |
| 129 |
<dt><b>--new-open</b> |
| 130 |
<dd>Start a new GUI and interpret the command line arguments as files to be opened.</dd> |
| 131 |
<dt><b>--run</b> |
| 132 |
<dd>Interpret the command line arguments as a script file name and its arguments, to run without a GUI.</dd> |
| 133 |
<dt><b>--send</b> |
| 134 |
<dd>Interpret the command line arguments as a script file name and its arguments, to run in an existing or new GUI.</dd> |
| 135 |
<dt><b>--new-send</b> |
| 136 |
<dd>Start a new GUI and interpret the command line arguments as a script file name and its arguments.</dd> |
| 137 |
<dt><b>--version</b> |
| 138 |
<dd>Print the Praat version.</dd> |
| 139 |
<dt><b>--help</b> |
| 140 |
<dd>Print this list of command line options.</dd> |
| 141 |
</dl> |
| 142 |
<p>Options:</p> |
| 143 |
<dl> |
| 144 |
<dt><b>--no-pref-files</b> |
| 145 |
<dd>Ignore the preferences file and the buttons file at start-up, and don't write them when quitting (see above).</dd> |
| 146 |
<dt><b>--no-plugins</b> |
| 147 |
<dd>Don't activate the plugins at start-up.</dd> |
| 148 |
<dt><b>--pref-dir=<b><code><font size=+1>/var/www/praat_plugins</font></code></b></b> |
| 149 |
<dd>Set the preferences folder to <code><font size=+1>/var/www/praat_plugins</font></code> (for instance). This can come in handy if you require access to preference files and/or plugins that are not in your home folder.</dd> |
| 150 |
<dt><b>-8</b>, <b>--utf8</b> |
| 151 |
<dd>Write the output (e.g. of <code><font size=+1>writeInfo</font></code><code><font size=+1></font></code>) 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 <code><font size=+1>chcp 65001</font></code> 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.</dd> |
| 152 |
<dt><b>-a</b>, <b>--ansi</b> |
| 153 |
<dd>Write the output (e.g. of <code><font size=+1>writeInfo</font></code>) 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.</dd> |
| 154 |
<dt><b>-u</b>, <b>--utf16</b> |
| 155 |
<dd>Write the output (e.g. of <code><font size=+1>writeInfo</font></code>) 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.</dd> |
| 156 |
<dt><b>--trace</b> |
| 157 |
<dd>Switch tracing on at start-up.</dd> |
| 158 |
<dt><b>--hide-picture</b> |
| 159 |
<dd>Hide the Picture window at start-up.</dd> |
| 160 |
</dl> |
| 161 |
<h3>Links to this page</h3> |
| 162 |
<ul> |
| 163 |
<li><a href="Interoperability.html">Interoperability</a> |
| 164 |
<li><a href="Scripting.html">Scripting</a> |
| 165 |
<li><a href="Scripting_6__Communication_outside_the_script.html">Scripting 6. Communication outside the script</a> |
| 166 |
<li><a href="Scripting_6_2__Writing_to_the_Info_window.html">Scripting 6.2. Writing to the Info window</a> |
| 167 |
<li><a href="Scripting_8_1__The_sendpraat_subroutine.html">Scripting 8.1. The sendpraat subroutine</a> |
| 168 |
<li><a href="What_was_new_in_6_0_.html">What was new in 6.0?</a> |
| 169 |
</ul> |
| 170 |
<hr> |
| 171 |
<address> |
| 172 |
<p>© ppgb 20220122</p> |
| 173 |
</address> |
| 174 |
</body> |
| 175 |
</html> |