Praat scripting tutorial

Interacting with the user via windows

Praat gives us a couple of commands to create popup windows and interact with the user. We've already seen "pauseScript" and "exitScript", but here we'll look at two more: "form" and "beginPause". Let's dive in and start with a quick example of both:

form This is on the top of the Window
	comment This is a form
	comment I am uneditable text used for messages to the user.
	real Real_number_form 5
	text Enter_text_form You can enter text here

appendInfoLine: "Form values: ", real_number_form, " ", enter_text_form$

beginPause: "This is on the top of the Window"
	comment: "This is a beginPause window"
	comment: "I am uneditable text used for messages to the user."
	real: "Real number", 5
	text: "Enter Text", "You can enter text here"
clicked = endPause: "First button", "Second button", 2

appendInfoLine: "Begin pause values: ", real_number, " ", enter_Text$
appendInfoLine: clicked, " was clicked"

Study and run the code. Hopefully you've noticed some interesting things, and have a lot of questions. This is one area of Praat scripting that I think is a little obtuse, the documentation for this topic is spotty, and the syntax of "form" is inconsistent with the syntax of the rest of the language, so always treat these forms with caution, and do a lot of testing while you build them: start small and run constantly before you program a long form.

The difference between form and beginPause

I can only really see two practical differences, besides the obvious disparities in syntax requirements (semi-colons, quotes, etc.). The first is that a form will run before any other code, no matter where it is in the file! Try it out, put the form code after the beginPause, and see which comes up first. The reasoning is that "forms" are for getting arguments into your script: setting variables it needs to run, so this must happen first.

The second difference is that you have more control over the available buttons that the user can click. We'll talk about those details in a second. Someone correct me if I'm wrong, but I see no advantage in using a form instead of beginPause, and beginPause conforms somewhat better to Praat scripting syntax, since we have the familiar colon, and difference between strings and numbers. From here on out we'll only be talking about beginPause, since there's virtually no difference.

A note on terminology: as I'm writing this I'm having trouble not using the word "form" when talking about the window generated by beginPause. This is because in general programming terminology a form is any window where the user is required to submit information for us to process. Think about when you register to use a website, or give your credit card information for a purchase. These are "forms". I'll try to be good and only use "beginPause" here.

Variables in beginPause

On to the details. Hopefully you've noticed some peculiar things about the variables: Look at the line where we ask for a number:

real: "Real number", 5

We'll talk about what is meant by "real" in a second. The first argument to real: is the string "Real number". Run the script again and find it on the form. Praat converts this string into a variable name. Notice later in the script that I printed the value of a variable named "real_number". Remember from the lesson on variables where we talked about legal variable names in Praat? Two important rules are that variable names have to start with a lowercase letter and cannot contain spaces, so Praat converted "Real number" to "real_number". Be careful what text you enter here since this will become a variable name. If instead of typing "Real number" we entered "1Real number" or "2", these cannot be converted into legal variable names, so even though beginPause will run without error, you'll have no way to access the input value (I wish the Praat compiler threw an error instead so that this error is more obvious).

Keeping this conversion to variables in mind, look at the line getting text input, and find where we printed its value. Our string "Enter text" became "enter_text$". Since this input is going to get text from the user, it obviously must be saved to a string variable, hence the dollar sign.

Number types in beginPause

Now what's the deal with "real:"? Remember in the chapter introducing string variables where we talked about different data types? In many programming languages, they have many more types of variables than numbers and strings, so that computer memory can be carefully managed. We mentioned how there are actually several different subcategories of numbers, like "integer", "float", "double", etc. Praat has hidden this complexity from us by choosing one of these number types behind the scenes, up until now.

For numbers in our forms, we can use "natural", "integer", "positive", or "real". I've put these in order of complexity of the numbers that these types will accept. Here's a rundown of the differences:

Praat is doing us a favor here by requiring we specify the type of number we want. If we specify that a field only accepts "natural" numbers, and a user enters "0.32", they will be shown a message telling them only positive whole numbers are accepted. Alter our script to require "natural" numbers, run it, and try to enter a number like -32, to view a sample error message. This way we don't have to code in checks to clean their input on numbers. Our script would break if we asked for a tier number and they included a decimal number.

Praat's requirement that decimal numbers less than one begin with a zero still stands (good: 0.89, bad: .89), and it gives a cryptic "Symbol misplaced" error if the user doesn't follow this convention. I would consider including a comment above a line like this warning the user of this.

Buttons in beginPause

Look at the last line for endPause:

clicked = endPause: "First button", "Second button", 2

As arguments to endPause, we gave it strings, followed by a number. The strings are the names of buttons we want to show, and the number is which button will be highlighted by default (in this case the second one). The variable, "clicked", is saving the index of the button chosen (in this case, 1 for "First button", 2 for "Second button". Praat automatically generates buttons for quitting for us.

Other types of input for beginPause

Getting text

You can get the user to enter text with "word", "sentence", or "text".

Note that "sentence" is for shorter sentence, and "text" for longer text input. The manual doesn't indicate if there is some type of limit to how long input can be for "sentence".

Radio buttons: choice

Enter this code and run it:


beginPause: "Radio button example"
	comment: "This is a radio button box"
	# first argment is variable name,
	# second is default selection
	choice: "Team", 3
		option: "Team Edward"
		option: "Team Jacob"
		option: "Team Twilight Sux"
clicked = endPause: "Impressed", "Not impressed", 1

appendInfoLine: "I chose team: ", team$,
			..." which is number ", team, 
			..." and clicked button number ", clicked

It presents a box of radio buttons, where only one selection is allowed. One thing I really like about it is that generates a numeric variable (here 'team') containing the index of the selection, and a string variable (team$) containing the text of the selected radio button.

Checkboxes: boolean

Lastly, you can get the user to give you a yes / no, true / false value using "boolean":


beginPause: "What type of person are you?"
	boolean: "Dog person", 1
	boolean: "Cat person", 0
clicked = endPause: "Submit", 1

if dog_person and cat_person
	exitScript: "You can't be both a dog and a cat person. You must choose."
elif dog_person
	appendInfoLine: "You are a dog person"
elif cat_person
	appendInfoLine: "You are a cat person"
	exitScript: "Programmer error, I thought I programmed for all possibilities.."

Next page: Procedures