|
This chapter is about how to select objects from your script, and how to find out what objects are currently selected.
To simulate the mouse-clicked and dragged selection in the list of objects, you have the following commands:
select Sound hallo
Play
select all
Remove
In the Praat shell, newly created objects are automatically selected. This is also true in scripts:
! Generate a sine wave, play it, and draw its spectrum.
Create Sound from formula... sine377 1 0 1 44100
... 0.9 * sin (2 * pi * 377 * x)
Play
To Spectrum... yes
! Draw the Spectrum:
Draw... 0 5000 20 80 yes
! Remove the created Spectrum and Sound:
plus Sound sine377
Remove
Instead of by name, you can also select objects by their sequential ID:
select 43
This selects the 43rd object that you created since you started the program (see below).
You can get the name of a selected object into a string variable. For instance, the following reads the name of the second selected Sound (as counted from the top of the list of objects) into the variable name$:
name$ = selected$ ("Sound", 2)
If the Sound was called "Sound hallo", the variable name$ will contain the string "hallo". To get the name of the topmost selected Sound object, you can leave out the number:
name$ = selected$ ("Sound")
To get the full name (type + name) of the third selected object, you do:
fullName$ = selected$ (3)
To get the full name of the topmost selected object, you do:
fullName$ = selected$ ()
To get the type and name out of the full name, you do:
type$ = extractWord$ (fullName$, "")
name$ = extractLine$ (fullName$, " ")
Negative numbers count from the bottom. Thus, to get the name of the bottom-most selected Sound object, you say
name$ = selected$ ("Sound", -1)
You would use selected$ for drawing the object name in a picture:
Draw... 0 0 0 0 yes
name$ = selected$ ("Sound")
Text top... no This is sound 'name$'
For identifying previously selected objects, this method is not very suitable, since there may be multiple objects with the same name:
# The following two lines are OK:
soundName$ = selected$ ("Sound", -1)
pitchName$ = selected$ ("Pitch")
# But the following line is questionable, since it doesn't
# necessarily select the previously selected Sound again:
select Pitch 'pitchName$'
Instead of this error-prone approach, you should get the object's unique ID. The correct version of our example becomes:
sound = selected ("Sound", -1)
pitch = selected ("Pitch")
# Correct:
select pitch
To get the number of selected Sound objects into a variable, use
numberOfSelectedSounds = numberOfSelected ("Sound")
To get the number of selected objects into a variable, use
numberOfSelectedObjects = numberOfSelected ()
n = numberOfSelected ("Sound")
for i to n
sound'i' = selected ("Sound", i)
endfor
# Median pitches of all selected sounds:
for i to n
select sound'i'
To Pitch... 0.0 75 600
f0 = Get quantile... 0 0 0.50 Hertz
Remove
endfor
# Restore selection:
if n >= 1
select sound1
for i from 2 to n
plus sound'i'
endfor
endif
Instead of
Create Sound from formula... sine 1 0 1 44100
... 0.5 * sin(2*pi*1000*x)
sound = selected ("Sound")
you can just write
sound = Create Sound from formula... sine 1 0 1 44100
... 0.5 * sin(2*pi*1000*x)
and instead of
To Pitch... 0.0 75 600
pitch = selected ("Pitch")
you can write
pitch = To Pitch... 0.0 75 600
This only works if the command creates a single object.
© ppgb, January 8, 2011