Session 6
Object Selection
Making Selections
Praat provides 3 commands to manage object selection in scripts, i.e. to simulate mouse clicks:
selectObject
plusObject
minusObject
The first command selects objects (and deselects others), the second command adds objects to an existing selection (simulating CTRL-clicks), and the third command deselects objects. Each command requires at least one argument to identify the object to select, add, or deselect. Objects can be identified using type + name or using their ID. I strongly recommend to use object's IDs because an object's ID is unique while type + name is not: for example, you can have as many objects of the type Sound with the name tone as you want. Admittedly, Praat has a reliable rule to handle multiple objects with similar type/name (always address the most recently created first) but why bother with keeping track of object names and types when objects can be easily addressed with a fail-safe, unique ID? Let's discuss both identification methods in more detail.
With the first method, identification by type + name, the target object's type and name constitute one (!) string argument. Within the argument, type must be specified first, followed by a space, followed by the name. This statement selects a Sound object called tone1:
selectObject: "Sound tone1"
As you see, the selectObject
command takes one internally structured string argument enclosed in double quotes. The internal structure must match the pattern type+space+name. If tone1 is associated with a TextGrid, you can add this to the selection with:
plusObject: "TextGrid tone1"
Same pattern as above: type of object and name of object, separated by a space. To deselect the sound and keep the TextGrid selected try this:
minusObject: "Sound tone1"
Address objects by ID
Identification by ID is much more straightforward—if you know the IDs (keeping track of IDs is easy with the help of variables). Assuming that the Sound object tone1 has the ID 1 and the TextGrid object tone1 has the ID 2, the sequence below does the same as the sequence above:
selectObject: 1
plusObject: 2
minusObject: 1
Because IDs are numbers they constitute numeric arguments. No double quotes needed, no internal structure to pay attention to. And the object reference with IDs is always unique.
As mentioned above, selecting an already selected object has no consequences. In the following example, a new Sound object is created and automatically selected. A few lines later a selection statement is inserted selecting the same object again:
Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
.
.
.
selectObject: "Sound tone"
Get sampling frequency
That's no problem, you can't over-select objects (it's like clicking with the mouse again and again on the same object in the list—pointless but harmless). Ok, so it's harmless, but it's also redundant—why not leave it out? Sure, you can leave it out, but then you have to stay alert. Imagine you later add some lines between sound creation and sampling frequency query creating a new object and you forget to adapt your script's object handling to the new situation. Then it depends on the type of the newly created object whether you are in trouble or not. If the new object is an object without sampling frequency (e.g. a Spectrum object) you are lucky, because the query fails and Praat reminds you of your mistake with an error message. If the new object is an object with sampling frequency (e.g. another Sound object) you are in trouble, because the query works flawlessly, albeit addressing the wrong object. This should encourage you to apply user controlled object selection generously in your scripts:
When in doubt, use selectObject
.
All three select commands usually have at least one argument but may have more arguments, separated by commas, to select/add/deselect more than one object at once.
selectObject: "Sound tone1"
plusObject: "TextGrid tone1"
is equivalent to
selectObject: "Sound tone1", "TextGrid tone1"
and
selectObject: 1
plusObject: 2
is equivalent to
selectObject: 1, 2
You can even mix identification methods:
selectObject: 1, "TextGrid tone1"
To select objects in a script you need to know their identifying attributes, i.e. type+name or ID. There are two scenarios: (1) You want to query the attributes of already selected objects, so you can use them later in the script. This scenario is covered in the next section (Querying Selections). (2) You want to keep track of objects created by your script. While it is possible to do that using the type+name method, it's much more straightforward with IDs. There's only one trick you need to know: If a statement creates an object it always returns the object's ID. You just have to grab the ID and store it for later use, i.e. assign it to a variable:
# create object from scratch
snd = Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
# create object by loading a file
grid = Read from file: "tone.TextGird"
Then you can use the variable whenever you need to identify the object in your script—unique, fast, and comfortable:
selectObject: snd
plusObject: grid
or
selectObject: snd, grid
Next: Querying Selections