Scripting 1. Your first scripts

This page tells you how to create, run and save a script. To get a feel for how it works, you are advised to try out all the steps.

## 1. A minimal script

Suppose that you want to create a script that allows you to play a selected Sound object twice. You first create an empty script, by choosing New Praat script from the Praat menu in the Objects window. A ScriptEditor window will appear on your screen:

In this window, you type

`Play
` `Play
`

Now select a Sound in the Objects window. As you expect from selecting a Sound, a Play button will appear in the dynamic menu. If you now choose Run from the Run menu in the ScriptEditor, Praat will play the sound twice. This works because Play is a command that becomes available in the dynamic menu when you select a Sound.

## 2. Some more commands

In the above example, you could use `Play` in your script because the Play command was available, i.e. because “Play” was the text on a button currently available in the dynamic menu. Apart from these selection-dependent (dynamic) commands, you can also use all fixed commands from the menus of the Objects window and the Picture window. For instance, try the following script:

`Erase all
` `Draw inner box
` `Play
` `Play
` `Erase all
`

When you run this script, you’ll see a rectangle appear in the Picture window (that’s what the command Draw inner box in the Margins menu does), then you’ll hear the Sound play twice, then you’ll see the rectangle disappear from the Picture window (that’s what the command Erase all from the Edit menu does).

Here we see that the Praat scripting language is an example of a procedural programming language, which means that the five statements are executed in the order in which they appear in the script, i.e. first Erase all, then Draw inner box, then Play twice, and finally Erase all.

## 3. Experimenting with your script

You don't have to be afraid of making mistakes. Here are a couple that you can try to make.

First, try to run the script when a Sound is not selected (e.g. you create a Pitch object from it and keep that selected, or you throw away the Sound). You will notice that Praat gives you an error message saying “The command “Play” is not available for the current selection”. Indeed, if you select a Pitch or if you select nothing, then no command Play appears in the dynamic menu, so the script cannot execute it. Note that the commands Erase all and Draw inner box are still available, because they continue to be present in the menus of the Picture window; therefore, the script will execute the first two lines (`Erase all` and `Draw inner box`) and stop running at the third line, i.e. at your first `Play`. The result is that the “box” will stay visible in the Picture window, because the fifth line of the script, which should erase the box, is never executed.

Second, try to mistype a command (there’s a good chance you already did it by accident), e.g. write `PLay` instead of `Play`, or `Draw inner bocks` or whatever. Again, you are likely to get a message saying that that command is not available. Such messages are the most common messages that you’ll see when writing scripts; now you know that they mean either that you mistyped something or that you made the wrong selection.

## 4. Saving your script

The File menu of the ScriptEditor has a command Save, with which you can save your script as a file on disk, for instance under the name “test.praat”.

Please try this with the five-line script you just typed. After saving the script, the name of the script file will appear in the window title:

After you save your script, you can close the ScriptEditor window without losing the script: you can reopen the script file by using Open Praat script... from the Praat menu, or by choosing New Praat script again, followed by Open... from the ScriptEditor’s File menu.

It advisable to use “.praat” as the extension for script file names. On the Mac, if you double-click a “.praat” file, Praat will automatically start up and show the script. On the Mac and on Windows, if you drag a “.praat” file on the Praat icon, Praat will also start up and show the script.

### Links to this page

- Scripting
- Scripting 2. How to script settings windows
- Scripting 3. Simple language elements