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$, you would write

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

on Windows, but

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

on Macintosh and Linux.

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

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

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"

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)
=>
    309

Links to this page


© Paul Boersma 2020,2023