Scripting 6.1. Arguments to the script

You can cause a Praat script to prompt for arguments. The file playSine.praat may contain the following:

form Play a sine wave
    positive Sine_frequency_(Hz) 377
    positive Gain_(0..1) 0.3 (= not too loud)
endform
Create Sound as pure tone: "sine" + string$ (sine_frequency), 1, 0, 1, 44100, sine_frequency, gain, 0.01, 0.01
Play
Remove

When running this script, the interpreter puts a settings window (form) on your screen, entitled "Play a sine wave", with two fields, titled "Sine frequency (Hz)" and "Gain", that have been provided with the standard values "377" and "0.3 (= not too loud)", which you can change before clicking OK.

As you see, the underscores have been replaced with spaces: that looks better in the form. Inside the script, the field names can be accessed as variables: these do contain the underscores, since they must not contain spaces, but the parentheses (Hz) have been chopped off. Note that the first letter of these variables is converted to lower case, so that you can assign to them in your script.

Inside the script, the value "0.3 (= not too loud)" will be known as "0.3", because this is a numeric field.

You can use the following field types in your forms:

real variable initialValue
for real numbers.
positive variable initialValue
for positive real numbers: the form issues an error message if the number that you enter is negative or zero; further on in the script, the number may take on any value.
integer variable initialValue
for whole numbers: the form reads the number as an integer; further on in the script, the number may take on any real value.
natural variable initialValue
for positive whole numbers: the form issues an error message if the number that you enter is negative or zero; further on in the script, the number may take on any real value.
word variable initialValue
for a string without spaces: the form only reads up to the first space ("oh yes" becomes "oh"); further on in the script, the string may contain spaces.
sentence variable initialValue
for any short string.
text variable initialValue
for any possibly long string (the variable name will not be shown in the form).
boolean variable initialValue
a check box will be shown; the value is 0 if off, 1 if on.
choice variable initialValue
a radio box will be shown; the value is 1 or higher. This is followed by a series of:
button text
a button in a radio box.
comment text
a line with any text.

Inside the script, strings are known as string variables, numbers as numeric variables. Consider the following form:

form Sink it
    sentence Name_of_the_ship Titanic
    real Distance_to_the_iceberg_(m) 500.0
    natural Number_of_people 1800
    natural Number_of_boats 10
endform

In the script following this form, the variables will be known as name_of_the_ship$, distance_to_the_iceberg, number_of_people, and number_of_boats.

The variable associated with a radio box will get a numeric as well as a string value:

form Fill attributes
    comment Choose any colour and texture for your paintings
    choice Colour: 5
       button Dark red
       button Sea green
       button Navy blue
       button Canary yellow
       button Black
       button White
    choice Texture: 1
       button Smooth
       button Rough
       button With holes
endform
writeInfoLine: "You chose the colour ", colour$, " and the texture ", texture$, "."

This shows two radio boxes. In the Colour box, the fifth button (Black) is the standard value here. If you click on "Navy blue" and then OK, the variable colour will have the value "3", and the variable colour$ will have the value "Navy blue". Note that the trailing colon is chopped off, and that the button and comment texts may contain spaces. So you can test the value of the Colour box in either of the following ways:

if colour = 4

or

if colour$ = "Canary yellow"

The field types optionmenu and option are completely analogous to choice and button, but use up much less space on the screen:

form Fill attributes
    comment Choose any colour and texture for your paintings
    optionmenu Colour: 5
       option Dark red
       option Sea green
       option Navy blue
       option Canary yellow
       option Black
       option White
    optionmenu Texture: 1
       option Smooth
       option Rough
       option With holes
endform
writeInfoLine: "You chose the colour ", colour$, " and the texture ", texture$, "."

You can combine two short fields into one by using left and right:

form Get duration
    natural left_Year_range 1940
    natural right_Year_range 1945
endform
duration = right_Year_range - left_Year_range
writeInfoLine: "The duration is ", duration, " years."

The interpreter will only show the single text "Year range", followed by two small text fields.

Calling a script from another script

Scripts can be nested: the file doremi.praat may contain the following:

runScript: "playSine.praat", 550, 0.9
runScript: "playSine.praat", 615, 0.9
runScript: "playSine.praat", 687, 0.9

With runScript, Praat will not display a form window, but simply execute the script with the two arguments that you supply on the same line (e.g. 550 and 0.9).

Values for choice must be passed as strings:

runScript: "fill attributes.praat", "Navy blue", "With holes"

You can pass values for boolean either as "yes" and "no" or as 1 and 0.

Links to this page


© ppgb, February 12, 2014