Scripting 6.5. Calling system commands

Running a command-line-like command

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 writeInfo command.

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 folder$ (and you are VERY SURE that this name contains NO SPACES OR SPECIAL CHARACTERS), you could write

runSystem: "del ", folder$, "\*.wav" ; DANGEROUS

on Windows, but

runSystem: "rm ", folder$, "/*.wav" ; DANGEROUS

on Macintosh and Linux.

The script will stop running if a system command returns an error. For instance,

runSystem: "rm ", folder$, "/*.wav" ; DANGEROUS

will stop the script if there are no WAV files in the folder, with a message like “No such file or directory”.

In order to prevent this, you can tell Praat to ignore the return value of runSystem}.

Thus, to make sure that the folder contains no WAV files, you would write

runSystem_nocheck: "rm ", folder$, "/*.wav" ; DANGEROUS

Typically, however, the use of runSystem or runSystem_nocheck is DANGEROUS: if the string folder$ 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 runSystem or runSystem_nocheck.

A safer way to call other programs

The above is dangerous because runSystem has no concept of what an argument to a program is. For instance, what if folder$ above is somehow “-rf .*; ls”? Will that remove my whole folder structure? A much safer way is to use runSubprocess, in which you supply the arguments directly:

runSubprocess: "/usr/local/sbin/theOtherApp ", folder$, "*.wav"

This will hand to theOtherApp: the contents of the string folder$ (without fear of executing any commands that are inside the foldername), and the string *.wav. What is done with the asterisk (“*”) depends on your platform: on macOS and Linux, the string is handed verbatim as “*.wav” to theOtherApp, 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.

In general, then, runSubprocess is quite predictable on macOS and Linux, but less so on Windows.

Getting the values of system variables

environment$ (symbol-string)
returns the value of an environment variable, e.g.
homeFolder$ = environment$ ("HOME")

Getting system duration

stopwatch
returns the time that has elapsed since the previous stopwatch.

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):

stopwatch
for i to 100000
a = 1.23456789e123
endfor
time = stopwatch
writeInfoLine: a, " ", fixed$ (time, 3)
=>
1.23456789e+123 0.028

How many nanoseconds is that per assignment?

writeInfoLine: round (time / 100000 * 1e9)
=>
283

Links to this page


© Paul Boersma 2020,2023,2025