Session 4
Generating Output

If you ever read a book that aimed to teach you a programming language, the first thing you learned was probably how to output Hello World. So far, I managed to ignore that tradition, but it's about time now to enlighten you. This is how you do it in Praat:

writeInfo: "Hello World"

The writeInfo command can have multiple arguments, strings as well as numbers, and writes all arguments to Praat Info. writeInfo has a sibling, writeInfoLine, and it has cousins: appendInfo and appendInfoLine. All 4 commands open the Praat Info window (if it's not already open) and write their argument(s) to the window, but they differ in what they do before and after writing. The write commands clear Praat Info before writing (erasing everything that was previously written), while the append commands add their arguments to the existing content of the window. So if you want to ensure that Praat Info is empty before your script starts outputting stuff, use one of the write commands. Very often, the first output command in a script is a write command, while subsequent output is generated with append commands. This way, the script's output is aggregated step by step. If you just want to bring an empty Praat Info window into the foreground to set the stage for later output, you use writeInfo: "" (or the archaic but simple clearinfo command).

The difference between writeInfo and appendInfo on one side and writeInfoLine and appendInfoLine on the other side is that the latter add a line break after writing, so that the next output statement starts writing to a new line.

writeInfo: "Hello World" appendInfo: "I like icecream"

produces the following output:

Hello WorldI like icecream

If you want two lines of output try:

writeInfoLine: "Hello World" appendInfoLine: "I like icecream"

This produces:

Hello World
I like icecream

Overview of the output commands:

command clear before writing add line break after writing
writeInfo yes no
writeInfoLine yes yes
appendInfo no no
appendInfoLine no yes

The output commands don't insert separators between multiple arguments, this is your responsibility:

writeInfoLine: "I", "like", "icecream" appendInfoLine: "I", " ", "like", " ", "icecream"

produces:

Ilikeicecream
I like icecream

To use tabs as separators, Praat provides the pre-defined variable tab$ (you'll learn about variables in the next session; pre-defined means, there's a value assigned to to the variable out of the box, namely a tab stop). To insert tab stops instead of spaces, try this:

appendInfoLine: "I", tab$, "like", tab$, "icecream"

Another pre-defined variable is newline$, assigned with a line break. These statements:

appendInfo: 1, tab$, 2, tab$, 3 appendInfoLine: tab$, 4

produce the same output as

appendInfo: 1, tab$, 2, tab$, 3, tab$, 4, newline$

This seems a bit redundant at the moment, but using non line breaking output commands and adding a newline$ at strategic points can prove helpful if the output is generated by loops.

By the way, arguments of output commands behave like other arguments: string arguments must be enclosed in double quotes, numeric arguments (and variables) must not. In the examples above, I, like, and icecream are strings, while 1, 2, 3, and 4 are numbers.

The content of Praat Info may be selected with the mouse, copied, and pasted somewhere else. Or it can be saved as a text file (Save as... in the File menu of Praat Info) to be imported in other software programs.

Debugging

Obviously, an important purpose of output commands is to write out final measurement results. But there's another purpose which is just as important: Debugging. Writing scripts means telling the computer to do something. Using scripts means to rely on the computer that things are done right. But the computer can't always tell right from wrong. If a script contains formal errors (typos, syntax errors, improper environment etc.) the computer usually alerts you with an error message. But if a script contains semantic or logical errors the computer just indifferently executes what you told it. It's your responsibility as a script author to detect and fix (= debug) this latter type of errors so that your scripts can be relied on! A very useful debugging method is to frequently write out and verify interim results. During development, make your scripts maximally verbose, i.e. insert output commands at every crucial position in order to review and validate your algorithms. At the end, when you're sure that everything works as expected, you can simply delete (or comment out) any superfluous output commands to prepare your script for productive operation.

Recap

Praat provides four commands to write text or numbers to Praat Info. Each commands accepts one or more arguments. Writing to Praat Info can have multiple purposes: Informing the script user about the progress of the script, warning the script user, aggregating measurement results to be processed with spreadsheets or statistics software, debugging, and so on.

tab$ and newline$ are pre-defined variables, representing a tab stop and a line break, respectively.

Next: Workshop: Generating Output