Introduction & Prerequisites
What is a Praat script? One starting point is to think of Praat scripts as macros. An application that supports macros (e.g. text processors, Photoshop etc.) allows you to record a series of GUI actions and to replay them as often as needed. GUI actions are actions you perform in the graphical user interface of an application, like pointing and clicking with the mouse or pressing keys on the keyboard. If you replay the macro, each recorded action is executed one after the other in the order you recorded them. So, with a macro you can automate a complex task and rerun it reliably, i.e. you don't need to worry about typos or missed clicks during later iterations. The benefit of macros is threefold: recurring tasks become less tedious, much faster and more reliable.
Praat scripts can do all that for you. You can collect any amount of Praat GUI actions in a Praat script and replay them as often as you like. You can save the script for later use or to distribute it among your students or colleagues (to make their lives easier or to make sure that they perform an analysis exactly the same way as you intended). Praat also provides you with a macro recording mechanism (the command history which is introduced in Session 1), accessible within the Praat script editor (also introduced in Session 1).
But Praat scripts are much more versatile than simple macros. First, because Praat scripts are plain text files. The Praat script editor obviously is the first choice to handle Praat scripts but principally you can use any text editor you like for editing. You don't even need Praat to edit Praat scripts! Usually, that's a bad idea because you want to test your scripts frequently during development and of course you need Praat to run scripts. But it is possible for example to adjust some parameters in a script that was mailed to your smartphone and send it back. Free choice of the editor is nice (especially since the Praat script editor is pretty simple) but much more important is the freedom to edit everything in a Praat script:
- You can reorder recorded actions.
- You can adjust parameters (e.g. if you recorded the GUI-action
Spectrogram settings...with the window length set appropriate for broad band analysis you can simply adjust window length later if you need narrow band analysis - no need to record the macro again).
- You can add new or delete obsolete actions.
- You can write your own code.
The last point of the list above—writing your own code—is the second reason why Praat scripts have the potential to be much more than macros (and the reason why you may want to attend this tutorial…): Praat lets you not only record GUI-actions but provides you with the power of a full-featured scripting language. With a scripting language you can add some logic to your macros, like for example:
- conditional execution of actions (e.g.: if the speaker is female set pitch range to 150..400 Hz else set pitch range to 50..250 Hz)
- looped execution of actions (e.g.: measure duration of interval for every interval in a TextGrid)
- ask the user for some input before execution of actions (e.g.: do you want to look at broad band or narrow band spectrograms)
- give the user some feedback during execution
- evaluate mathematical expressions
- make use of variables
While you can still record simple (dumb) macros, now you can add some code and have much more useful (intelligent) macros. Macros - dumb or intelligent - adapt specifically to your current materials and tasks, they are only of 'local' interest (to you and maybe your workgroup). If you wish to develop generic scripts that are of 'global' interest (a functionality useful for other Praat users working with completely different materials), that's also possible with the Praat scripting language. It's even possible to create new menu items and buttons in the Praat GUI to launch your scripts. Furthermore (and not covered in this tutorial), because we can't only add new GUI elements but also hide existing ones, we're able to create our own individual Praat version (cf. Praat plug-in mechanism)! In fact, the Praat scripting language lets us even create our own application, backed by the power of Praat but with an autonomous GUI completely controlled by Praat scripts (cf. Praat demo window).
The only prerequisite to attend this tutorial is that you have some experience with Praat. You should be more or less familiar with the basic tasks that could be achieved in the Praat graphical user interface (windows, menus, buttons):
- Loading files
- Managing objects (create, copy, rename, save, remove)
- View objects and analyses
- Setting analysis parameters
- Querying objects (using buttons or menu commands starting with
- TextGrid annotation
Experience with scripting or programming languages is not required! As you proceed through the tutorial all the basic principles of scripting (conditionals, loops, variables etc.) will be covered and explained. If you have scripting or even programming experience you should consider another approach towards learning Praat scripting, because reading through this tutorial most of the time you'll be bored. You might read the first sessions to learn about Command History and Scripting Environments. After that just plunge right into the Praat help on scripting and/or look at other's Praat scripts to become acquainted with the idiosyncrasies of the Praat scripting language.
Up-to-date Praat version
To follow this tutorial you'll need a current version of Praat running on your computer. I recommend you use Praat version 5.3.64 or newer (to find out what version you have, find and click the
About Praat... menu item); if you need to update, download now from the Praat homepage. (As you know, you can have several versions of Praat installed on your computer at the same time. So if you already have an older version which you would like to keep, just download the latest version and run it parallel to the old version.)
Starting with Praat version 5.3.44 (April 2013), Paul Boersma, the Praat developer, established a new concept called variable-substitution-free scripting. Simultaneously, a new scripting syntax was introduced that differed considerably from the old syntax that was in use in the years before. Starting with version 5.3.63 (January 2014), the scripting syntax was modified again, resulting in the currently valid brand-new version. The brand-new syntax is a blend of the new and the old syntax and I hope it will remain unchanged for some years. This tutorial is based on the 'brand-new syntax'. This means two things: (1) If you try to reproduce examples given in this tutorial or if you try to work through the tutorial using an old Praat version, your scripts won't run! And (2), if you download Praat scripts from the internet, you'll most likely look at scripts using the old syntax; those scripts will run with new Praat versions without any problems, but to be able to understand them read the hints on old functions in the manual.
Now, let's get started! First, we'll review the basic concepts of Praat scripting and of course you'll learn and practice macro creation in the first session. But also already in the first session you'll become acquainted with the concept of editing scripts and adding additional code. Have fun :-)Next: Session 1: Script Editor & Command History