Debian Codegraph @

docs/manual/Scripting_6_1__Arguments_to_the_script.html

raw
1 <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2 <title>Scripting 6.1. Arguments to the script</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 6.1. Arguments to the script
13 </b></font></table></table>
14 <p>You can cause a Praat script to prompt for arguments. The file <code><font size=+1>playSine.praat</font></code> may contain the following:</p>
15 <code> <b>form</b>: “Play a sine wave”<br></code>
16 <code> <b>positive</b>: “Sine frequency (Hz)”, “377.0”<br></code>
17 <code> <b>positive</b>: “Gain (0..1)\", “0.3 (= not too loud)”<br></code>
18 <code> <b>endform</b><br></code>
19 <code> Create Sound as pure tone: “sine” + string$ (sine_frequency),<br></code>
20 <code> ... 1, 0, 1, 44100, sine_frequency, gain, 0.01, 0.01<br></code>
21 <code> Play<br></code>
22 <code> Remove<br></code>
23 <p>When running this script, the interpreter puts a settings window (<i>form</i>) on your screen, entitled &#8220;Play a sine wave&#8221;, with two fields, titled &#8220;Sine frequency (Hz)&#8221; and &#8220;Gain&#8221;, that have been provided with the standard values &#8220;377.0&#8221; and &#8220;0.3 (= not too loud)&#8221;, which you can change before clicking <b>OK</b>.</p>
24 <p>Inside the script, the field names can be accessed as variables: these have underscores instead of spaces, and the parentheses (Hz) have been chopped off. Note that the first letter of these variables is converted to lower case, so that you can assign to them in your script.</p>
25 <p>Inside the script, the value &#8220;0.3 (= not too loud)&#8221; will be known as 0.3, because this is a numeric field.</p>
26 <p>You can use the following field types in your forms:</p>
27 <dl>
28 <dt><b><code><font size=+1>real</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
29 <dd>for real numbers.</dd>
30 <dt><b><code><font size=+1>positive</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
31 <dd>for positive real numbers: the form issues an error message if the number that you enter is negative or zero (further on in the script, you can freely change it to any real number, including negatives ones such as -1.5, or zero).</dd>
32 <dt><b><code><font size=+1>integer</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
33 <dd>for whole numbers: the form reads the number as an integer (further on in the script, you can give it any real value, e.g. 3.14).</dd>
34 <dt><b><code><font size=+1>natural</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
35 <dd>for positive whole numbers: the form issues an error message if the number that you enter is negative or zero (further on in the script, you can give it any real value).</dd>
36 <dt><b><code><font size=+1>word</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
37 <dd>for a string without spaces: the form only reads up to the first space (&#8220;oh yes&#8221; becomes &#8220;oh&#8221;; further on in the script, you can give the string any value, perhaps with spaces in it).</dd>
38 <dt><b><code><font size=+1>sentence</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
39 <dd>for any short string.</dd>
40 <dt><b><code><font size=+1>text</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
41 <dd>for any possibly long string (the variable name will show up above the field).</dd>
42 <dt><b><code><font size=+1>text</font></code></b>: <i><code><font size=+1>numberOfLines</font></code></i>, <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
43 <dd>for any possibly very long string (multiple lines), <i><code><font size=+1>numberOfLines</font></code></i> can be between 1 and 33.</dd>
44 <dt><b><code><font size=+1>boolean</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue</font></code></i>
45 <dd>a check box will be shown; the initial value is 1 (on) or 0 (off).</dd>
46 <dt><b><code><font size=+1>boolean</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
47 <dd>a check box will be shown; to switch it on, set the initial value to <code><font size=+1>“on”</font></code>, <code><font size=+1>“yes”</font></code>, <code><font size=+1>“ON”</font></code>, <code><font size=+1>“YES”</font></code>, <code><font size=+1>“On”</font></code> or <code><font size=+1>“Yes”</font></code>; to switch it off, set it to <code><font size=+1>“off”</font></code>, <code><font size=+1>“no”</font></code>, <code><font size=+1>“OFF”</font></code>, <code><font size=+1>“NO”</font></code>, <code><font size=+1>“Off”</font></code> or <code><font size=+1>“No”</font></code>.</dd>
48 <dt><b><code><font size=+1>choice</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue</font></code></i>
49 <dd>a multiple-choice box (or &#8220;radio box&#8221;) will be shown; the value is 1 or higher. This is followed by a series of:</dd>
50 <dt><b><code><font size=+1>option</font></code></b>: <i><code><font size=+1>text$</font></code></i>
51 <dd>an option button in a multiple-choice box (see example below).</dd>
52 <dt><b><code><font size=+1>optionmenu</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue</font></code></i>
53 <dd>a multiple-choice menu header will be shown; the value is 1 or higher. Just as <code><font size=+1>choice</font></code>, this is followed by:</dd>
54 <dt><b><code><font size=+1>option</font></code></b>: <i><code><font size=+1>text$</font></code></i>
55 <dd>an option in a multiple-choice menu (see example below).</dd>
56 <dt><b><code><font size=+1>comment</font></code></b>: <i><code><font size=+1>text$</font></code></i>
57 <dd>a line with any text.</dd>
58 <dt><b><code><font size=+1>infile</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
59 <dd>for a full path to an existing file, usually for reading.</dd>
60 <dt><b><code><font size=+1>outfile</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
61 <dd>for a full path to a new file, usually for saving.</dd>
62 <dt><b><code><font size=+1>folder</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
63 <dd>for a full path to a folder.</dd>
64 <dt><b><code><font size=+1>realvector</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
65 <dd>for a vector with real values. The format can be <code><font size=+1>“(whitespace-separated)”</font></code> or <code><font size=+1>“(formula)”</font></code>; the initial value should then probably be something like <code><font size=+1>“10 -9 80”</font></code> or <code><font size=+1>“{ 10, -9, 80 }”</font></code>, respectively.</dd>
66 <dt><b><code><font size=+1>realvector</font></code></b>: <i><code><font size=+1>numberOfLines</font></code></i>, <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
67 <dd>use this if you want a field with less or more than the standard 7 lines.</dd>
68 </dl>
69 <p><b><code><font size=+1>positivevector</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i></p>
70 <dl>
71 <dt><b><code><font size=+1>positivevector</font></code></b>: <i><code><font size=+1>numberOfLines</font></code></i>, <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
72 <dd>use either of these if you want to check that all initial elements are positive real numbers.</dd>
73 </dl>
74 <p><b><code><font size=+1>integervector</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i></p>
75 <dl>
76 <dt><b><code><font size=+1>integervector</font></code></b>: <i><code><font size=+1>numberOfLines</font></code></i>, <i>variable </i>, <i>format<code><font size=+1></font></code> </i>, <i>initialValue<code><font size=+1></font></code> </i>
77 <dd>use either of these if you want to check that all initial elements are whole numbers.</dd>
78 </dl>
79 <p><b><code><font size=+1>naturalvector</font></code></b>: <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i></p>
80 <dl>
81 <dt><b><code><font size=+1>naturalvector</font></code></b>: <i><code><font size=+1>numberOfLines</font></code></i>, <i><code><font size=+1>variable$</font></code></i>, <i><code><font size=+1>format$</font></code></i>, <i><code><font size=+1>initialValue$</font></code></i>
82 <dd>use either of these if you want to check that all initial elements are positive whole numbers.</dd>
83 </dl>
84 <p>Inside the script, strings are known as string variables, numbers as numeric variables. Consider the following form:</p>
85 <code> <b>form</b>: “Sink it”<br></code>
86 <code> <b>sentence</b>: “Name of the ship”, “Titanic”<br></code>
87 <code> <b>real</b>: “Distance to the iceberg (m)”, “500.0”<br></code>
88 <code> <b>natural</b>: “Number of people”, “1800”<br></code>
89 <code> <b>natural</b>: “Number of boats”, “10”<br></code>
90 <code> <b>endform</b><br></code>
91 <p>In the script following this form, the variables will be known as <code><font size=+1>name_of_the_ship$</font></code>, <code><font size=+1>distance_to_the_iceberg</font></code>, <code><font size=+1>number_of_people</font></code>, and <code><font size=+1>number_of_boats</font></code>.</p>
92 <p>The variable associated with a radio box will get a numeric as well as a string value:</p>
93 <code> <b>form</b>: “Fill attributes”<br></code>
94 <code> <b>comment</b>: “Choose any colour and texture for your paintings”<br></code>
95 <code> <b>choice</b>: “Colour”, 5<br></code>
96 <code> <b>option</b>: “Dark red”<br></code>
97 <code> <b>option</b>: “Sea green”<br></code>
98 <code> <b>option</b>: “Navy blue”<br></code>
99 <code> <b>option</b>: “Canary yellow”<br></code>
100 <code> <b>option</b>: “Black”<br></code>
101 <code> <b>option</b>: “White”<br></code>
102 <code> <b>choice</b>: \"Texture”, 1<br></code>
103 <code> <b>option</b>: “Smooth”<br></code>
104 <code> <b>option</b>: “Rough”<br></code>
105 <code> <b>option</b>: “With holes”<br></code>
106 <code> <b>endform</b><br></code>
107 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “You chose the colour ”, colour$, “ and the texture ”, texture$, “.”<br></code>
108 <p>This shows two multiple-choice boxes. In the Colour box, the fifth button (Black) is the standard value here. If you click &#8220;Navy blue&#8221; and then <b><i>OK</i></b>, the variable <i><code><font size=+1>colour</font></code></i> will have the value 3, and the variable <i><code><font size=+1>colour$</font></code></i> will have the value &#8220;Navy blue&#8221;. So you can test the value of the Colour box in either of the following ways:</p>
109 <code> if colour = 4<br></code>
110 <p>or</p>
111 <code> if colour$ = “Canary yellow\"<br></code>
112 <p>The field type <b><code><font size=+1>optionmenu</font></code></b> is completely analogous to <b><code><font size=+1>choice</font></code></b>, but uses up much less space on the screen:</p>
113 <code> <b>form</b>: “Fill attributes”<br></code>
114 <code> <b>comment</b>: “Choose any colour and texture for your paintings”<br></code>
115 <code> <b>optionmenu</b>: “Colour”, 5<br></code>
116 <code> <b>option</b>: “Dark red”<br></code>
117 <code> <b>option</b>: “Sea green”<br></code>
118 <code> <b>option</b>: “Navy blue”<br></code>
119 <code> <b>option</b>: “Canary yellow”<br></code>
120 <code> <b>option</b>: “Black”<br></code>
121 <code> <b>option</b>: “White”<br></code>
122 <code> <b>optionmenu</b>: “Texture”, 1<br></code>
123 <code> <b>option</b>: “Smooth”<br></code>
124 <code> <b>option</b>: “Rough”<br></code>
125 <code> <b>option</b>: “With holes”<br></code>
126 <code> <b>endform</b><br></code>
127 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “You chose the colour ”, colour$, “ and the texture ”, texture$, “.”<br></code>
128 <p>For the vector types <b><code><font size=+1>realvector</font></code></b>, <b><code><font size=+1>positivevector</font></code></b>, <b><code><font size=+1>integervector</font></code></b> and <b><code><font size=+1>naturalvector</font></code></b> you have to specify an initial format, which will be shown to the user:</p>
129 <code> <b>form</b>: “Interesting times”<br></code>
130 <code> <b>comment</b>: “List the times that you are interested in”<br></code>
131 <code> <b>realvector</b>: “Fixed times (s)”, “(whitespace-separated)”, “0.5 1.7 2.8”<br></code>
132 <code> <b>realvector</b>: “Random times (s)”, “(formula)”, “randomUniform# (5, 0.0, 1.0)”<br></code>
133 <code> <b>endform</b><br></code>
134 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “Your fixed times are ”, fixed_times#, “ and your random times are ”, random_times#, “.”<br></code>
135 <p>Clicking OK without editing the two fields may print</p>
136 <code> Your fixed times are 0.5 1.7 2.8 and your random times are 0.754675 0.121393653 0.39856 0.8376572 0.387537.<br></code>
137 <p>The field types <b>infile</b>, <b>outfile</b> and <b>folder</b> always yield a full path. Consider the script <code><font size=+1>playFile.praat</font></code>, which contains the following:</p>
138 <code> <b>form</b>: “Play file”<br></code>
139 <code> <b>infile</b>: “File to play”, “hello.wav”<br></code>
140 <code> <b>endform</b><br></code>
141 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “You chose the file ”, file_to_play$, “.”<br></code>
142 <code> Read from file: file_to_play$<br></code>
143 <code> Play<br></code>
144 <code> Remove<br></code>
145 <p>If you just click OK and <code><font size=+1>playFile.praat</font></code> is in the folder <code><font size=+1>/Users/miep/research/usefulScripts</font></code>, then this will print</p>
146 <code> You chose the file /Users/miep/research/usefulScripts/hello.wav.<br></code>
147 <p>into the Info window, and play the sound in that file.</p>
148 <p>You can combine two short fields into one by using <b><code><font size=+1>left</font></code></b> and <b><code><font size=+1>right</font></code></b>:</p>
149 <code> <b>form</b>: “Get duration”<br></code>
150 <code> <b>natural</b>: “left Year range”, “1940”<br></code>
151 <code> <b>natural</b>: “right Year range”, “1945”<br></code>
152 <code> <b>endform</b><br></code>
153 <code> duration = right_Year_range - left_Year_range<br></code>
154 <code> <a href="_writeInfoLine_.html">writeInfoLine</a>: “The duration is ”, duration, “ years.”<br></code>
155 <p>The interpreter will only show the single text &#8220;Year range&#8221;, followed by two small text fields.</p>
156 <h2>Calling a script from another script</h2>
157 <p>Scripts can be nested: the file <code><font size=+1>doremi.praat</font></code> may contain the following:</p>
158 <code> <b>runScript</b>: “playSine.praat”, 550, 0.9<br></code>
159 <code> <b>runScript</b>: “playSine.praat”, 615, 0.9<br></code>
160 <code> <b>runScript</b>: “playSine.praat”, 687, 0.9<br></code>
161 <p>With <b><code><font size=+1>runScript</font></code></b>, Praat will not display a form window, but simply execute the script with the two arguments that you supply on the same line (e.g. 550 and 0.9).</p>
162 <p>Values for <b><code><font size=+1>choice</font></code></b> must be passed as strings:</p>
163 <code> <b>runScript</b>: “fill attributes.praat”, “Navy blue”, “With holes”<br></code>
164 <p>Values for vectors can be passed either as a vector expression or as a white-space separated string, independently of the initial format:</p>
165 <code> <b>runScript</b>: “interesting times.praat”, { 0.3, 0.5, 0.7, 2.0 }, “18000 0.3”<br></code>
166 <p>You can pass values for <b>boolean</b> either as the quoted strings &#8220;yes&#8221; and &#8220;no&#8221; (or their variants) or as the unquoted numbers 1 and 0.</p>
167 <p>In <b><code><font size=+1>runScript</font></code></b>, the path to the external script, as well as the paths to <b><code><font size=+1>infile</font></code></b>, <b><code><font size=+1>outfile</font></code></b> and <b><code><font size=+1>folder</font></code></b> parameters are taken relative to the folder of the current script. For instance, suppose that the current script is <code><font size=+1>/Users/miep/research/project19/analyse.praat</font></code> and contains:</p>
168 <code> <b>runScript</b>: “../usefulScripts/playFile.praat”, “sounds/sound3.wav”<br></code>
169 <p>then running the current script will run the above-mentioned script <code><font size=+1>/Users/miep/research/usefulScripts/playFile.praat</font></code>, which will play the file <code><font size=+1>/Users/miep/research/project19/sounds/sound3.wav</font></code>.</p>
170 <h3>Links to this page</h3>
171 <ul>
172 <li><a href="_runScript_.html"><code><font size=+1>runScript</font></code></a>
173 <li><a href="Scripting.html">Scripting</a>
174 <li><a href="Scripting_6__Communication_outside_the_script.html">Scripting 6. Communication outside the script</a>
175 <li><a href="Scripting_6_6__Controlling_the_user.html">Scripting 6.6. Controlling the user</a>
176 <li><a href="Scripting_6_9__Calling_from_the_command_line.html">Scripting 6.9. Calling from the command line</a>
177 </ul>
178 <hr>
179 <address>
180 <p>© Paul Boersma xx,2023,2024</p>
181 </address>
182 </body>
183 </html>