Session 2
Syntactic Structure
Comments & Whitespace
As any other scripting/programming language the Praat scripting language allows comments. Comments are lines which are ignored during the execution of the script. So, what's the benefit of comments? First, with comments you can explain your script. This could be useful for yourself if you revisit a script you coded months ago and try to adapt it to your current task. More often than not you'll ask yourself what a given line or sequence of lines contributes to the script. That's the moment when you'll be grateful for any comment that tells you what the script actually does. If you distribute your scripts among your colleagues or offer them for download it's imperative to include at least some usage information and requirements. This is best done as a comment section at the beginning of the script.
Another more practical benefit of comments is the possibility to comment out statements. This allows us to deactivate statements without deleting them. Reactivation is just a matter of deleting the comment indicator. That's a very common and useful practice during the testing/debugging of scripts.
The comment indicator in Praat scripts is the hash (#) symbol. Lines starting with the hash symbol are not executed when you run the script, they are just ignored and execution is continued in the next line. To function like this, the hash symbol must be the first symbol in that line. Leading whitespace (space/tab) in front of the hash symbol is allowed, but nothing else.
# I'am a valid comment
Play
# I'am another valid comment
# Yet another valid comment with some whitespace in front of it
Play # Error! Hash symbol must be first in line!
The following script creates a tone, replays it, and opens it in a sound editor:
Create Sound as pure tone "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Play
Edit
To temporarily deactivate the Play
statement just put a hash (#) in front of the statement:
Create Sound as pure tone "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
#Play
Edit
Now the tone is opened immediately after creation. Delete the hash to listen to the tone again.
Readability of scripts
Comments are a great tool to improve readability of scripts (for humans). Another great tool complementing comments to achieve better readability is whitespace. Whitespace is applied in order to visually reflect the structure of scripts. Praat allows generous usage of whitespace: We can insert empty lines everywhere we want and we can indent lines with spaces or tabs whenever we want. I recommend you commit yourself to two rules right at the beginning of your scripting career to make your scripts better (not only look better):
- Separate code blocks (sequences of statements dedicated to the same subtask) with empty lines (and consider introducing them with a comment)
- Indent lines within loops, conditionals, and similar blocks (forms, procedures etc.)
We'll come back to this issue as soon as our scripts grow and include loops and other fancy stuff.
Recap
One complete statement per line! Statements often consist of a command and its arguments (as you'll see later, there are some other types of statements). Arguments are separated by commas and have a fixed order. String arguments must be enclosed in double quotes, numeric arguments must not. It is good practice to insert complex GUI commands (i.e. GUI commands with many arguments) with the help of command history.
Don't be afraid of error messages! They are common, frequent, and helpful—at least the line number.
Comments and whitespace help you and others to read and understand your scripts.