| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 6.5. Calling system commands</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.5. Calling system commands |
| 13 | </b></font></table></table> |
| 14 | <h2>Running a command-line-like command</h2> |
| 15 | <p>From a Praat script you can call system commands. These are the same commands that you would normally type into a terminal window or into the Windows command line prompt. The syntax is the same as that of the <a href="_writeInfo_.html"><b><code><font size=+1>writeInfo</font></code></b></a> command.</p> |
| 16 | <p>Most system commands are different on different platforms. For instance, to throw away all WAV files in the folder whose path (relative to the script’s folder) is in the variable <code><font size=+1>folder$</font></code> (and you are VERY SURE that this name contains NO SPACES OR SPECIAL CHARACTERS), you could write</p> |
| 17 | <code> <b><a href="_runSystem_.html">runSystem</a></b>: "del ", folder$, "\*.wav" ; DANGEROUS<br></code> |
| 18 | <p>on Windows, but</p> |
| 19 | <code> <b><a href="_runSystem_.html">runSystem</a></b>: "rm ", folder$, "/*.wav" ; DANGEROUS<br></code> |
| 20 | <p>on Macintosh and Linux.</p> |
| 21 | <p>The script will stop running if a system command returns an error. For instance,</p> |
| 22 | <code> <b><a href="_runSystem_.html">runSystem</a></b>: "rm ", folder$, "/*.wav" ; DANGEROUS<br></code> |
| 23 | <p>will stop the script if there are no WAV files in the folder, with a message like “No such file or directory”.</p> |
| 24 | <p>In order to prevent this, you can tell Praat to ignore the return value of runSystem}.</p> |
| 25 | <p>Thus, to make sure that the folder contains no WAV files, you would write</p> |
| 26 | <code> <b>runSystem_nocheck</b>: "rm ", folder$, "/*.wav" ; DANGEROUS<br></code> |
| 27 | <p>Typically, however, the use of <a href="_runSystem_.html"><b><code><font size=+1>runSystem</font></code></b></a> or <b><code><font size=+1>runSystem_nocheck</font></code></b> is DANGEROUS: if the string <code><font size=+1>folder$</font></code> contains spaces and semicolons and slashes, for instance, you may lose all files on your computer or install malware without noticing it; you really have to have full control over your files and know exactly what you are doing before you use <a href="_runSystem_.html"><b><code><font size=+1>runSystem</font></code></b></a> or <b><code><font size=+1>runSystem_nocheck</font></code></b>.</p> |
| 28 | <h2>A safer way to call other programs</h2> |
| 29 | <p>The above is dangerous because <code><font size=+1>runSystem</font></code> has no concept of what an argument to a program is. For instance, what if <code><font size=+1>folder$</font></code> above is somehow “-rf .*; ls”? Will that remove my whole folder structure? A much safer way is to use <a href="_runSubprocess_.html"><b><code><font size=+1>runSubprocess</font></code></b></a>, in which you supply the arguments directly:</p> |
| 30 | <code> <b><a href="_runSubprocess_.html">runSubprocess</a></b>: "/usr/local/sbin/theOtherApp", folder$, "*.wav"<br></code> |
| 31 | <p>This will hand to <code><font size=+1>theOtherApp</font></code>: the contents of the string <code><font size=+1>folder$</font></code> (without fear of executing any commands that are inside the foldername), and the string <code><font size=+1>*.wav</font></code>. What is done with the asterisk (“*”) depends on your platform: on macOS and Linux, the string is handed verbatim as “*.wav” to <code><font size=+1>theOtherApp</font></code>, whereas on Windows the string is handed verbatim if there are no WAV files (in the folder of the script) but as e.g. “hello.wav goodbye.wav” if the script folder contains those two files.</p> |
| 32 | <p>In general, then, <a href="_runSubprocess_.html"><b><code><font size=+1>runSubprocess</font></code></b></a> is quite predictable on macOS and Linux, but less so on Windows.</p> |
| 33 | <h2>Getting the values of system variables</h2> |
| 34 | <dl> |
| 35 | <dt><b><code><font size=+1>environment$</font></code></b> (<i><code><font size=+1>symbol-string</font></code></i>) |
| 36 | <dd>returns the value of an environment variable, e.g.</dd> |
| 37 | </dl> |
| 38 | <code> homeFolder$ = <b><a href="_environment-S_.html">environment$</a></b> ("HOME")<br></code> |
| 39 | <h2>Getting system duration</h2> |
| 40 | <dl> |
| 41 | <dt><b><code><font size=+1>stopwatch</font></code></b> |
| 42 | <dd>returns the time that has elapsed since the previous <b>stopwatch</b>.</dd> |
| 43 | </dl> |
| 44 | <p>Here is a Praat script that measures how long it takes to do a hundred thousand assignments on your computer (if you are reading this in Praat’s own Help, not on the web):</p> |
| 45 | <code> <b><a href="_stopwatch_.html">stopwatch</a></b><br></code> |
| 46 | <code> for i to 100000<br></code> |
| 47 | <code> a = 1.23456789e123<br></code> |
| 48 | <code> endfor<br></code> |
| 49 | <code> time = <b><a href="_stopwatch_.html">stopwatch</a></b><br></code> |
| 50 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: a, " ", fixed$ (time, 3)<br></code> |
| 51 | <code style="color:red">=></code><br> |
| 52 | <code> 1.23456789e+123 0.029<br></code> |
| 53 | <p>How many nanoseconds is that per assignment?</p> |
| 54 | <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: round (time / 100000 * 1e9)<br></code> |
| 55 | <code style="color:red">=></code><br> |
| 56 | <code> 293<br></code> |
| 57 | <h3>Links to this page</h3> |
| 58 | <ul> |
| 59 | <li><a href="_runSubprocess-S_.html"><code><font size=+1>runSubprocess$</font></code></a> |
| 60 | <li><a href="_runSystem-S_.html"><code><font size=+1>runSystem$</font></code></a> |
| 61 | <li><a href="Interoperability.html">Interoperability</a> |
| 62 | <li><a href="Scripting.html">Scripting</a> |
| 63 | <li><a href="Scripting_6__Communication_outside_the_script.html">Scripting 6. Communication outside the script</a> |
| 64 | </ul> |
| 65 | <hr> |
| 66 | <address> |
| 67 | <p>© Paul Boersma 2020,2023,2025</p> |
| 68 | </address> |
| 69 | </body> |
| 70 | </html> |