Scripting 4.1. Selecting objects

To simulate the mouse-clicked and dragged selection in the list of objects, you have the commands selectObject, plusObject and minusObject.

Suppose you start Praat and use Create Sound as tone... to create a Sound called tone. In the object list it looks like "1. Sound tone". Suppose you then do To Spectrum... from the Analyse Spectrum menu. A second object, called "2. Spectrum tone" appears in the list and is selected. To select and play the Sound, you can do either

selectObject: 1
Play

or

selectObject: "Sound tone"
Play

So you can select an object either by its unique ID (identifier: the unique number by which it appears in the list) or by name.

The function selectObject works by first deselecting all objects, and then selecting the one you mention. If you don't want to deselect the existing selection, you can use plusObject or minusObject. When the Sound is selected, you can select the Spectrum as well by doing

plusObject: 2

or

plusObject: "Spectrum tone"

If you then want to deselect the Sound, and keep the Spectrum selected, you can do

minusObject: 1

or

minusObject: "Sound tone"

All these functions can take more than one argument. To select the Sound and the Spectrum together, you can do

selectObject: 1, 2

or

selectObject: "Sound tone", "Spectrum tone"

or even

selectObject: 1, "Spectrum tone"

How to refer to objects created in your script

In a script, you typically don't know whether the IDs of the objects are 1 and 2, or much higher numbers. Fortunately, commands that create a new object give you the ID of the object that is created, so that you can refer to the object later on. For instance, suppose you want to generate a sine wave, play it, draw its spectrum, and then throw away both the Sound and the Spectrum. Here is how you do it:

sound = Create Sound as pure tone: "sine377",
... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound
Play ; the Sound is selected, so it plays
To Spectrum: "yes"
Draw: 0, 5000, 20, 80, "yes" ; the Spectrum is selected, so it is drawn
# Remove the created Spectrum and Sound:
plusObject: sound ; the Spectrum was already selected
Remove

You could also select the objects by name:

Create Sound as pure tone: "sine377",
... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; no need to remember the ID of the Sound
Play ; the Sound is selected, so it plays
To Spectrum: "yes"
Draw: 0, 5000, 20, 80, "yes" ; the Spectrum is selected, so it is drawn
# Remove the created Spectrum and Sound:
plusObject: "Sound sine377" ; the Spectrum was already selected
Remove

This works even if there are multiple objects called "Sound sine377", because if there are more objects with the same name, selectObject and plusObject select the most recently created one, i.e., the one nearest to the bottom of the list of objects.

Links to this page


© ppgb, February 23, 2014