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:

Draw... 0 0 0 0 yes Curve

You see that in a script, all of the arguments are supplied on the same line as the command, 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

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

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:

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. They are written in a script just as you would type them into the settings window. Well, mostly (see 6 and 7 below)...

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

Read from file... /Users/miep/Sounds/Animals/miauw.aifc

or just

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:

Read from file... /Users/miep/Desktop/miauw.aifc

or just

Read from file... ~/Desktop/miauw.aifc

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

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

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

Read from file... ../Animals/miauw.aifc

6. Space paranoia

The thing that separates the arguments in a script line is the space character (" "). This can become problematic if the argument itself also contains spaces, as can happen in text arguments such as in Formula above under 4 (because "1/2 * sin(2*pi*377*x)" contains two spaces). If the text argument with spaces is the last argument (as Formula is above), then there's actually no problem: Praat knows that the Create Sound from formula... command takes six arguments; the sixth argument is simply everything that follows the first five arguments, so Praat can figure out that those last two spaces aren't meant to separate arguments.

For this reason, most texts in Praat's settings windows appear in the last (bottom) field. When they don't, you have to use double quotes around the text argument with spaces. For instance, consider the command Report difference (Student t)... (for Table objects):

This command performs a paired-samples t-test between the columns F0 before and F0 after. In a script this would look like:

Report difference (Student t)... "F0 before" "F0 after" 0.025

The quotes around the first argument tell Praat that the space between F0 and before is part of the argument, i.e. part of the first column name.

The last, and most confusing, strange thing that can happen is if a (non-last) text argument contains quotes itself. In such a case, you enclose the argument between quotes, and you double each quote that appears inside the argument. For example, the following window

is scripted as

Report difference (Student t)... "F0 ""before""" "F0 ""after""" 0.025

7. How to supply arguments automatically

Now you know all the ways to write the arguments of commands in a script line, including some weird cases. If you dislike manually copying arguments from settings windows into your script, or if you are still not sure how a complicated argument should be written in a script, 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 (with correct quotes, for example), 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, January 6, 2011