| 1 |
<html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 |
<title>Scripting 4.3. Querying objects</title> |
| 3 |
<style> |
| 4 |
td { padding-left: 5pt; padding-right: 5pt; } |
| 5 |
th { padding-left: 5pt; padding-right: 5pt; } |
| 6 |
code { white-space: pre-wrap; } |
| 7 |
dd { white-space: pre-wrap; } |
| 8 |
</style> |
| 9 |
</head><body bgcolor="#FFFFFF"> |
| 10 |
|
| 11 |
<table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 |
Scripting 4.3. Querying objects |
| 13 |
</b></font></table></table> |
| 14 |
<p>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 <i><code><font size=+1>name$</font></code></i>:</p> |
| 15 |
<code> name$ = <b><a href="_selected-S_.html">selected$</a></b> (“Sound”, 2)<br></code> |
| 16 |
<p>If the Sound was called “Sound hallo”, the variable <i><code><font size=+1>name$</font></code></i> will contain the string “hallo”. To get the name of the topmost selected Sound object, you can leave out the number:</p> |
| 17 |
<code> name$ = selected$ (“Sound”)<br></code> |
| 18 |
<p>To get the full name (type + name) of the third selected object, you do:</p> |
| 19 |
<code> fullName$ = selected$ (3)<br></code> |
| 20 |
<p>To get the full name of the topmost selected object, you do:</p> |
| 21 |
<code> fullName$ = selected$ ()<br></code> |
| 22 |
<p>To get the type and name out of the full name, you do:</p> |
| 23 |
<code> type$ = <a href="_extractWord-S_.html">extractWord$</a> (fullName$, “”)<br></code> |
| 24 |
<code> name$ = <a href="_extractLine-S_.html">extractLine$</a> (fullName$, “ ”)<br></code> |
| 25 |
<p>Negative numbers count from the bottom. Thus, to get the name of the bottom-most selected Sound object, you say</p> |
| 26 |
<code> name$ = selected$ (“Sound”, -1)<br></code> |
| 27 |
<p>You would use <code><font size=+1>selected$</font></code> () for drawing the object name in a picture:</p> |
| 28 |
<code> Draw: 0, 0, 0, 0, “yes”<br></code> |
| 29 |
<code> name$ = selected$ (“Sound”)<br></code> |
| 30 |
<code> Text top: “no”, “This is sound ” + name$<br></code> |
| 31 |
<p>For identifying previously selected objects, this method is not very suitable, since there may be multiple objects with the same name:</p> |
| 32 |
<code> # The following two lines are OK:<br></code> |
| 33 |
<code> soundName$ = selected$ (“Sound”, -1)<br></code> |
| 34 |
<code> pitchName$ = selected$ (“Pitch”)<br></code> |
| 35 |
<code> # But the following line is questionable, since it doesn’t<br></code> |
| 36 |
<code> # necessarily select the previously selected Pitch again:<br></code> |
| 37 |
<code> selectObject: “Pitch ” + pitchName$<br></code> |
| 38 |
<p>Instead of this error-prone approach, you should get the object’s unique ID. The correct version of our example becomes:</p> |
| 39 |
<code> sound = selected (“Sound”, -1)<br></code> |
| 40 |
<code> pitch = selected (“Pitch”)<br></code> |
| 41 |
<code> # Correct:<br></code> |
| 42 |
<code> selectObject: pitch<br></code> |
| 43 |
<p>To get the number of selected Sound objects into a variable, use</p> |
| 44 |
<code> numberOfSelectedSounds = numberOfSelected (“Sound”)<br></code> |
| 45 |
<p>To get the number of selected objects into a variable, use</p> |
| 46 |
<code> numberOfSelectedObjects = numberOfSelected ()<br></code> |
| 47 |
<h2>Example: doing something to every selected Sound</h2> |
| 48 |
<code> sounds# = <b><a href="_selected-H_.html">selected#</a></b> (“Sound”)<br></code> |
| 49 |
<code> # Median pitches of all selected sounds:<br></code> |
| 50 |
<code> for i to size (sounds#)<br></code> |
| 51 |
<code> selectObject: sounds# [i]<br></code> |
| 52 |
<code> To Pitch (filtered ac): 0.0, 50, 800, 15, “no”, 0.03, 0.09, 0.50, 0.055, 0.35, 0.14<br></code> |
| 53 |
<code> f0 = Get quantile: 0, 0, 0.50, “Hertz”<br></code> |
| 54 |
<code> appendInfoLine: f0<br></code> |
| 55 |
<code> Remove<br></code> |
| 56 |
<code> endfor<br></code> |
| 57 |
<code> # Restore selection:<br></code> |
| 58 |
<code> selectObject (sounds#)<br></code> |
| 59 |
<h3>Links to this page</h3> |
| 60 |
<ul> |
| 61 |
<li><a href="_selected_.html"><code><font size=+1>selected</font></code></a> |
| 62 |
<li><a href="Scripting.html">Scripting</a> |
| 63 |
<li><a href="Scripting_4__Object_selection.html">Scripting 4. Object selection</a> |
| 64 |
<li><a href="What_was_new_in_6_1_.html">What was new in 6.1?</a> |
| 65 |
</ul> |
| 66 |
<hr> |
| 67 |
<address> |
| 68 |
<p>© Paul Boersma 1999,2004,2006–2008,2011,2013,2014,2018,2023</p> |
| 69 |
</address> |
| 70 |
</body> |
| 71 |
</html> |