Scripting 4.3. Querying objects

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 Pitch again:
selectObject: “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:
selectObject: 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 ()

Example: doing something to every selected Sound

sounds# = selected# (“Sound”)
# Median pitches of all selected sounds:
for i to size (sounds#)
selectObject: sounds# [i]
To Pitch (filtered ac): 0.0, 50, 800, 15, “no”, 0.03, 0.09, 0.50, 0.055, 0.35, 0.14
f0 = Get quantile: 0, 0, 0.50, “Hertz”
appendInfoLine: f0
Remove
endfor
# Restore selection:
selectObject (sounds#)

Links to this page


© Paul Boersma 1999,2004,2006–2008,2011,2013,2014,2018,2023