Session 3
Scripting Environments
Switching Environments
Now you know how to create shell scripts (if you want to use shell commands) and how to create editor scripts (if you want to use editor commands). But what if you want to use both shell commands and editor commands in one and the same script? For example, if you want to create an object and open it in an editor (shell commands) and then set a specific pitch range and enable the pitch display (editor commands). Or if you want to select part of a signal and extract it as a new object (editor commands) and then rename the new object and draw it to Praat Picture (shell commands). Fortunately, Praat provides a couple of commands to switch between environments in scripts: editor
and endeditor
.
Use editor
to switch to an editor environment. Because editor environments are ambiguous the editor
command has an obligatory argument to specify the target environment (with one exception, see below). The argument is a string (therefore, enclosed in double quotes) and must contain the type and the name of the editor environment, separated by a space (starting with Praat version 5.3.78 you can use an object's ID (which is a number; cf. Object Selection) instead). A template for a complete editor
statement looks like this (Type and Name must be replaced with the actual type and name):
editor: "Type Name"
That enables us to correct the erroneous script from above:
Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Play
Edit
editor: "Sound tone"
Zoom in
Zoom in
Zoom in
Zoom in
That works because the object we opened in an editor is of the type Sound, so the editor is of the same type; and because the object we opened is called tone (cf. 1st argument of the 1st statement), so the editor is also called tone (cf. sound editor window title). Line indentation below the editor
statement is not mandatory, it's just a visual reminder of the environment switch (cf. Comments and Whitespace).
To switch back to the shell environment use the endeditor
command. Since the shell environment is unique there's no need for arguments:
Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Play
Edit
editor: "Sound tone"
Zoom in
Zoom in
Zoom in
Zoom in
endeditor
Play
The switch commands themselves are neither shell nor editor commands, i.e. they may be used in any environment. For example, you can switch from within an editor environment to another editor environment:
# create and open first tone
Create Sound as pure tone: "tone1", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Edit
# create and open second tone
Create Sound as pure tone: "tone2", 1, 0, 0.4, 44100, 220, 0.2, 0.01, 0.01
Edit
# switch to first editor and play window
editor: "Sound tone1"
Play window
# switch to second editor and play window
editor: "Sound tone2"
Play window
# back to the shell
endeditor
Play
The script creates and opens two tones one after the other. Then it switches to the first editor's environment and plays the window (an editor command located in the endeditor
command and plays the selected sound, which is tone2.
Note that endeditor
does not return to the previous environment (which would be the first editor in the above example) but always returns directly to the shell environment. Hence, the use of whitespace in the above example might be misleading since it creates the impression that environments are nested. That's not the case. Each editor
statement steers the script to a new environment and at the same time abandons any previous environment. To reflect this logic all editor
statements should share the same indentation level:
Create Sound as pure tone: "tone1", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Edit
Create Sound as pure tone: "tone2", 1, 0, 0.4, 44100, 220, 0.2, 0.01, 0.01
Edit
# abandon shell and switch to first editor environment
editor: "Sound tone1"
Play window
# abandon first editor environment and switch to second
editor: "Sound tone2"
Play window
# abandon second editor environment and switch to shell
endeditor
Play
The endeditor
command steers the script back to the shell but activated editor windows stay open. If you want to return to the shell and close the editor window at the same time use the Close
command instead. Close
is a menu command located in the
Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
Edit
# switch to editor
editor: "Sound tone"
Play window
Close
# back in the shell
Play
So far, we always started in the shell then switched to an editor and back to the shell. Of course, the other way round is also possible: start as an editor script then switch to the shell and back to the editor. To try this, we'll expand the editor script from the previous section:
Move cursor to: 0.19
Move end of selection by: 0.02
Zoom to selection
Running that script we have a nice selection of 20 ms in the middle of the sound. Let's extract that selection, rename it, and return to the editor to continue working with the zoomed out signal. This could be achieved with the following script (if you use command history don't forget to manually add environment switching statements!):
Move cursor to: 0.19
Move end of selection by: 0.02
Zoom to selection
Extract selected sound (time from 0)
endeditor
Rename: "sound-snippet"
editor
Show all
Object renaming is a shell task, so we must abandon the editor environment to execute the Rename
statement. Since the script started as editor script it has its native habitat: the editor environment it belongs to. Returning to the home environment should be easy for an editor script, and indeed it is: a simple editor
statement will suffice. In this case (the exception mentioned above) no argument is needed. Bare editor
statements are only allowed in editor scripts, meaning return to the home editor. Back at home, the last statement of the script triggers the zoom out action.
It is also possible, of course, to switch to other editor environments within editor scripts. In that case a complete editor statement is necessary (editor: "Type Name"
).
Recap
A script running in the shell environment is able to execute all GUI commands located in Praat Objects and Praat Picture. A script running in an editor environment is able to execute all GUI commands located in the menus of the specified editor.
Depending on its initial environment a script is called shell script or editor script. The initial environment of a script is determined by the way it is created/opened: shell scripts are managed with the
endeditor
always activates the shell environmenteditor
always activates the native editor environment of an editor script (therefore, only meaningful and allowed in editor scripts)editor: "Type Name"
activates the specified editor environmentClose
closes the current editor window and activates the shell
Each statement abandons the previous environment since only one environment can be active at any given script line.