Scripting 2. How to script settings windows

Not all menu commands are as simple as those on the previous page, which act immediately once you choose them from a menu (e.g. Play, Erase all). Most commands in Praat require the user to supply additional information; these are the commands whose title ends in "...".

For instance, when you select a Sound, the command Draw... will appear in the Draw menu, and when you click it, Praat will present you with a settings window, which asks you to supply six pieces of additional information, i.e. six so-called settings (or in programming jargon: arguments):

In this example, all the settings have their standard values: you want to draw the whole time domain of the Sound, you want to have autoscaling vertically, you want to see garnishings around the picture (a box, labelled axes, and numbers), and you want the waveform to be drawn as a curve. Pressing the OK button in the above window is equivalent to executing the following script line:

do ("Draw...", 0, 0, 0, 0, "yes", "Curve")

You see that in a script, all of the arguments are supplied inside the same do function as the command, separated by commas, in the same order as in the settings window, counted from top to bottom (and, within a line, from left to right). The texts "(= all)" and "(= auto)" above are just Praat's explanations of what it means to type a zero in those fields (namely "draw all times" and "use vertical autoscaling", respectively); in a script they are superfluous and you shouldn't write them.

If you want to draw the sound with different settings, say from 1 to 3.2 seconds, scaled between -1 and +1 instead of automatically, with garnishings off, and with the waveform drawn as poles, you would have the following settings window:

In a script this would look like

do ("Draw...", 1.0, 3.2, -1, 1, "no", "Poles")

1. Numeric arguments

The first four arguments in the above examples are numeric arguments: they are (real or integer) numbers. You just write them in the script as you would write them into the settings window.

2. Boolean (yes/no) arguments

The fifth argument in the above examples (Garnish) is a boolean argument (yes/no choice) and is represented by a check button. In the script you write it as "yes" (including the quotes) or "no" (or as 1 or 0).

3. Multiple-choice arguments

The sixth argument in the above examples (Drawing method) is a multiple-choice argument and is represented by an option menu. In the script you write the text of the choice, i.e. "Curve" or "Poles" in the examples.

A multiple choice argument is sometimes represented by a radio box instead of by an option menu. For instance, the last example above could equally well have looked like

In supplying arguments to a command in a script, there is no difference between an option menu and a radio box. This last example will therefore again look like the following in a script:

do ("Draw...", 1.0, 3.2, -1, 1, "no", "Poles")

4. Text arguments

Consider another frequently used menu command, namely Create Sound from formula... in the New menu:

In a script this would look like:

do ("Create Sound from formula...", "sine", 1, 0.0, 1.0, 44100, "1/2 * sin(2*pi*377*x)"

Both the first argument (Name) and the sixth argument (Formula) are text arguments. In a script they are written within quotes.

5. File arguments

The commands from the Open and Save menus, and several other commands whose names start with Read, Open, or Save, present a file selector window instead of a typical Praat settings window. File selector windows ask the user to supply a single argument: the file name.

In a script, you can supply the complete path, including the directory (folder) hierarchy and the name of the file. On MacOS X, it goes like this (if you are user "miep"):

do ("Read from file...", "/Users/miep/Sounds/Animals/miauw.aifc")

or just

do ("Read from file...", "~/Sounds/Animals/miauw.aifc")

where "~" is the Unix way to refer to your home directory. If your file is on the desktop, the command would be:

do ("Read from file...", "/Users/miep/Desktop/miauw.aifc")

or just

do ("Read from file...", "~/Desktop/miauw.aifc")

If your Sounds folder is on a USB drive called Miep, it would be:

do ("Read from file...", "/Volumes/Miep/Sounds/Animals/miauw.aifc")

Instead of these complete path names, you can use relative path names. These are taken as relative to the directory in which your script resides.

On MacOS X, a relative path name starts without a "/". So if your script is /Users/miep/Sounds/analysis.praat, the above line could be

do ("Read from file...", "Animals/miauw.aifc")

Finally, your script may not be in a directory above the directory from which you like to read, but in a directory on the side, like /Users/miep/scripts. The command would then read

do ("Read from file...", "../Animals/miauw.aifc")

6. How to supply arguments automatically

Now you know all the ways to write the arguments of commands in a script line. If you dislike manually copying arguments from settings windows into your script, or if you are not sure whether something is a numeric or a string argument, you can use the history mechanism: choose Clear history from the Edit menu in your ScriptEditor, click your command button, edit the arguments, and click OK. The command will be executed. Then choose Paste history, and the command line, including the arguments, will appear in the ScriptEditor at the position of the text cursor. You can build whole new scripts on the basis of this mechanism.

Links to this page


© ppgb, April 6, 2013