| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 4.1. Selecting 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.1. Selecting objects |
| 13 | </b></font></table></table> |
| 14 | <p>To simulate the mouse-clicked and dragged selection in the list of objects, you have the functions <a href="_selectObject_.html"><b><code><font size=+1>selectObject</font></code></b></a>, <a href="_plusObject_.html"><b><code><font size=+1>plusObject</font></code></b></a> and <a href="_minusObject_.html"><b><code><font size=+1>minusObject</font></code></b></a>.</p> |
| 15 | <p>Suppose you start Praat and use <b>Create Sound as tone...</b> to create a Sound called <i>tone</i>. In the object list it looks like “1. Sound tone”. Suppose you then do <b>To Spectrum...</b> from the <b>Analyse Spectrum</b> 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</p> |
| 16 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: 1<br></code> |
| 17 | <code> Play<br></code> |
| 18 | <p>or</p> |
| 19 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: “Sound tone”<br></code> |
| 20 | <code> Play<br></code> |
| 21 | <p>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.</p> |
| 22 | <p>The function <a href="_selectObject_.html"><b><code><font size=+1>selectObject</font></code></b></a> 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 <a href="_plusObject_.html"><b><code><font size=+1>plusObject</font></code></b></a> or <a href="_minusObject_.html"><b><code><font size=+1>minusObject</font></code></b></a>. When the Sound is selected, you can select the Spectrum as well by doing</p> |
| 23 | <code> <b><a href="_plusObject_.html">plusObject</a></b>: 2<br></code> |
| 24 | <p>or</p> |
| 25 | <code> <b><a href="_plusObject_.html">plusObject</a></b>: “Spectrum tone”<br></code> |
| 26 | <p>If you then want to deselect the Sound, and keep the Spectrum selected, you can do</p> |
| 27 | <code> <b><a href="_minusObject_.html">minusObject</a></b>: 1<br></code> |
| 28 | <p>or</p> |
| 29 | <code> <b><a href="_minusObject_.html">minusObject</a></b>: “Sound tone”<br></code> |
| 30 | <p>All these functions can take more than one argument. To select the Sound and the Spectrum together, you can do</p> |
| 31 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: 1, 2<br></code> |
| 32 | <p>or</p> |
| 33 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: “Sound tone”, “Spectrum tone”<br></code> |
| 34 | <p>or even</p> |
| 35 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: 1, “Spectrum tone”<br></code> |
| 36 | <p>or, using a numeric vector:</p> |
| 37 | <code> myObjects# = { 1, 2 }<br></code> |
| 38 | <code> <b><a href="_selectObject_.html">selectObject</a></b>: myObjects#<br></code> |
| 39 | <h2>How to refer to objects created in your script</h2> |
| 40 | <p>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:</p> |
| 41 | <code> sound = Create Sound as pure tone: “sine377”,<br></code> |
| 42 | <code> ... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound<br></code> |
| 43 | <code> Play ; the Sound is selected, so it plays<br></code> |
| 44 | <code> To Spectrum: “yes”<br></code> |
| 45 | <code> Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn<br></code> |
| 46 | <code> # Remove the created Spectrum and Sound:<br></code> |
| 47 | <code> <b><a href="_plusObject_.html">plusObject</a></b>: sound ; the Spectrum was already selected<br></code> |
| 48 | <code> Remove<br></code> |
| 49 | <p>You could also select the objects by name:</p> |
| 50 | <code> Create Sound as pure tone: “sine377”,<br></code> |
| 51 | <code> ... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; no need to remember the ID of the Sound<br></code> |
| 52 | <code> Play ; the Sound is selected, so it plays<br></code> |
| 53 | <code> To Spectrum: “yes”<br></code> |
| 54 | <code> Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn<br></code> |
| 55 | <code> # Remove the created Spectrum and Sound:<br></code> |
| 56 | <code> <b><a href="_plusObject_.html">plusObject</a></b>: “Sound sine377” ; the Spectrum was already selected<br></code> |
| 57 | <code> Remove<br></code> |
| 58 | <p>This works even if there are multiple objects called “Sound sine377”, because if there are more objects with the same name, <a href="_selectObject_.html"><b><code><font size=+1>selectObject</font></code></b></a> and <a href="_plusObject_.html"><b><code><font size=+1>plusObject</font></code></b></a> select the most recently created one, i.e., the one nearest to the bottom of the list of objects.</p> |
| 59 | <h3>Links to this page</h3> |
| 60 | <ul> |
| 61 | <li><a href="Scripting.html">Scripting</a> |
| 62 | <li><a href="Scripting_4__Object_selection.html">Scripting 4. Object selection</a> |
| 63 | <li><a href="Scripting_4_2__Removing_objects.html">Scripting 4.2. Removing objects</a> |
| 64 | </ul> |
| 65 | <hr> |
| 66 | <address> |
| 67 | <p>© ppgb 20180428</p> |
| 68 | </address> |
| 69 | </body> |
| 70 | </html> |