Session 3
Scripting Environments

About Environments

Remember the script below from the session on command history:

Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01 Play Edit Zoom in Zoom in Zoom in Zoom in

This script terminates in line 4 with an error message. The reason is that the script runs in what is called the Praat shell and the Praat shell doesn't know about the command Zoom in. The Praat shell is only aware of GUI commands available in Praat Objects and Praat Picture. Praat Objects and Praat Picture are the primary components of the Praat GUI, everything else you can do with Praat you'll start either in Praat Objects or Praat Picture. That's why these two windows are displayed when Praat starts up and that's also why this environment is called the shell, because it provides the framework for the entire Praat functionality.

Shell scripts

How do you know whether a script runs in the Praat shell? Well, that's easy: If you create a new script with the New Praat script command or if you open an existing script with the Open Praat script... command then your script will run in the Praat shell. If the distinction between different environments is important we'll call those scripts Praat shell scripts.

Editor scripts

Now, what about Zoom in? Zoom in is one of many editor commands, i.e. a command which is only available in the GUI when a sound editor (or any other editor) is open. As we noticed above, editor commands can't be readily used in scripts which reside in the shell environment. Can they be used at all? Yes, of course, they feel perfectly at ease in scripts which reside in the editor's environment. Scripts which run in an editor's environment are created or opened with New editor script or Open editor script...; both commands can be found in every editor's File menu. To distinguish those scripts from Praat shell scripts we'll call them Praat editor scripts.

Let's try it and create our first editor script. We'll use the tone which was created with New > Sound > Create Sound as pure tone... and standard settings. Open the tone in a sound editor and create a new editor script (in the sound editor select File > New editor script). The familiar Praat script editor appears in a new window. First, as always, clear the history. Then switch back to the sound editor and click some buttons or menu items. For example, to select and view exactly 20 ms in the middle of the signal

  • choose Select > Move cursor to... and type 0.19
  • then choose Select > Move end of selection by... and type 0.02
  • finally, choose View > Zoom to selection

This is what we want to achieve with the script, so let's paste the history:

Move cursor to: 0.19 Move end of selection by: 0.02 Zoom to selection

To test the script, choose View > Show all in the sound editor to reset the view and click anywhere in the signal to expunge the selection. Now, when you run the script, the desired selection and zoom is instantly restored. You can even close the sound editor (not the script editor!), open it again (with the same sound!), and run the script—with the identical result.

If you have a close look at the script editor containing the newly created editor script you'll notice a small but important difference to a script editor containing a shell script (just open a new shell script to see the difference; yes, you can have multiple script editors open simultaneously). The difference is in the window title: shell scripts have only their name in the title (untitled script for unsaved/unnamed scripts) while editor scripts have their name plus the specification of their environment in square brackets. The specification resembles the specification of the object in the Praat Objects list: ID, object type, and object name. If the tone was the first object which was created after starting up Praat, the specification should look like [1. Sound tone]. Because it is the first object the ID is 1, the type of the object (and the type of the editor in which it opens) is Sound, and the name of the object is tone.

One shell environment,
many editor environments

This is a remarkably precise and strict specification of the environment and Praat is in fact very serious about it: This script wouldn't run with another editor! Every open editor initiates its own environment and every editor script is dedicated to exactly one and only one editor environment. That means, if you create a new tone and open it in an editor, the existing script won't do anything to that editor. The new editor is specified as, for example, [2. Sound tone] which is not the same as [1. Sound tone] where the existing script resides, therefore the existing script can't affect the new editor.

To use an existing editor script with a new editor there are two options:

  1. Create a new empty editor script in the new editor, then copy the statements from the existing script to the new script and run it.
  2. Save the existing script to your harddrive, open it in the new editor environment (File > Open editor script...), and run it.

Permanent shell environment,
transient editor environments

Editor environments are transient, i.e. they only exist as long as the respective editor window is open. When you close an editor window its environment ceases. If you create an editor script and then close the editor to which it belongs, the script is homeless. If you try to run a homeless editor script Praat will crash, so be careful! If you accidentally closed an editor window and made a script homeless you can make the script happy again by reopening the object, hence restoring the script's familiar environment.

To summarize: There is one permanently available shell environment and as many temporarily available editor environments as there are open editor windows. Each editor environment is autonomous and strictly specified. Each editor script belongs to exactly one editor environment, which is specified in the window title of the script editor.

The Praat command history, by the way, is global and indifferent regarding environments, i.e. it obstinately records every single GUI action without interest in its environmental origin.

Next: Switching Environments