Scripting 6.5. Calling system commands

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, howver, 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.

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

How many nanoseconds is that per assignment?

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

Links to this page


© Paul Boersma 2020,2023,2025