Debian Codegraph @

fon/manual_scripting.cpp

raw symbols SCIP
1 /* manual_scripting.cpp
2 *
3 * Copyright (C) 1992-2025 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "ManPagesM.h"cxx . . $ `<file>/sys/ManPagesM.h`/
20
21 void manual_scripting_initcxx . . $ manual_scripting_init(e57bd08652fa63a5). (ManPagescxx . . $ ManPages# me);
22 void manual_scripting_initcxx . . $ manual_scripting_init(e57bd08652fa63a5). (ManPagescxx . . $ ManPages# me) {
23
24 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Paste history", U"ppgb", 20050822)
25 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the #Edit menu of a @ScriptEditor, for inserting the history of commands. "
26 "See @@History mechanism@.")
27 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
28
29 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Clear history", U"ppgb", 20000927)
30 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the Edit menu of the @ScriptEditor for clearing the remembered history. "
31 "See @@History mechanism@.")
32 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
33
34 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"History mechanism", U"ppgb", 20040414)
35 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"The easiest way to do @@scripting@. "
36 "The %history is the sequence of all menu commands "
37 "(in the Objects or Picture window or in the editors), "
38 "action commands (in the dynamic menu), "
39 "or mouse clicks on objects (in the list of objects), that you performed during your Praat session, "
40 "together with the settings that you specified in the settings windows "
41 "that popped up as a result of those commands.")
42 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Viewing the history")
43 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To view your history, you first open a @ScriptEditor with @@New Praat script@ or @@Open Praat script...@. "
44 "You then choose @@Paste history@ from the #Edit menu.")
45 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Recording a macro")
46 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To record a sequence of mouse clicks for later re-use, "
47 "perform the following steps:")
48 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. Choose @@Clear history@ from the #Edit menu. "
49 "This makes the history mechanism forget all previous clicks.")
50 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. Perform the actions that you want to record.")
51 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. Choose @@Paste history@ from the #Edit menu. Because you cleared the history "
52 "before you started, the resulting script contains only the actions "
53 "that you performed in step 2. "
54 "You can now already re-run the actions that you performed in step 2.")
55 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"4. You can save the recorded actions to a script file by choosing #Save from the #File menu.")
56 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"5. You can put this script file under a button in the @@dynamic menu@ "
57 "by choosing @@Add to dynamic menu...@ from the File menu, "
58 "or under a button in a fixed menu by choosing @@Add to fixed menu...@. "
59 "This button will be preserved across Praat sessions.")
60 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This macro mechanism is much more flexible than the usual opaque macro mechanism "
61 "used by most programs, because you can edit the script and make some "
62 "of the arguments variable by putting them in the #form clause at the top of the script. "
63 "In this way, the script will prompt the user for these arguments, "
64 "just as with all the menu and action commands that end in the three dots (...). "
65 "See the @Scripting tutorial for all the things that you can do in scripts.")
66 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
67
68 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"New Praat script", U"ppgb", 20050822)
69 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the @@Praat menu@ for creating a new Praat script. "
70 "It creates a @ScriptEditor with an empty script that you can edit, run, and save.")
71 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
72
73 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"New Praat notebook", U"ppgb", 20230325)
74 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the @@Praat menu@ for creating a new Praat notebook. "
75 "It creates a @NotebookEditor with an empty notebook that you can edit, run, and save.")
76 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
77
78 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Open Praat script...", U"ppgb", 20050822)
79 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the @@Praat menu@ for editing an existing @@Praat script@. "
80 "It creates a @ScriptEditor and asks "
81 "you to select a file. If you click #%OK, the file is read into the ScriptEditor window, "
82 "and you can run and edit it; if you click #%Cancel, you get an empty script, as with @@New Praat script@.")
83 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
84
85 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Open Praat notebook...", U"ppgb", 20230325)
86 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"A command in the @@Praat menu@ for editing an existing @@Praat notebook@. "
87 "It creates a @NotebookEditor and asks "
88 "you to select a file. If you click #%OK, the file is read into the NotebookEditor window, "
89 "and you can run and edit it; if you click #%Cancel, you get an empty notebook, as with @@New Praat notebook@.")
90 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
91
92 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Praat script", U"ppgb", 20230325)
93 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"An executable program text in the Praat scripting language, which consists of menu commands, action commands, computations and control flow.")
94 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"See the @Scripting tutorial.")
95 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
96
97 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Praat notebook", U"ppgb", 20230325)
98 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U" A text document with which you can create things that look like Praat’s manual pages, "
99 "with capabilities of graphics and @scripting.")
100 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
101
102 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"NotebookEditor", U"ppgb", 20230325)
103 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"An aid to documented @@scripting@ as well as to creating manual pages.")
104 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
105
106 MAN_PAGES_BEGINcxx . . $ `sys/ManPagesM.h:27:9`!
107 R"~~~(
108 ################################################################################
109 "Scripting"
110 © Paul Boersma 2000,2002–2004,2008,2010,2011,2013,2014,2017,2020,2024
111
112 This is one of the tutorials of the Praat program. It assumes you are familiar with the @Intro.
113
114 A %script is a text that consists of menu commands and action commands.
115 If you %run the script (perhaps from a @ScriptEditor),
116 the commands are executed as if you clicked on them.
117 You can read this tutorial sequentially with the help of the “##< 1#” and “##1 >#” buttons.
118
119 , @@Scripting 1. Your first scripts@ (how to create, how to run, how to save)
120 , @@Scripting 2. How to script settings windows@ (numeric, boolean, multiple-choice, text, file)
121 , @@Scripting 3. Simple language elements
122 , @@Scripting 3.1. Hello world@ (`writeInfoLine`, `appendInfoLine`)
123 , @@Scripting 3.2. Numeric variables@ (assignments)
124 , @@Scripting 3.3. Numeric queries
125 , @@Scripting 3.4. String variables@ (assignments)
126 , @@Scripting 3.5. String queries
127 , @@Scripting 3.6. “For” loops@ (`for`, `endfor`)
128 , @@Scripting 3.7. Layout@ (white space, comments, continuation lines)
129 , @@Scripting 4. Object selection@
130 , @@Scripting 4.1. Selecting objects
131 , @@Scripting 4.2. Removing objects
132 , @@Scripting 4.3. Querying objects
133 , @@Scripting 5. Language elements reference@
134 , @@Scripting 5.1. Variables@ (numeric, string)
135 , @@Scripting 5.2. Expressions@ (numeric, string)
136 , @@Scripting 5.3. Jumps@ (`if`, `then`, `elsif`, `else`, `endif`)
137 , @@Scripting 5.4. Loops@ (`for`/`endfor`, `while`/`endwhile`, `repeat`/`until`)
138 , @@Scripting 5.5. Procedures@ (`@`, `procedure`)
139 , @@Scripting 5.6. Arrays and dictionaries
140 , @@Scripting 5.7. Vectors and matrices
141 , @@Scripting 5.8. Including other scripts
142 , @@Scripting 5.9. Quitting@ (`exitScript`)
143 , @@Scripting 6. Communication outside the script
144 , @@Scripting 6.1. Arguments to the script@ (`form`/`endform`, `runScript`)
145 , @@Scripting 6.2. Writing to the Info window@ (`writeInfoLine`, `appendInfoLine`, `appendInfo`, `tab$`)
146 , @@Scripting 6.3. Query commands@ (#Get, #Count)
147 , @@Scripting 6.4. Files@ (`fileReadable`, `readFile`, `writeFile`, `deleteFile`, `createFolder`)
148 , @@Scripting 6.5. Calling system commands@ (`runSystem`, `environment$`, `stopwatch`)
149 , @@Scripting 6.6. Controlling the user@ (`pause`, `beginPause`/`endPause`, `chooseReadFile$`)
150 , @@Scripting 6.7. Sending a message to another program@
151 , @@Scripting 6.8. Messages to the user@ (`exitScript`, `assert`, `nowarn`, `nocheck`)
152 , @@Scripting 6.9. Calling from the command line
153 , @@Scripting 7. Scripting the editors
154 , @@Scripting 7.1. Scripting an editor from a shell script@ (`editor`/`endeditor`)
155 , @@Scripting 7.2. Scripting an editor from within
156 , @@Scripting 8. Controlling Praat from another program
157 , @@Scripting 8.1. The sendpraat subroutine
158 , @@Scripting 8.2. The sendpraat program
159 , @@Scripting 9. Turning a script into a stand-alone program
160 , @@Scripting 10. Old functions
161
162 Also see the @@scripting examples@.
163
164 ################################################################################
165 "Scripting 1. Your first scripts"
166 © Paul Boersma 2000,2002,2004,2005,2008(“My first script”),2011,2013,2014,2020,2023
167
168 This page tells you how to create, run and save a script.
169 To get a feel for how it works, you are advised to try out all the steps.
170
171 1. A minimal script
172 ===================
173 Suppose that you want to create a script that allows you to play a selected Sound object twice.
174 You first create an empty script, by choosing @@New Praat script@ from the Praat menu in the Objects window.
175 A @ScriptEditor window will appear on your screen:
176 {- 6x4
177 )~~~"
178 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (4, "untitled script", "File Edit Search Convert Font Run Help")
179 R"~~~(
180 Draw rectangle: 0, 560, 0, 360
181 info$ = Picture info
182 fontSize = extractNumber (info$, "Font size: ")
183 ;Text: 50, "centre", 50, "half", string$ (fontSize)
184 }
185 In this window, you type
186 {;
187 Play
188 Play
189 }
190 Now select a Sound in the Objects window. As you expect from selecting a Sound, a #Play button will
191 appear in the dynamic menu. If you now choose #Run from the Run menu in the ScriptEditor,
192 Praat will play the sound twice.
193 This works because #Play is a command that becomes available in the dynamic menu when you select a Sound.
194
195 2. Some more commands
196 =====================
197 In the above example, you could use $Play in your script because the #Play command was available,
198 i.e. because “Play” was the text on a button currently available in the dynamic menu.
199 Apart from these selection-dependent (dynamic) commands,
200 you can also use all fixed commands from the menus of the @@Objects window@
201 and the @@Picture window@. For instance, try the following script:
202 {;
203 Erase all
204 Draw inner box
205 Play
206 Play
207 Erase all
208 }
209 When you run this script, you’ll see a rectangle appear in the Picture window
210 (that’s what the command ##Draw inner box# in the #Margins menu does),
211 then you’ll hear the Sound play twice, then you’ll see the rectangle disappear from the Picture window
212 (that’s what the command ##Erase all# from the Edit menu does).
213
214 Here we see that the Praat scripting language is an example of a %%procedural programming language%,
215 which means that the five %statements are executed in the order in which they appear in the script,
216 i.e. first ##Erase all#, then ##Draw inner box#, then #Play twice, and finally ##Erase all#.
217
218 3. Experimenting with your script
219 =================================
220 You don't have to be afraid of making mistakes. Here are a couple that you can try to make.
221
222 First, try to run the script when a Sound is not selected
223 (e.g. you create a Pitch object from it and keep that selected, or you throw away the Sound).
224 You will notice that Praat gives you an error message saying
225 “The command “Play” is not available for the current selection”.
226 Indeed, if you select a Pitch or if you select nothing, then no command #Play appears in the dynamic menu,
227 so the script cannot execute it. Note that the commands ##Erase all# and ##Draw inner box# are still available,
228 because they continue to be present in the menus of the Picture window;
229 therefore, the script will execute the first two lines (`Erase all` and `Draw inner box`)
230 and stop running at the third line, i.e. at your first `Play`.
231 The result is that the “box” will stay visible in the Picture window, because the fifth line of the script,
232 which should erase the box, is never executed.
233
234 Second, try to mistype a command (there’s a good chance you already did it by accident),
235 e.g. write `PLay` instead of `Play`, or `Draw inner bocks` or whatever.
236 Again, you are likely to get a message saying that that command is not available.
237 Such messages are the most common messages that you’ll see when writing scripts;
238 now you know that they mean either that you mistyped something or that you made the wrong selection.
239
240 4. Saving your script
241 =====================
242 The File menu of the ScriptEditor has a command #Save,
243 with which you can save your script as a file on disk, for instance under the name “test.praat”.
244
245 Please try this with the five-line script you just typed.
246 After saving the script, the name of the script file will appear in the window title:
247 {- 6x4
248 )~~~"
249 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (4, "Script “/Users/Rose/Desktop/test.praat”", "File Edit Search Convert Font Run Help")
250 R"~~~(
251 Courier
252 Text: 0, “left”, 75, “half”, “\s{Erase all}”
253 Text: 0, “left”, 90, “half”, “\s{Draw inner box}”
254 Text: 0, “left”, 105, “half”, “\s{Play}”
255 Text: 0, “left”, 120, “half”, “\s{Play}”
256 Text: 0, “left”, 135, “half”, “\s{Erase all}”
257 Draw rectangle: 0, 560, 0, 360
258 }
259 After you save your script, you can close the ScriptEditor window without losing the script:
260 you can reopen the script file by using @@Open Praat script...@ from the Praat menu,
261 or by choosing ##New Praat script# again, followed by ##Open...# from the ScriptEditor’s File menu.
262
263 It advisable to use “.praat” as the extension for script file names.
264 On the Mac, if you double-click a “.praat” file, Praat will automatically start up and show the script.
265 On the Mac and on Windows, if you drag a “.praat” file on the Praat icon, Praat will also start up and show the script.
266
267 ################################################################################
268 "Scripting 2. How to script settings windows"
269 Paul Boersma 1999,2004,(“Arguments to commands”),2011,2013,2014,2017,2019,2020,2023
270
271 Not all menu commands are as simple as those on the @@Scripting 1. Your first scripts|previous page@,
272 which act immediately once you choose them from a menu (e.g. ##Play#, ##Erase all#).
273 Most commands in Praat require the user to supply additional information;
274 these are the commands whose title ends in “...”.
275
276 For instance, when you select a Sound, the command ##Draw...# will appear in the #Draw menu,
277 and when you click it, Praat will present you with a %%settings window%,
278 which asks you to supply six pieces of additional information, i.e. six so-called %settings (or in programming jargon: %arguments):
279
280 // 4 fields, of which 0 texts and 0 additional radio buttons,
281 // hence lines = 4 + 0 * 0.6 - 0 * 0.3 = 4.0,
282 // hence height = 1.4 + 4.0 * 0.4 = 3.0
283 {- 5.4x3.0,
284 )~~~"
285 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Sound: Draw", 4.0)
286 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Time range (s)", "0.0", "0.0 (= all)")
287 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Vertical range", "0.0", "0.0 (= auto)")
288 Manual_DRAW_SETTINGS_WINDOW_BOOLEANcxx . . $ `sys/ManPagesM.h:137:9`! ("Garnish", 1)
289 Manual_DRAW_SETTINGS_WINDOW_OPTIONMENUcxx . . $ `sys/ManPagesM.h:160:9`! ("Drawing method", "Curve")
290 R"~~~(
291 }
292 In this example, all the settings have their standard values: you want to draw the whole time domain of the Sound,
293 you want to have autoscaling vertically, you want to see garnishings around the picture (a box, labelled axes, and numbers),
294 and you want the waveform to be drawn as a curve. Pressing the OK button in the above window
295 is equivalent to executing the following script line:
296 {;
297 Draw: 0, 0, 0, 0, “yes”, “Curve”
298 }
299 You see that in a script, all of the arguments are supplied after the command, preceded by a colon and separated by commas,
300 in the same order as in the settings window, counted from top to bottom (and, within a line, from left to right).
301 The texts “(= all)” and “(= auto)” above are just Praat’s explanations of what it means to type a zero in those fields
302 (namely “draw all times” and “use vertical autoscaling”, respectively); in a script they are superfluous and you shouldn’t write them.
303
304 If you want to draw the sound with different settings, say from 1 to 3.2 seconds, scaled between -1 and +1 instead of automatically,
305 with garnishings off, and with the waveform drawn as poles, you would have the following settings window:
306
307 // 4 fields, of which 0 texts and 0 additional radio buttons,
308 // hence lines = 4 + 0 * 0.6 - 0 * 0.3 = 4.0,
309 // hence height = 1.4 + 4.0 * 0.4 = 3.0
310 {- 5.4x3.0
311 )~~~"
312 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Sound: Draw", 4.0)
313 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Time range (s)", "1.0", "3.2")
314 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Vertical range", "-1", "1")
315 Manual_DRAW_SETTINGS_WINDOW_BOOLEANcxx . . $ `sys/ManPagesM.h:137:9`! ("Garnish", 0)
316 Manual_DRAW_SETTINGS_WINDOW_OPTIONMENUcxx . . $ `sys/ManPagesM.h:160:9`! ("Drawing method", "Poles")
317 R"~~~(
318 }
319 In a script this would look like
320 {;
321 Draw: 1.0, 3.2, -1, 1, “no”, “Poles”
322 }
323 1. Numeric arguments
324 ====================
325 The first four arguments in the above examples are %%numeric arguments%: they are (real or integer) numbers.
326 You just write them in the script as you would write them into the settings window.
327
328 2. Boolean (yes/no) arguments
329 =============================
330 The fifth argument in the above examples (#Garnish) is a %%boolean argument% (yes/no choice) and is represented by a %%check button%.
331 In the script you write it as `"yes"` (including the quotes) or `"no"` (or as 1 or 0).
332
333 3. Multiple-choice arguments
334 ============================
335 The sixth argument in the above examples (##Drawing method#) is a %%multiple-choice argument% and is represented by an %%option menu%.
336 In the script you write the text of the choice, i.e. `"Curve"` or `"Poles"` in the examples.
337
338 A multiple-choice argument is sometimes represented by a %%choice box% instead of by an option menu.
339 For instance, the last example above could equally well have looked like
340
341 // 7 fields, of which 0 texts and 3 additional radio buttons,
342 // hence lines = 7 + 0 * 0.6 - 3 * 0.3 = 6.1,
343 // hence height = 1.4 + 6.1 * 0.4 = 3.84
344 {- 5.4x3.84,
345 )~~~"
346 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Sound: Draw", 6.1)
347 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Time range (s)", "1.0", "3.2")
348 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Vertical range", "-1", "1")
349 Manual_DRAW_SETTINGS_WINDOW_BOOLEANcxx . . $ `sys/ManPagesM.h:137:9`! ("Garnish", 0)
350 Manual_DRAW_SETTINGS_WINDOW_CHOICEcxx . . $ `sys/ManPagesM.h:147:9`! ("Drawing method", "Curve", 0)
351 "y -= 12\n"
352 Manual_DRAW_SETTINGS_WINDOW_CHOICEcxx . . $ `sys/ManPagesM.h:147:9`! ("", "Bars", 0)
353 "y -= 12\n"
354 Manual_DRAW_SETTINGS_WINDOW_CHOICEcxx . . $ `sys/ManPagesM.h:147:9`! ("", "Poles", 1)
355 "y -= 12\n"
356 Manual_DRAW_SETTINGS_WINDOW_CHOICEcxx . . $ `sys/ManPagesM.h:147:9`! ("", "Speckles", 0)
357 R"~~~(
358 }
359 In supplying arguments to a command in a script, there is no difference between an option menu and a radio box.
360 This last example will therefore again look like the following in a script:
361 {;
362 Draw: 1.0, 3.2, -1, 1, “no”, “Poles”
363 }
364 4. Text arguments
365 =================
366 Consider another frequently used menu command, namely ##Create Sound from formula...# in the #New menu:
367
368 // 6 fields, of which 1 text and 0 additional radio buttons,
369 // hence lines = 6 + 1 * 0.6 - 0 * 0.3 = 6.6,
370 // hence height = 1.4 + 6.6 * 0.4 = 4.04
371 {- 5.4x4.04,
372 )~~~"
373 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Create Sound from formula", 6.6)
374 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Name", "sine")
375 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Number of channels", "1")
376 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Start time (s)", "0.0")
377 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("End time (s)", "1.0")
378 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Sampling frequency (Hz)", "44100")
379 Manual_DRAW_SETTINGS_WINDOW_TEXTcxx . . $ `sys/ManPagesM.h:167:9`! ("Formula", "1/2 * sin(2*pi*377*x)")
380 R"~~~(
381 }
382 In a script this would look like:
383 {;
384 Create Sound from formula: “sine”, 1, 0.0, 1.0, 44100, “1/2 * sin(2*pi*377*x)”
385 }
386 Both the first argument (#Name) and the sixth argument (#Formula) are %%text arguments%.
387 In a script they are written within quotes.
388
389 5. Formula arguments
390 ====================
391 Sometimes it would be a bit awkward to write a formula that contains double quotes.
392 Imagine you have a @Table object with a column “stimulus”, a column “response”, and a column “correct”,
393 and you want to have a 1 in column “correct” if the texts in the columns “stimulus” and “response”
394 are the same, and a 0 otherwise. You would do this by selecting the Table and choosing the ##Formula...# command:
395
396 // 2 fields, of which 1 text and 0 additional radio buttons,
397 // hence lines = 2 + 1 * 0.6 - 0 * 0.3 = 2.6,
398 // hence height = 1.4 + 2.6 * 0.4 = 2.44
399 {- 5.4x2.44
400 )~~~"
401 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Table: Formula", 2.6) // 0.6 extra for the text
402 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Column (label)", "correct")
403 Manual_DRAW_SETTINGS_WINDOW_TEXTcxx . . $ `sys/ManPagesM.h:167:9`! ("Formula", "self\\$ [\\\" response\\\" ] = self\\$ [\\\" stimulus\\\" ]")
404 R"~~~(
405 }
406 According to section 4 above, and according to @@Formulas 2.2. Representation of strings@,
407 you would have to write this in the following way in a script:
408 {;
409 Formula: "correct", "self$ [""response""] = self$ [""stimulus""]"
410 }
411 The required doubling of string-internal double quotes is awkward.
412 Therefore, there exists a special way for typing formula arguments, namely with the tilde ("~"):
413 {;
414 Formula: "correct", ~ self$ ["response"] = self$ ["stimulus"]
415 }
416 This means that you can write the example of section 4 in an analogous way:
417 {;
418 Create Sound from formula: “sine”, 1, 0.0, 1.0, 44100, ~ 1/2 * sin(2*pi*377*x)
419 }
420 The tilde is probably the preferred way to write formula arguments.
421 You should remember, however, that the result is still a string (i.e. text),
422 and you can treat it as a string with the string methods described later on in this tutorial.
423
424 Another way is to use curly quotes, for instance:
425 {;
426 Formula: "correct", “self$ ["response"] = self$ ["stimulus"]”
427 }
428 or
429 {;
430 Formula: "correct", "self$ [“response”] = self$ [“stimulus”]"
431 }
432 6. File arguments
433 =================
434 The commands from the Open and Save menus, and several other commands whose names
435 start with #Read, #Open, or #Save, present a %%file selector window% instead of a typical Praat
436 settings window. File selector windows ask the user to supply a single argument: the file name.
437
438 In a script you can either supply the %%complete path% to the file, or supply a %%relative path%.
439
440 A complete path includes both the folder (directory) hierarchy and the name of the file.
441 This goes slightly differently on the Windows platform on the one hand,
442 and on the Mac and Linux platforms on the other. If your user name is Miep,
443 and your home folder contains a folder `Sounds`,
444 and this folder contains a folder `Animals`, and this contains the file `miauw.wav`,
445 you can open that file as follows:
446 {;
447 Read from file: “C:/Users/Miep/Sounds/Animals/miauw.wav” ; Windows
448 Read from file: “/Users/Miep/Sounds/Animals/miauw.wav” ; Mac
449 Read from file: “/home/miep/Sounds/Animals/miauw.wav” ; Linux
450 }
451 (the part before your user name may be slightly different on your computer;
452 use your command or terminal window to find out)
453
454 In these examples, “C” is the Windows %%drive letter% and
455 `/Users/Miep` or `/home/Miep` is your %%home folder%. Both the home folder and the drive letter
456 can be abbreviated away by using the tilde (“~”) in the path
457 (this tilde has nothing to do with the tilde used in formula arguments):
458 {;
459 Read from file: “~/Sounds/Animals/miauw.wav”
460 }
461 If your `Sounds` folder is not in your home folder but on your desktop, you do
462 {;
463 Read from file: “~/Desktop/Sounds/Animals/miauw.wav”
464 }
465 (this works because on all three platforms, the desktop folder is a subfolder of your home folder)
466
467 If your Sounds folder is on a USB drive called PORCH, it would be something like:
468 {;
469 Read from file: “G:/Sounds/Animals/miauw.wav” ; Windows
470 Read from file: “/Volumes/PORCH/Sounds/Animals/miauw.wav” ; Mac
471 Read from file: “/media/PORCH/Sounds/Animals/miauw.wav” ; Linux
472 }
473 Instead of all these complete path names, you can use %relative path names.
474 These are taken as relative to the folder in which your script resides,
475 and help to make your script portable if you move the script along with your data.
476
477 Thus, if your script (after you have saved it!) is in the `Animals` folder mentioned above,
478 i.e. in the same folder as `miauw.wav`, you would simply open the file with
479 {;
480 Read from file: “miauw.wav”
481 }
482 If your script is in the `Sounds` folder mentioned above,
483 i.e. in the same folder as where the `Animals` folder is, you would open the file with
484 {;
485 Read from file: “Animals/miauw.aifc”
486 }
487 If your script is in the folder `Scripts` that is inside the `Sounds` folder,
488 i.e. if your script is a sister folder of the `Animals` folder, you would open the file with
489 {;
490 Read from file: “../Animals/miauw.aifc”
491 }
492 where “..” is the general way on all platforms to go one folder up in the hierarchy.
493
494 Note that on Windows you could use the backslash (“\”) instead of the forward slash (“/”),
495 but with the forward slash your script will work on all three platforms.
496
497 7. Colour arguments
498 ===================
499 Several commands in the World and Pen menus of the Picture window,
500 as well as many object drawing commands, ask for a colour in the following way:
501
502 // 5 fields, of which 0 texts and 0 additional radio buttons,
503 // hence lines = 5 + 0 * 0.6 - 0 * 0.3 = 5.0,
504 // hence height = 1.4 + 5.0 * 0.4 = 3.4
505 {- 5.4x3.4
506 )~~~"
507 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Praat picture: Paint rectangle", 5.0)
508 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Colour (0-1, name, or {r,g,b})", "0.5")
509 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("From x", "0.0")
510 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("To x", "1.0")
511 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("From y", "0.0")
512 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("To y", "1.0")
513 R"~~~(
514 }
515 For the colour, you can supply, as text, one of the 16 explicit names that also appear in the Pen menu:
516 {
517 Paint rectangle: “maroon”, 0.0, 1.0, 0.0, 1.0
518 }
519 Alternatively, you can supply a grey value, as a number between 0.0 (black) and 1.0 (white). The following produces a very light grey:
520 {
521 Paint rectangle: 0.9, 0.0, 1.0, 0.0, 1.0
522 }
523 Finally, you can supply an explicit RGB (red–green–blue) value, as a “vector” of three numbers between 0.0 and 1.0.
524 The following produces dark pink:
525 {
526 Paint rectangle: {0.8,0.2,0.4}, 0.0, 1.0, 0.0, 1.0
527 }
528 8. How to supply arguments automatically
529 ========================================
530 Now you know all the ways to write the arguments of commands in a script line.
531 If you dislike manually copying arguments from settings windows into your script,
532 or if you are not sure whether something is a numeric or a string argument,
533 you can use the @@history mechanism@:
534 choose @@Clear history@ from the Edit menu in your @ScriptEditor,
535 click your command button, edit the arguments, and click #OK. The command will be executed.
536 Then choose @@Paste history@, and the command line, including the arguments,
537 will appear in the ScriptEditor at the position of the text cursor. You can build whole new scripts on the basis of this mechanism.
538
539 ################################################################################
540 )~~~"
541 MAN_PAGES_ENDcxx . . $ `sys/ManPagesM.h:28:9`!
542
543 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3. Simple language elements", U"ppgb", 20130421)
544 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"The Praat scripting language doesn't only call the menu commands "
545 "discussed in the @@Scripting 1. Your first scripts|first@ and @@Scripting 2. How to script settings windows|second@ chapters of this tutorial, "
546 "it is also a general procedural programming language that allows you to compute numbers, handle texts, and make custom analyses.")
547 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This chapter focuses on the things you need most. It is designed in such a way that you can work through it even if you haven't written computer programs before.")
548 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.1. Hello world@ (writeInfoLine, appendInfoLine)")
549 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.2. Numeric variables@ (assignments)")
550 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.3. Numeric queries")
551 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.4. String variables@ (assignments)")
552 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.5. String queries")
553 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.6. “For” loops@ (for, endfor)")
554 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 3.7. Layout@ (white space, comments, continuation lines)")
555 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
556
557 #define Manual_DRAW_PICTURE_WINDOWcxx . . $ `fon/manual_scripting.cpp:557:9`!(height,vpLeft,vpRight,vpTop,vpBottom) \
558 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (height, "Praat Picture", "File Edit Margins World Select Pen Font Help") \
559 "worldHeight = " #height " - 1\n" \
560 "Select inner viewport: 0.2, 5.8, 0.8, 0.8+worldHeight\n" \
561 "Axes: 0, 5.6, worldHeight, 0\n" \
562 "vpLeft = " #vpLeft "\nvpRight = " #vpRight "\nvpTop = " #vpTop "\nvpBottom = " #vpBottom "\n" \
563 "Paint rectangle: “Pink”, vpLeft, vpRight, vpTop, vpBottom\n" \
564 "Paint rectangle: “White”, vpLeft+0.69, vpRight-0.69, vpTop+0.46, vpBottom-0.46\n" \
565 "Yellow\n" \
566 "Draw line: 3, 0, 3, worldHeight\n" \
567 "for i to worldHeight/3\n" \
568 " Draw line: 0, i*3, 5.6, i*3\n" \
569 "Red\n" \
570 "for i to 5\n" \
571 " Text special: i, “centre”, 0, “top”, “Helvetica”, fontSize/1.2, “0”, string$(i)\n" \
572 "endfor\n" \
573 "for i to worldHeight\n" \
574 " Text special: 0, “left”, i, “half”, “Helvetica”, fontSize/1.2, “0”, string$(i)\n" \
575 "endfor\n" \
576 "Black\n" \
577 "Draw line: 0, 0, 5.6, 0\n" \
578
579 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.1. Hello world", U"ppgb", 20140111)
580 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Many manuals of computer programming languages start with their answer on the following question:")
581 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"%%How do I write the text “Hello world” on the screen?")
582 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For the Praat scripting language, there are two answers.")
583 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"1. “Hello world” in the Info window")
584 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The simplest answer is that you open the ScriptEditor window with ##New Praat script# from the #Praat menu, "
585 "then type the following line into the ScriptEditor window:")
586 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “Hello world”")
587 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and finally choose #Run from the #Run menu.")
588 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When you try this, the result should be that the Info window comes to the front, and that it shows the text `Hello world`:")
589 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
590 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
591 "Courier\n"
592 "Text: 0, “left”, 75, “half”, “\\s{Hello world}”\n"
593 "Draw rectangle: 0, 560, 0, 260\n"
594 )
595 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Now suppose that you want to write two lines instead of just one, so you try a script with two lines:")
596 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “Hello world”")
597 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “How do you do?”")
598 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This turns out not to do what you want: it seems to write only the text “How do you do?”. "
599 "This happens because the #writeInfoLine function first erases the Info window, then writes the line of text. "
600 "So the first line of the script did write the text “Hello world”, but the second line wiped it out "
601 "and wrote “How do you do?” instead. The script that does what you want is")
602 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “Hello world”")
603 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “How do you do?”")
604 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Now the result will be")
605 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
606 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
607 "Courier\n"
608 "Text: 0, “left”, 75, “half”, “\\s{Hello world}”\n"
609 "Text: 0, “left”, 90, “half”, “\\s{How do you do?}”\n"
610 "Draw rectangle: 0, 560, 0, 260\n"
611 )
612 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This works because @`appendInfoLine` writes a line without erasing the Info window first.")
613 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Finally, try the following script:")
614 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Another try”")
615 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Goodbye”")
616 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The result could be")
617 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
618 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
619 "Courier\n"
620 "Text: 0, “left”, 75, “half”, “\\s{Hello world}”\n"
621 "Text: 0, “left”, 90, “half”, “\\s{How do you do?}”\n"
622 "Text: 0, “left”, 105, “half”, “\\s{Another try}”\n"
623 "Text: 0, “left”, 120, “half”, “\\s{Goodbye}”\n"
624 "Draw rectangle: 0, 560, 0, 260\n"
625 )
626 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In other words, @`appendInfoLine` writes lines into the Info window without erasing it, even if you run a script anew. "
627 "This is why many Praat scripts that write into the Info window do a #writeInfoLine first, and follow it with a series of @`appendInfoLine` calls.")
628 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For more information on these commands, see @@Scripting 6.2. Writing to the Info window@.")
629 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"2. “Hello world” in the Picture window.")
630 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can also show text in the Picture window. If you are an experienced Praat user, you have probably used the comamnd ##Text top...# before. "
631 "You can use it do draw a text at the top of the current %viewport, which is the part of the Picture window where the next drawing will occur "
632 "and which is marked by the pink %margins. Thus, when you select the top 4\\xx3 inches of the Picture window (with the mouse), "
633 "set the font size to 12 (with the Pen menu), and run the script")
634 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Text top: “yes”, “Hello world”")
635 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"then you’ll see")
636 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 4.5, U""
637 Manual_DRAW_PICTURE_WINDOWcxx . . $ `fon/manual_scripting.cpp:557:9`! (4.5, 0,4,0,3)
638 "Select outer viewport: 0.2, 4.2, 0.8, 12\n" \
639 "Times\n" \
640 "Text top: “yes”, “Hello world”\n" \
641 "Select inner viewport: 0.2, 5.8, 0.2, 4.3\n"\
642 "Axes: 0, 1, 0, 1\n" \
643 "Draw rectangle: 0, 1, 0, 1\n"
644 )
645 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"So this works the same as when you choose ##Text top...# from the #Margins menu by hand, with #Far switched on.")
646 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you want your script to always show the same text at the same position, with nothing else in the picture, "
647 "then you can make your script a bit more extensive:")
648 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Erase all")
649 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Times")
650 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Font size: 12")
651 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Select outer viewport: 0, 4, 0, 3")
652 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Text top: “yes”, “Hello world”")
653 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In this script, line 1 erases the Picture window, so that nothing besides your text can appear in the Picture window.")
654 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Line 2 executes the command #Times from the #Font menu, so that the script will always draw the text in Times, "
655 "even if you choose #Helvetica in the Font menu with the mouse before you run the script "
656 "(after the script has run, you’ll see that #Times is chosen in the Font menu).")
657 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Line 3 executes the command ##Font size...# from the #Font menu, setting the font size to 12 and setting the width of the pink margins "
658 "accordingly.")
659 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Line 4 executes the command @@Select outer viewport...@ from the Select menu. "
660 "This performs an action that you would normally do by dragging the mouse, "
661 "namely selecting the part of the Picture window that runs from 0 to 4 inches horizontally "
662 "and from 0 to 3 inches vertically. After running the script, "
663 "the %viewport is indeed [0, 4] \\xx [0, 3], as you can clearly see "
664 "from the pink margins above.")
665 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Line 5 finally writes the text.")
666 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For more information on these commands, see @@Picture window@.")
667 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
668
669 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.2. Numeric variables", U"ppgb", 20230201)
670 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"In any general procedural programming language you can work with %variables, "
671 "which are places in your computer's memory where you can store a number or anything else.")
672 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For instance, you could put the number 3.1 into the variable $%b in the following way:")
673 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1")
674 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This statement is called as %assignment, i.e., you %assign the %value 3.1 to the %variable %`b`. "
675 "We read this statement aloud as “%`b` becomes 3.1” (or “%`b` gets 3.1”, but not “%`b` is 3.1”). "
676 "What this means is that after this statement, the memory location %`b` %contains the numeric value (number) 3.1.")
677 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can regard a variable as a box: you put the value 3.1 into the box named %`b`. "
678 "Or you can regard a variable as a house: the house is called %`b` and now the family “3.1” is living there. "
679 "Or you can regard it as any other storage location.")
680 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To see what value a variable contains (what’s in the box, or who lives in the house), "
681 "you can use the #`writeInfoLine` function:")
682 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1")
683 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The value is ”, b, “.”")
684 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This will put the text “`The value is 3.1.`” into the Info window, as you are invited to verify.")
685 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A variable is called a variable because it is %variable, i.e. its value can change. Try the script")
686 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1")
687 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 5.8")
688 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The value is ”, b, “.”")
689 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You will see that %`b` ends up having the value 5.8. The first line puts the value 3.1 there, but the second line "
690 "replaces it with 5.8. It’s like taking the 3.1 out of the box and putting the 5.8 in its stead. "
691 "Or the family 3.1 moves from the house, and the family called 5.8 moves in.")
692 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In an assignment, the part to the right of the “becomes” sign (the “=” sign) doesn’t have to be a number; "
693 "it can be any %formula that %evaluates to a number. For instance, the script")
694 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1 * 2")
695 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The value is ”, b, “.”")
696 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"puts the text “`The value is 6.2.`” into the Info window. This works because Praat handles the first line "
697 "in the following way:")
698 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. the formula `3.1 * 2` is %evaluated (i.e. its value is computed), and the result is 6.2.")
699 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. the value 6.2 is subsequently stored in the variable `b`.")
700 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"After line 1 has been executed, the variable %`b` just contains the value 6.2, nothing more; "
701 "the variable %`b` doesn’t remember that that value has been computed by multiplying 3.1 with 2.")
702 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Formulas can contain more things than numbers: they can also contain other variables:")
703 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1")
704 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"c = b * 2")
705 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The value of b is ”, b, “, and the value of c is ”, c, “.”")
706 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In the first line, %`b` gets the value 3.1. In the second line, the formula `b * 2` first has to be evaluated. "
707 "Praat looks up the value of %`b` (which is 3.1), so that it knows that the formula actually means `3.1 * 2`. "
708 "Praat evaluates this formula and stores the result (namely the value 6.2) "
709 "into the variable %`c`, which will then contain nothing else than the value 6.2. "
710 "The Info window thus reports “`The value of b is 3.1, and the value of c is 6.2.`”.")
711 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"After these explanations, consider the following script:")
712 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 3.1")
713 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"c = b * 2")
714 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"b = 5.8")
715 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The value of c is ”, c, “.”")
716 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Can you figure out what the Info will report? If you think it will report "
717 "“`The value of c is 6.2.`”, then you are correct: after the first line, %`b` contains the value 3.1; "
718 "after the second line, the value of %`c` is therefore 6.2, and nothing more; "
719 "after line 3, the value of %`b` has changed to 5.8, but the value of %`c` hasn’t changed and is still 6.2.")
720 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you thought that %`c` would end up having the value 11.6, then you’re thinking in terms "
721 "of a non-procedural language such as Prolog; you may have thought that the thing assigned to %`c` in the second line "
722 "is the whole %formula `b * 2`, so that %`c` changes when %`b` changes. But this is not the case: "
723 "the thing stored in %`c` is just the %value of the formula `b * 2` at that moment, which is 6.2, "
724 "and %`c` doesn’t remember how it got that value. If you have trouble understanding this, "
725 "consult anybody who writes programs.")
726 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
727
728 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.3. Numeric queries", U"ppgb", 20221202)
729 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Now that you know how to script a menu command, and you know how variables work, "
730 "you are ready to combine the two.")
731 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Suppose you have selected a Sound in the object list. One of the commands available in the @@Query submenu@ "
732 "is ##Get power...#. When you choose it, you will see the following settings window:")
733 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (5.4, Manual_SETTINGS_WINDOW_HEIGHTcxx . . $ `sys/ManPagesM.h:82:9`! (1), U""
734 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("Sound: Get power", 1)
735 Manual_DRAW_SETTINGS_WINDOW_RANGEcxx . . $ `sys/ManPagesM.h:126:9`! ("Time range (s)", "0.0", "0.0 (= all)")
736 )
737 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When you click OK, something like the following will appear in the Info window:")
738 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
739 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
740 "Courier\n"
741 "Text: 0, “left”, 75, “half”, “\\s{0.1350605005239421 Pa2}”\n"
742 "Draw rectangle: 0, 560, 0, 260\n"
743 )
744 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This is the mean power of the whole Sound.")
745 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In a script, you want to use the value of this power in the script itself, not in the Info window, "
746 "perhaps because you want to do computations with it or because you want to report the value with a nice text around it. "
747 "This is how you do the latter:")
748 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"power = Get power: 0.0, 0.0")
749 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The power of this sound is ”, power, “ Pascal-squared.”")
750 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The first line of this script executes the menu command ##Get power...#, "
751 "but puts the value 0.1350605005239421 into the variable %`power` instead of into the Info window "
752 "(the variable can have any name you like, as long as it starts with a lower-case letter "
753 "and consists of letters and digits; see @@Scripting 5.1. Variables@).")
754 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The second line then reports the value in the Info window, this time with a nice text around it:")
755 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
756 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
757 "Courier\n"
758 "Text: 0, “left”, 75, “half”, “\\s{The power of this sound is 0.1350605005239421 Pascal-squared.}”\n"
759 "Draw rectangle: 0, 560, 0, 260\n"
760 )
761 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
762
763 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.4. String variables", U"ppgb", 20130411)
764 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Just as you can store @@Scripting 3.2. Numeric variables|numeric variables@, "
765 "you can store %%string variables%, which contain text instead of numbers. Here is an example:")
766 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"word1$ = “Hello”")
767 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"word2$ = “world”")
768 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"sentence$ = word1$ + “ ” + word2$")
769 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The whole sentence is: ”, sentence$")
770 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Yes, this is another way to get the sentence `Hello world` into the Info window. "
771 "It's a more linguistically valid way to do it, and here is how it works:")
772 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. In line 1, the value “Hello”, which is a text (as we can see by its use of quotes), "
773 "is stored into the variable %`word1$`, which is a string variable (as we can see because its name ends in a dollar sign).")
774 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. In line 2, the text value “world” is stored into the string variable %`word2$`.")
775 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. In line 3, we have the formula `word1$ + “ ” + word2$`, which contains two variables, "
776 "namely %`word1$` and %`word2$`.")
777 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"4. The values of the two variables are “Hello” and “world”, respectively, "
778 "so what the formula actually says is “Hello” + “ ” + “world”.")
779 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"5. The pluses in the formula mean “concatenate”, so we concatenate the three strings "
780 "“Hello”, “ ”, and “world”, giving the longer string “Hello world”.")
781 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"6. Still in line 3, the string value “Hello world” is assigned to the string variable %`sentence$`.")
782 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"7. Line 4 reports in the Info window: `The whole sentence is: Hello world`")
783 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
784
785 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.5. String queries", U"ppgb", 20221202)
786 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Just as you can use menu commands (usually in a @@Query submenu@) to query @@Scripting 3.3. Numeric queries|numbers@, "
787 "you can query texts as well.")
788 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For instance, when you select a Textgrid, the @@Query submenu@ will contain the command ##Get label of interval...#, "
789 "which takes two numeric arguments, namely ##Tier number# and ##Interval number#:")
790 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (5.4, Manual_SETTINGS_WINDOW_HEIGHTcxx . . $ `sys/ManPagesM.h:82:9`! (2), U""
791 Manual_DRAW_SETTINGS_WINDOWcxx . . $ `sys/ManPagesM.h:83:9`! ("TextGrid: Get label of interval", 2)
792 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Tier number", "1")
793 Manual_DRAW_SETTINGS_WINDOW_FIELDcxx . . $ `sys/ManPagesM.h:118:9`! ("Interval number", "3")
794 )
795 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When you click OK, and interval 3 of tier 1 happens to contain the text `hello`, the following appears in the Info window:")
796 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
797 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
798 "Courier\n"
799 "Text: 0, “left”, 75, “half”, “\\s{hello}”\n"
800 "Draw rectangle: 0, 560, 0, 260\n"
801 )
802 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In a script, you will want to put the result of the query in a string variable instead of in the Info window, "
803 "because you want to manipulate it further:")
804 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 3")
805 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The text in interval 3 of tier 1 is: ”, text$")
806 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The script first stores the text of the interval, i.e. `hello`, into the variable %`text$`, "
807 "then writes it, preceded by some informative text, into the Info window:")
808 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
809 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
810 "Courier\n"
811 "Text: 0, “left”, 75, “half”, “\\s{The text in interval 3 of tier 1 is: hello}”\n"
812 "Draw rectangle: 0, 560, 0, 260\n"
813 )
814 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Hey, yet another way to implement “Hello world” with the Praat scripting language!")
815 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"The difference between numeric queries and string queries")
816 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A string query stores in a string variable the whole text that would appear in the Info window. "
817 "For instance, the script")
818 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"power$ = Get power: 0.0, 0.0")
819 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: power$")
820 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"could give you the following result:")
821 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
822 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
823 "Courier\n"
824 "Text: 0, “left”, 75, “half”, “\\s{0.1350605005239421 Pa2}”\n"
825 "Draw rectangle: 0, 560, 0, 260\n"
826 )
827 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A numeric query stores in a numeric variable only the first number that it can find in the text that would appear in the Info window. "
828 "For instance, the script")
829 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"power = Get power: 0.0, 0.0")
830 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: power")
831 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"could give you the following result:")
832 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
833 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
834 "Courier\n"
835 "Text: 0, “left”, 75, “half”, “\\s{0.1350605005239421}”\n"
836 "Draw rectangle: 0, 560, 0, 260\n"
837 )
838 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
839
840 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.6. “For” loops", U"ppgb", 20140111)
841 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"The power of a procedural programming language is most easily illustrated with the %%for-loop%.")
842 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Take the example of the @@Scripting 3.5. String queries|previous page@, "
843 "whereas you wanted to know the text in the third interval of the first tier of a selected TextGrid. "
844 "It’s easy to imagine that you actually want the texts of %%all the first five% intervals. "
845 "With knowledge from the previous sections, you could write it like this:")
846 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The texts in the first five intervals:”")
847 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 1")
848 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval 1: ”, text$")
849 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 2")
850 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval 2: ”, text$")
851 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 3")
852 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval 3: ”, text$")
853 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 4")
854 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval 4: ”, text$")
855 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, 5")
856 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval 5: ”, text$")
857 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The result will be something like")
858 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
859 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
860 "Courier\n"
861 "Text: 0, “left”, 75, “half”, “\\s{The texts in the first five intervals:}”\n"
862 "Text: 0, “left”, 90, “half”, “\\s{Interval 1: I}”\n"
863 "Text: 0, “left”, 105, “half”, “\\s{Interval 2: say}”\n"
864 "Text: 0, “left”, 120, “half”, “\\s{Interval 3: hello}”\n"
865 "Text: 0, “left”, 135, “half”, “\\s{Interval 4: and}”\n"
866 "Text: 0, “left”, 150, “half”, “\\s{Interval 5: you}”\n"
867 "Draw rectangle: 0, 560, 0, 260\n"
868 )
869 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This can be done more nicely. The first step is to realize that the sentences starting with `text$` are similar to each other, "
870 "and the sentence starting with `appendInfoLine` are also similar to each other. They only differ in the interval number, "
871 "and can therefore be made %identical by using a variable for the interval number, like this:")
872 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The texts in the first five intervals:”")
873 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"intervalNumber = 1")
874 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, intervalNumber")
875 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
876 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"intervalNumber = 2")
877 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, intervalNumber")
878 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
879 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"intervalNumber = 3")
880 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, intervalNumber")
881 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
882 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"intervalNumber = 4")
883 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, intervalNumber")
884 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
885 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"intervalNumber = 5")
886 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"text$ = Get label of interval: 1, intervalNumber")
887 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
888 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A new trick that you see here is that as a numeric argument (##Interval number#, "
889 "the second argument to ##Get label of interval...#), you can use not only a number "
890 "(as in all previous examples), but also a variable (%`intervalNumber`). "
891 "The rest of the script should be known stuff by now.")
892 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The script above is long, but it can be made much shorter with the use of a %%for-loop%:")
893 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The texts in the first five intervals:”")
894 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for intervalNumber from 1 to 5")
895 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"text$ = Get label of interval: 1, intervalNumber")
896 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
897 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
898 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The two lines that were repeated five times in the previous version now show up with indentation "
899 "between a `for` line and its corresponding `endfor`. Those two lines (the `text$` and the `appendInfoLine` line) "
900 "are executed five times: for %`intervalNumber` equal to 1, for %`intervalNumber` equal to 2, for %`intervalNumber` equal to 3, "
901 "for %`intervalNumber` equal to 4, and for %`intervalNumber` equal to 5, in that order.")
902 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In the above example, using a loop does not do much more than save eight lines, at the cost of adding two new lines. "
903 "But imagine the case in which you want to list %all the texts in the intervals: "
904 "the version without the loop is no longer possible. By contrast, the version %with the loop is still possible, "
905 "because we have the command ##Get number of intervals...#, which gives us the number of intervals in the specified tier "
906 "(here, tier 1). So you do:")
907 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"numberOfIntervals = Get number of intervals: 1")
908 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The texts in all ”, numberOfIntervals, “ intervals:”")
909 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for intervalNumber from 1 to numberOfIntervals")
910 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"text$ = Get label of interval: 1, intervalNumber")
911 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “: ”, text$")
912 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
913 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This may yield something like")
914 SCRIPTcxx . . $ `sys/ManPagesM.h:59:9`! (6, 3, U""
915 Manual_DRAW_WINDOWcxx . . $ `sys/ManPagesM.h:63:9`! (3, "Praat Info", "File Edit Search Convert Font Help")
916 "Courier\n"
917 "Text: 0, “left”, 75, “half”, “\\s{The texts in all 7 intervals:}”\n"
918 "Text: 0, “left”, 90, “half”, “\\s{Interval 1: I}”\n"
919 "Text: 0, “left”, 105, “half”, “\\s{Interval 2: say}”\n"
920 "Text: 0, “left”, 120, “half”, “\\s{Interval 3: hello}”\n"
921 "Text: 0, “left”, 135, “half”, “\\s{Interval 4: and}”\n"
922 "Text: 0, “left”, 150, “half”, “\\s{Interval 5: you}”\n"
923 "Text: 0, “left”, 165, “half”, “\\s{Interval 6: say}”\n"
924 "Text: 0, “left”, 180, “half”, “\\s{Interval 7: goodbye}”\n"
925 "Draw rectangle: 0, 560, 0, 260\n"
926 )
927 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This is the first script in this tutorial that is useful in itself. On the basis of it "
928 "you can create all kinds of ways to list the texts in intervals. Here is how you would also list the durations "
929 "of those intervals:")
930 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"numberOfIntervals = Get number of intervals: 1")
931 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The durations and texts in all ”, numberOfIntervals, “ intervals:”")
932 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for intervalNumber from 1 to numberOfIntervals")
933 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"startTime = Get start point: 1, intervalNumber")
934 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endTime = Get end point: 1, intervalNumber")
935 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"duration = endTime - startTime")
936 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"text$ = Get label of interval: 1, intervalNumber")
937 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “ is ”, "
938 "duration, “ seconds long and contains the text: ”, text$")
939 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
940 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
941
942 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 3.7. Layout", U"ppgb", 20170904)
943 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"This chapter handles the way you use white space, comments, and continuation lines in a Praat script.")
944 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"White space")
945 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Praat ignores all white space (spaces and tabs) that you put at the beginning of lines. The indentation "
946 "that you saw on the @@Scripting 3.6. “For” loops|previous page@ was therefore used solely for readability. "
947 "You are advised to use indenting, though, with three or four spaces for each level, "
948 "as in the following example, which loops over all tiers and intervals of a TextGrid:")
949 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The texts in all tiers and intervals:”")
950 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"numberOfTiers = Get number of tiers")
951 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for tierNumber from 1 to numberOfTiers")
952 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"numberOfIntervals = Get number of intervals: tierNumber")
953 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"for intervalNumber from 1 to numberOfIntervals")
954 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"text$ = Get label of interval: tierNumber, intervalNumber")
955 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"appendInfoLine: “Tier ”, tierNumber, “, interval ”, intervalNumber, “: ”, text$")
956 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endfor")
957 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
958 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Praat also ignores lines that are empty or consist solely of white space, "
959 "so you use those to structure your script visually.")
960 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Comments")
961 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Comments are lines that start with “`#`” or “`;`”. Praat ignores these lines when your script is running:")
962 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"# Create 1 second of a sine wave with a frequency of 100 Hertz,")
963 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"# sampled at 44100 Hz:")
964 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound from formula: “sine”, 1, 0, 1, 44100, ~ sin (2*pi*100*x)")
965 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Because of its visibility, you are advised to use “`#`” for comments that structure your script, "
966 "and “;” perhaps only for “commenting out” a statement, i.e. to temporarily put it before a line "
967 "that you don’t want to execute.")
968 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Continuation lines")
969 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"There is normally one line per statement, and one statement per line. But some statements are very long, "
970 "such as this one on a previous page:")
971 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “ is ”, duration, “ seconds long and contains the text: ”, text$")
972 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"By making the current window wider, you can see that I really put this whole statement on a single line. "
973 "I could have distributed it over two lines in the following way, by using three dots (an %ellipsis):")
974 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: “Interval ”, intervalNumber, “ is ”, duration, “ seconds long")
975 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... and contains the text: ”, text$")
976 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Here is another common type of example:")
977 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound from formula: “windowedSine”, 1, 0, 1, 44100,")
978 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... ~ 0.5 * sin(2*pi*1000*x) * exp(-0.5*((x-0.5)/0.1)^2)")
979 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You will normally want to follow such an ellipsis with a space, unless you want to concatenate "
980 "the parts of a long word:")
981 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Select outer viewport: 0, 10, 0, 4")
982 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Text top: “yes”, “It’s a long way to Llanfairpwllgwyngyll")
983 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"...gogerychwyrndrobwllllantysiliogogogoch,")
984 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... unless you start from Tyddyn-y-felin.”")
985 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
986
987 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 4. Object selection", U"ppgb", 20130501)
988 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"This chapter is about how to select objects from your script, "
989 "and how to find out what objects are currently selected.")
990 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 4.1. Selecting objects")
991 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 4.2. Removing objects")
992 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Scripting 4.3. Querying objects")
993 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
994
995 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 4.1. Selecting objects", U"ppgb", 20180428)
996 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To simulate the mouse-clicked and dragged selection in the list of objects, "
997 "you have the functions @`selectObject`, @`plusObject` and @`minusObject`.")
998 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Suppose you start Praat and use ##Create Sound as tone...# to create a Sound called %tone. "
999 "In the object list it looks like “1. Sound tone”. "
1000 "Suppose you then do ##To Spectrum...# from the ##Analyse Spectrum# menu. "
1001 "A second object, called “2. Spectrum tone” appears in the list and is selected. "
1002 "To select and play the Sound, you can do either")
1003 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: 1")
1004 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
1005 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
1006 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: “Sound tone”")
1007 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
1008 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"So you can select an object either by its unique ID (identifier: the unique number by which it appears in the list) "
1009 "or by name.")
1010 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The function @`selectObject` works by first deselecting all objects, and then selecting the one you mention. "
1011 "If you don’t want to deselect the existing selection, you can use @`plusObject` or @`minusObject`. "
1012 "When the Sound is selected, you can select the Spectrum as well by doing")
1013 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{plusObject}: 2")
1014 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
1015 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{plusObject}: “Spectrum tone”")
1016 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you then want to deselect the Sound, and keep the Spectrum selected, you can do")
1017 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{minusObject}: 1")
1018 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
1019 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{minusObject}: “Sound tone”")
1020 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"All these functions can take more than one argument. To select the Sound and the Spectrum together, you can do")
1021 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: 1, 2")
1022 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
1023 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: “Sound tone”, “Spectrum tone”")
1024 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or even")
1025 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: 1, “Spectrum tone”")
1026 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or, using a numeric vector:")
1027 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"myObjects# = { 1, 2 }")
1028 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{selectObject}: myObjects#")
1029 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"How to refer to objects created in your script")
1030 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In a script, you typically don't know whether the IDs of the objects are 1 and 2, or much higher numbers. "
1031 "Fortunately, commands that create a new object give you the ID of the object that is created, "
1032 "so that you can refer to the object later on. For instance, suppose you want to generate a sine wave, play it, "
1033 "draw its spectrum, and then throw away both the Sound and the Spectrum. Here is how you do it:")
1034 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"sound = Create Sound as pure tone: “sine377”,")
1035 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound")
1036 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play ; the Sound is selected, so it plays")
1037 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Spectrum: “yes”")
1038 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn")
1039 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"# Remove the created Spectrum and Sound:")
1040 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{plusObject}: sound ; the Spectrum was already selected")
1041 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
1042 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You could also select the objects by name:")
1043 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound as pure tone: “sine377”,")
1044 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; no need to remember the ID of the Sound")
1045 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play ; the Sound is selected, so it plays")
1046 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Spectrum: “yes”")
1047 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn")
1048 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"# Remove the created Spectrum and Sound:")
1049 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#`{plusObject}: “Sound sine377” ; the Spectrum was already selected")
1050 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
1051 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This works even if there are multiple objects called “Sound sine377”, "
1052 "because if there are more objects with the same name, @`selectObject` and @`plusObject` select the most recently created one, "
1053 "i.e., the one nearest to the bottom of the list of objects.")
1054 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
1055
1056
1057 MAN_PAGES_BEGINcxx . . $ `sys/ManPagesM.h:27:9`!
1058 R"~~~(
1059 ################################################################################
1060 "Scripting 4.2. Removing objects"
1061 © Paul Boersma 1999,2004,2006–2008,2011,2013,2014
1062
1063 In @@Scripting 4.1. Selecting objects|\SS4.1@ we saw that objects could be removed by
1064 selecting them first and then calling the #Remove command.
1065 A faster way is the #removeObject function, which can also remove unselected objects:
1066 {;
1067 sound = Create Sound as pure tone: “sine377”,
1068 ... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound
1069 Play ; the Sound is selected, so it plays
1070 spectrum = To Spectrum: “yes” ; remember the ID of the Spectrum
1071 Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn
1072 # Remove the created Spectrum and Sound:
1073 \#`{removeObject}: sound, spectrum ; remove one selected and one unselected object
1074 }
1075 The #removeObject function keeps the objects selected that were selected before
1076 (except of course the ones it throws away).
1077 This allows you to easily throw away objects as soon as you no longer need them:
1078 {;
1079 sound = Create Sound as pure tone: “sine377”,
1080 ... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound
1081 Play ; the Sound is selected, so it plays
1082 spectrum = To Spectrum: “yes”
1083 \#`{removeObject}: sound ; we no longer need the Sound, so we remove it
1084 Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is still selected, so it is drawn
1085 \#`{removeObject}: spectrum ; remove the last object created by the script
1086 }
1087 Selecting and removing all objects from the list (don’t)
1088 ========================================================
1089 A very strange command, which you should not normally use, is `select all`:
1090 {;
1091 \#{select all}
1092 Remove
1093 }
1094 This selects all objects in the list and then removes them.
1095 Please try not to use this, because it will remove even the objects that your script did not create!
1096 After all, you don’t want the users of your script to lose the objects they created!
1097 So please try to remove in your script only the objects that your script created,
1098 even if the script is for your own use (because if it is a nice script, others will want to use it).
1099
1100 ################################################################################
1101 "Scripting 4.3. Querying objects"
1102 © Paul Boersma 1999,2004,2006–2008,2011,2013,2014,2018,2023
1103
1104 You can get the name of a selected object into a string variable.
1105 For instance, the following reads the name of the second selected Sound
1106 (as counted from the top of the list of objects) into the variable %`name$`:
1107 {;
1108 name$ = \#`{selected$} (“Sound”, 2)
1109 }
1110 If the Sound was called “Sound hallo”, the variable %`name$` will contain the string “hallo”.
1111 To get the name of the topmost selected Sound object, you can leave out the number:
1112 {;
1113 name$ = selected$ (“Sound”)
1114 }
1115 To get the full name (type + name) of the third selected object, you do:
1116 {;
1117 fullName$ = selected$ (3)
1118 }
1119 To get the full name of the topmost selected object, you do:
1120 {;
1121 fullName$ = selected$ ()
1122 }
1123 To get the type and name out of the full name, you do:
1124 {;
1125 type$ = \`{extractWord$} (fullName$, “”)
1126 name$ = \`{extractLine$} (fullName$, “ ”)
1127 }
1128 Negative numbers count from the bottom. Thus, to get the name of the bottom-most selected Sound
1129 object, you say
1130 {;
1131 name$ = selected$ (“Sound”, -1)
1132 }
1133 You would use `selected$` () for drawing the object name in a picture:
1134 {;
1135 Draw: 0, 0, 0, 0, “yes”
1136 name$ = selected$ (“Sound”)
1137 Text top: “no”, “This is sound ” + name$
1138 }
1139 For identifying previously selected objects, this method is not very suitable, since
1140 there may be multiple objects with the same name:
1141 {;
1142 # The following two lines are OK:
1143 soundName$ = selected$ (“Sound”, -1)
1144 pitchName$ = selected$ (“Pitch”)
1145 # But the following line is questionable, since it doesn’t
1146 # necessarily select the previously selected Pitch again:
1147 selectObject: “Pitch ” + pitchName$
1148 }
1149 Instead of this error-prone approach, you should get the object’s unique ID.
1150 The correct version of our example becomes:
1151 {;
1152 sound = selected (“Sound”, -1)
1153 pitch = selected (“Pitch”)
1154 # Correct:
1155 selectObject: pitch
1156 }
1157 To get the number of selected Sound objects into a variable, use
1158 {;
1159 numberOfSelectedSounds = numberOfSelected (“Sound”)
1160 }
1161 To get the number of selected objects into a variable, use
1162 {;
1163 numberOfSelectedObjects = numberOfSelected ()
1164 }
1165 Example: doing something to every selected Sound
1166 ================================================
1167 {;
1168 sounds# = \#`{selected#} (“Sound”)
1169 # Median pitches of all selected sounds:
1170 for i to size (sounds#)
1171 selectObject: sounds# [i]
1172 To Pitch (filtered ac): 0.0, 50, 800, 15, “no”, 0.03, 0.09, 0.50, 0.055, 0.35, 0.14
1173 f0 = Get quantile: 0, 0, 0.50, “Hertz”
1174 appendInfoLine: f0
1175 Remove
1176 endfor
1177 # Restore selection:
1178 selectObject (sounds#)
1179 }
1180
1181 ################################################################################
1182 "Scripting 5. Language elements reference"
1183 © Paul Boersma 2017-07-18
1184
1185 In a Praat script, you can use variables, expressions, and functions, of numeric as well as string type,
1186 and most of the control structures known from other procedural computer languages.
1187 The way the distinction between numbers and strings is made, may remind you of the programming language Basic.
1188
1189 , @@Scripting 5.1. Variables@ (numeric, string)
1190 , @@Scripting 5.2. Expressions@ (numeric, string)
1191 , @@Scripting 5.3. Jumps@ (if, then, elsif, else, endif)
1192 , @@Scripting 5.4. Loops@ (for/endfor, while/endwhile, repeat/until)
1193 , @@Scripting 5.5. Procedures@ (\@ , procedure)
1194 , @@Scripting 5.6. Arrays and dictionaries@
1195 , @@Scripting 5.7. Vectors and matrices
1196 , @@Scripting 5.8. Including other scripts@
1197 , @@Scripting 5.9. Quitting@ (exit)
1198
1199 ################################################################################
1200 "Scripting 5.1. Variables"
1201 © Paul Boersma 2020-12-29,2024,2025
1202
1203 A %variable is a location in your computer’s memory that has a name and where you can store something,
1204 as explained in @@Scripting 3.2. Numeric variables|\\SS3.2@ and @@Scripting 3.4. String variables|\\SS3.4@.
1205 In a Praat script, you can store numbers and texts, i.e. you can use %%numeric variables% and %%string variables%.
1206
1207 Numeric variables
1208 =================
1209
1210 Numeric variables can hold integer numbers between -1,000,000,000,000,000 and +1,000,000,000,000,000
1211 or real numbers between -10^^308^ and +10^^308^. The smallest numbers lie near -10^^-308^ and +10^^-308^.
1212
1213 You use numeric variables in your script like this:
1214 { 4x2
1215 length = 10
1216 Axes: 0, 100, 0, 100
1217 Draw line: 0, 50, length, 50
1218 Draw inner box
1219 }
1220 This draws a line in the Picture window from position (0, 50) to position (%`length`, 50).
1221 In the first line, you assign the value 10 to the variable called %`length`,
1222 and in the third line you use the value of %`length` as the third argument to the command ##Draw line...#.
1223
1224 Names of numeric variables must start with a lower-case letter, optionally followed by a sequence
1225 of letters, digits, and underscores.
1226
1227 String variables
1228 ================
1229
1230 You use string variables, which contain text, as follows:
1231 { 4x2
1232 title$ = “Dutch nasal place assimilation”
1233 Text top: “no”, title$
1234 Draw inner box
1235 }
1236 This writes the text “Dutch nasal place assimilation” above your drawing.
1237
1238 As in the programming language Basic, the names of string variables end in a dollar sign.
1239
1240 Making numeric variables visible
1241 ================================
1242
1243 You can write the content of numeric variables directly to the info window:
1244 {;
1245 x = 2.0
1246 root = sqrt (x)
1247 \#`{writeInfoLine}: “The square root of ”, x, “ is ”, root, “.”
1248 }
1249 When you try this out, this will write the following text to the Info window:
1250 `
1251 The square root of 2 is 1.4142135623730951.
1252 `
1253 {-;
1254 assert string$ (sqrt (2)) = "1.4142135623730951"
1255 }
1256
1257 You can fix the number of digits after the decimal point by use of the @`fixed$` function:
1258 {
1259 x = 2.0
1260 root = sqrt (x)
1261 \`{writeInfoLine}: “The square root of ”, \#`{fixed$} (x, 3), “ is approximately ”, \#`{fixed$} (root, 3), “.”
1262 }
1263 {-;
1264 assert fixed$ (sqrt (2), 3) = "1.414"
1265 }
1266
1267 By using 0 decimal digits, you round to whole values:
1268 {
1269 root = sqrt (2)
1270 \`{writeInfoLine}: “The square root of 2 is very approximately ”, fixed$ (root, 0), “.”
1271 }
1272 {-;
1273 assert fixed$ (sqrt (2), 0) = "1"
1274 }
1275
1276 By using the `percent$` function, you give the result in a percent format:
1277 {
1278 jitter = 0.0156789
1279 \`{writeInfoLine}: “The jitter is ”, \#`{percent$} (jitter, 3), “.”
1280 }
1281 {-;
1282 jitter = 0.0156789
1283 assert percent$ (jitter, 3) = "1.568%"
1284 jitter = -0.0156789
1285 assert percent$ (jitter, 3) = "-1.568%"
1286 }
1287
1288 The number 0, however, will always be written as 0, and for small numbers the number of
1289 significant digits will never be less than 1:
1290 {
1291 jitter = 0.000000156789
1292 \`{writeInfoLine}: “The jitter is ”, percent$ (jitter, 3), “.”
1293 }
1294 {-;
1295 assert percent$ (0, 3) = "0"
1296 jitter = 0.000000156789
1297 assert percent$ (jitter, 3) = "0.00002%"
1298 jitter *= -1
1299 assert percent$ (jitter, 3) = "-0.00002%"
1300 }
1301
1302 Predefined variables
1303 ====================
1304
1305 All of the variables you saw earlier in this tutorial were defined at the first moment a value was assigned to them.
1306 Some variables, however, are already defined implicitly at the start of your script.
1307
1308 Some predefined ##numeric variables# are `macintosh`, `windows`, and `unix`, which are 1 if your edition of Praat
1309 was built for the Macintosh, Windows, or Unix platform (respectively), and which are otherwise zero.
1310 Likewise, we have `praat_32bit` and `praat_64bit`, of which one is 1 and the other 0,
1311 depending on whether your edition of Praat was built for 32-bit or 64-bit computers.
1312 More precisely, we have `praat_intel32`, `praat_intel64`, `praat_arm64`, `praat_s390x`, and `praat_armv7`,
1313 one of which is 1 (and the others 0)
1314 depending on whether the type of processor chip that your edition of Praat was made for is
1315 32-bit Intel (= x86, i386, i686), or 64-bit Intel (= AMD64), or ARM64 (= Aarch64),
1316 or s390x (LinuxONE), or ARMv7 (smaller Raspberry Pi).
1317 Another predefined numeric variable is `praatVersion`, which is something like 6443.
1318
1319 Some ##predefined string variables# are `newline$`, `tab$`, and `shellDirectory$`.
1320 The last one specifies the folder that was the default folder when Praat started up;
1321 you can use it in scripts that run from the Unix or Windows command line.
1322 Likewise, there exist the predefined string variables `homeDirectory$`,
1323 `preferencesDirectory$`, and `temporaryDirectory$`. These three refer to your home folder
1324 (which is where you log in), the Praat @@preferences folder@, and a folder for saving temporary files;
1325 if you want to know what they are on your computer, try to write them into a script window.
1326 The variable `defaultDirectory$` is available for formulas in scripts; it is the folder that contains the script file.
1327 Finally, we have `praatVersion$`, which is something like “6.4.43”."
1328
1329 A disadvantage of predefined variables is that they can be changed, in which case they take on a different meaning.
1330 For this reason you are advised to use functions instead (if they exist), because these always have the same meaning:
1331 - @`appVersion`() instead of `praatVersion`
1332 - @`appVersion$`() instead of `praatVersion$`
1333
1334 Functions that handle variables
1335 ===============================
1336
1337 To check whether a variable exists, you can use the function
1338 {;
1339 variableExists (variableName$)
1340 }
1341 as in
1342 {
1343 fgh = 567
1344 assert variableExists ("fgh")
1345 assert not variableExists ("jhfwbfejfgcds")
1346 }
1347
1348 ################################################################################
1349 )~~~"
1350 MAN_PAGES_ENDcxx . . $ `sys/ManPagesM.h:28:9`!
1351
1352 /*
1353 form Convert from WAV to AIFF
1354 text fileName hello.wav
1355 endform
1356 fileName$ = fileName$ - ".wav"
1357 Read from file: shellDirectory$ + \"/\" + fileName$ + \".wav\"
1358 Save as AIFF file: shellDirectory$ + \"/\" + fileName$ + \".aiff\"
1359
1360 if left$ (fileName$) <> "/"
1361 fileName$ = shellDirectory$ + \"/\" + fileName$
1362 endif
1363 */
1364
1365 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 5.2. Expressions", U"ppgb", 20180721)
1366 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"In a Praat script, you can use numeric expressions as well as string expressions.")
1367 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Numeric expressions")
1368 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use a large variety of @@Formulas@ in your script:")
1369 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"length = 10")
1370 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"height = length/2")
1371 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"area = length * height")
1372 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The area is ”, area, “.”")
1373 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use numeric variables and formulas in numeric arguments to commands:")
1374 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw line: 0, 0, length / 2, 2 * height")
1375 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use numeric expressions in assignments (as above), or after "
1376 "#`if`, #`elsif`, #`while`, #`until`, and twice after #`for`.")
1377 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"String expressions")
1378 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use a large variety of @@Formulas@ in your script:")
1379 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"addressee$ = “Silke”")
1380 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"greeting$ = “Hi ” + addressee$ + “!”")
1381 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “The greeting is: ”, greeting$")
1382 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use string variables and formulas in numeric arguments to commands:")
1383 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw line: 0, length (greeting$), 0, 100")
1384 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw line: 0, if answer$ = “yes” then 20 else 30 fi, 0, 100")
1385 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use numeric and string variables and formulas in string arguments to commands:")
1386 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Text top: “yes”, “Hi ” + addressee$ + “!”")
1387 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Text top: “yes”, left$ (fileName$, index (fileName$, “.”) - 1)")
1388 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Assignments from query commands")
1389 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On how to get information from commands that normally write to the Info window, "
1390 "see @@Scripting 6.3. Query commands@.")
1391 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
1392
1393 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 5.3. Jumps", U"ppgb", 19991112)
1394 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can use conditional jumps in your script:")
1395 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#if %expression")
1396 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#elsif %expression")
1397 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"if the expression evaluates to zero or %false, the execution of the script jumps to the next "
1398 "#`elsif` or after the next #`else` or #`endif` at the same depth.")
1399 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The following script computes the preferred length of a bed for a person `age` years of age:")
1400 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if age <= 3")
1401 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"length = 1.20")
1402 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"elsif age <= 8")
1403 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"length = 1.60")
1404 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"else")
1405 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"length = 2.00")
1406 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
1407 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A variant spelling for #`elsif` is #`elif`.")
1408 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
1409
1410 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 5.4. Loops", U"ppgb", 20140111)
1411 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"\"For\" loops")
1412 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#for %variable #from %expression__1_ #to %expression__2_")
1413 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#for %variable #to %expression")
1414 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"the statements between the #for line and the matching #endfor will be executed "
1415 "while a variable takes on values between two expressions, with an increment (raise) of 1 "
1416 "on each turn of the loop. If there is no #from, the loop variable starts at 1.")
1417 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The following script plays nine sine waves, with frequencies of 200, 300, ..., 1000 Hz:")
1418 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{for} i \\#{from} 2 \\#{to} 10")
1419 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Create Sound as pure tone: \"tone\", 1, 0, 0.3, 44100, i * 100, 0.2, 0.01, 0.01")
1420 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Play")
1421 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Remove")
1422 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endfor}")
1423 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The stop value of the #for loop is evaluated on each turn. If the second expression "
1424 "is already less than the first expression to begin with, the statements between #for and #endfor "
1425 "are not executed even once.")
1426 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"“Repeat” loops")
1427 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#until %expression")
1428 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"the statements between the matching preceding #repeat and the #until line "
1429 "will be executed again if the expression evaluates to zero or %false.")
1430 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The following script measures the number of trials it takes me to throw 12 with two dice:")
1431 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"throws = 0")
1432 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{repeat}")
1433 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"eyes = randomInteger (1, 6) + randomInteger (1, 6)")
1434 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"throws = throws + 1")
1435 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{until} eyes = 12")
1436 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"It took me \", throws, \" trials to throw 12 with two dice.\"")
1437 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The statements in the #repeat/#until loop are executed at least once.")
1438 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"\"While\" loops")
1439 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#while %expression")
1440 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"if the expression evaluates to zero or %false, the execution of the script jumps "
1441 "after the matching #endwhile.")
1442 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#endwhile")
1443 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"execution jumps back to the matching preceding #while line, which is then evaluated again.")
1444 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The following script forces the number %x into the range [0; 2\\pi):")
1445 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{while} x < 0")
1446 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"x = x + 2 * pi")
1447 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endwhile}")
1448 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{while} x >= 2 * pi")
1449 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"x = x - 2 * pi")
1450 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endwhile}")
1451 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If the expression evaluates to zero or %false to begin with, the statements between #while and #endwhile "
1452 "are not executed even once.")
1453 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
1454
1455 MAN_PAGES_BEGINcxx . . $ `sys/ManPagesM.h:27:9`!
1456 R"~~~(
1457 ################################################################################
1458 "Scripting 5.5. Procedures"
1459 © Paul Boersma 2017-09-04,2025
1460
1461 Sometimes in a Praat script, you will want to perform the same thing more than once.
1462 In @@Scripting 5.4. Loops|\SS5.4@ we saw how %loops can help there.
1463 In this section we will see how %procedures (also called %subroutines) can help us.
1464
1465 Imagine that you want to play a musical note with a frequency of 440 Hz (an “A”)
1466 followed by a note that is one octave higher, i.e. has a frequency of 880 Hz (an “a”).
1467 You could achieve this with the following script:
1468 {;
1469 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 440, 0.2, 0.01, 0.01
1470 Play
1471 Remove
1472 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 880, 0.2, 0.01, 0.01
1473 Play
1474 Remove
1475 }
1476 This script creates a sound with a sine wave with an amplitude of 0.4 and a frequency of 440 Hz,
1477 then plays this sound, then changes the sound into a sine wave with a frequency of 880 Hz,
1478 then plays this changed sound, and then removes the Sound object from the object list.
1479
1480 This script is perfect if all you want to do is to play those two notes and nothing more.
1481 But now imagine that you want to play such an octave jump not only for a note of 440 Hz,
1482 but also for a note of 400 Hz and for a note of 500 Hz. You could use the following script:
1483 {;
1484 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 440, 0.2, 0.01, 0.01
1485 Play
1486 Remove
1487 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 880, 0.2, 0.01, 0.01
1488 Play
1489 Remove
1490 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 400, 0.2, 0.01, 0.01
1491 Play
1492 Remove
1493 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 800, 0.2, 0.01, 0.01
1494 Play
1495 Remove
1496 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 500, 0.2, 0.01, 0.01
1497 Play
1498 Remove
1499 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, 1000, 0.2, 0.01, 0.01
1500 Play
1501 Remove
1502 }
1503 This script works but is no longer perfect. It contains many similar lines, and is difficult to read.
1504
1505 Here is where %procedures come in handy. With procedures, you can re-use similar pieces of code.
1506 To make the three parts of the above script more similar, I’ll rewrite it using two variables
1507 (%`frequency` and %`octaveHigher`):
1508 {;
1509 frequency = 440
1510 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1511 Play
1512 Remove
1513 octaveHigher = 2 * frequency
1514 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1515 Play
1516 Remove
1517 frequency = 400
1518 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1519 Play
1520 Remove
1521 octaveHigher = 2 * frequency
1522 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1523 Play
1524 Remove
1525 frequency = 500
1526 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1527 Play
1528 Remove
1529 octaveHigher = 2 * frequency
1530 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1531 Play
1532 Remove
1533 }
1534 You can now see that seven lines of the script appear identically three times.
1535 I'll put those seven lines into a %procedure that I name #`playOctave`:
1536 {;
1537 \#{procedure} playOctave
1538 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1539 Play
1540 Remove
1541 octaveHigher = 2 * frequency
1542 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1543 Play
1544 Remove
1545 \#{endproc}
1546 }
1547 As you see, a %%procedure definition% in Praat consists of three parts:
1548
1549 1. a line with the word #`procedure`, followed by the name of the procedure, followed by a pair of parentheses;
1550 2. the %body of the procedure (here: seven lines);
1551 3. a line with the word #`endproc`.
1552
1553 You can put a procedure definition anywhere in your script;
1554 the beginning or end of the script are common places.
1555
1556 The bodies of procedures are executed only if you %call the procedure explicitly,
1557 which you can do anywhere in the rest of your script:
1558 {;
1559 frequency = 440
1560 @playOctave
1561 frequency = 400
1562 @playOctave
1563 frequency = 500
1564 @playOctave
1565 \#{procedure} playOctave
1566 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1567 Play
1568 Remove
1569 octaveHigher = 2 * frequency
1570 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1571 Play
1572 Remove
1573 \#{endproc}
1574 }
1575 This script works as follows. First, the number 440 is assigned to the variable %`frequency` in line 1.
1576 Then, execution of the script arrives at the #`@` (“call”) statement of line 2.
1577 Praat then knows that it has to jump to the procedure called %`playOctave`,
1578 which is found on line 7. The execution of the script then proceeds with the first line of the procedure body,
1579 where a Sound is created. Then, the other lines of the procedure body are also executed, ending with the removal of the Sound.
1580 Then, the execution of the script arrives at the #`endproc` statement. Here, Praat knows that it has to jump back
1581 to the line after the line that the procedure was called from; since the procedure was called from line 2,
1582 the execution proceeds at line 3 of the script. There, the number 400 is assigned to the variable %`frequency`.
1583 In line 4, execution will jump to the procedure again, and with the next #`endproc` the execution will jump back to line 5.
1584 There, 500 is assigned to %`frequency`, followed by the third jump to the procedure.
1585 the third #`endproc` jumps back to the line after the third #`@`, i.e. to line 7.
1586 Here the execution of the script will stop, because there are no more executable commands
1587 (the procedure definition at the end is not executed again).
1588
1589 Arguments
1590 =========
1591
1592 The above example contains something awkward. The procedure %`playOctave` requires that the variable %`frequency`
1593 is set to an appropriate value, so before calling %`playOctave` you always have to insert a line like
1594 {;
1595 frequency = 440
1596 }
1597 This can be improved upon. In the following version of the script, the procedure %`playOctave` requires an explicit %argument:
1598 {;
1599 @playOctave: 440
1600 @playOctave: 400
1601 @playOctave: 500
1602 \#{procedure} playOctave: frequency
1603 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1604 Play
1605 Remove
1606 octaveHigher = 2 * frequency
1607 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1608 Play
1609 Remove
1610 \#{endproc}
1611 }
1612 This works as follows. The first line of the procedure now not only contains the name (%`playOctave`),
1613 but also a list of variables (here only one: %`frequency`). In the first line of the script,
1614 the procedure %`playOctave` is called with the %%argument% 440.
1615 Execution then jumps to the procedure, where the argument 440 is assigned to the variable %`frequency`,
1616 which is then used in the body of the procedure.
1617
1618 Encapsulation and local variables
1619 =================================
1620
1621 Although the size of the script has now been reduced to 12 lines, which cannot be further improved upon,
1622 there is still something wrong with it. Imagine the following script:
1623 {;
1624 frequency = 300
1625 @playOctave: 440
1626 @playOctave: 400
1627 @playOctave: 500
1628 \`{writeInfoLine}: frequency
1629 \#{procedure} playOctave: frequency
1630 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, frequency, 0.2, 0.01, 0.01
1631 Play
1632 Remove
1633 octaveHigher = 2 * frequency
1634 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, octaveHigher, 0.2, 0.01, 0.01
1635 Play
1636 Remove
1637 \#{endproc
1638 }
1639 You might have thought that this script will write “300” to the Info window,
1640 because that is what you expect if you look at the first five lines.
1641 However, the procedure will assign the values 440, 400, and 500 to the variable %`frequency`,
1642 so that the script will actually write “500” to the Info window, because 500
1643 is the last (fourth!) value that was assigned to the variable %`frequency`.
1644
1645 What you would want is that variables that are used inside procedures, such as %`frequency` and %`octaveHigher` here,
1646 could somehow be made not to “clash” with variable names used outside the procedure.
1647 A trick that works would be to include the procedure name into the names of these variables:
1648 {;
1649 frequency = 300
1650 @playOctave: 440
1651 @playOctave: 400
1652 @playOctave: 500
1653 \`{writeInfoLine}: frequency
1654 \#{procedure} playOctave: playOctave.frequency
1655 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, playOctave.frequency, 0.2, 0.01, 0.01
1656 Play
1657 Remove
1658 playOctave.octaveHigher = 2 * playOctave.frequency
1659 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, playOctave.octaveHigher, 0.2, 0.01, 0.01
1660 Play
1661 Remove
1662 \#{endproc
1663 }
1664 This works. The six tones will be played, and \"300\" will be written to the Info window.
1665 But the formulation is a bit wordy, isn’t it?
1666
1667 Fortunately, Praat allows an abbreviated version of these long names:
1668 just leave “playOctave” off from the names of the variables, but keep the period (`.`):
1669 {;
1670 frequency = 300
1671 @playOctave: 440
1672 @playOctave: 400
1673 @playOctave: 500
1674 \`{writeInfoLine}: frequency
1675 \#{procedure} playOctave: .frequency
1676 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, .frequency, 0.2, 0.01, 0.01
1677 Play
1678 Remove
1679 .octaveHigher = 2 * .frequency
1680 Create Sound as pure tone: “note”, 1, 0, 0.3, 44100, .octaveHigher, 0.2, 0.01, 0.01
1681 Play
1682 Remove
1683 \#{endproc
1684 }
1685 This is the final version of the script. It works because Praat knows that
1686 you are using the variable names %`.frequency` and %`.octaveHigher` in the context of the procedure %`playOctave`,
1687 so that Praat knows that by these variable names you actually mean to refer to %`playOctave.frequency` and %`playOctave.octaveHigher`.
1688
1689 It is advisable that you use such “local” variable names for all %parameters of a procedure,
1690 i.e. for the variables listed after the #`procedure` word (e.g. %`.frequency`),
1691 as well as for all variables that you create in the procedure body (e.g. %`.octaveHigher`).
1692 In this way, you make sure that you don't inadvertently use a variable name that is also used outside the procedure
1693 and thereby perhaps inadvertently change the value of a variable that you expect to be constant across a procedure call.
1694
1695 A list of numeric and string arguments
1696 ======================================
1697
1698 You can use multiple arguments, separated by commas, and string arguments (with a dollar sign in the variable name):
1699 {
1700 @listSpeaker: “Bart”, 38
1701 @listSpeaker: “Katja”, 24
1702 \#{procedure} listSpeaker: .name$, .age
1703 appendInfoLine: “Speaker ”, .name$, “ is ”, .age, “ years old.”
1704 \#{endproc
1705 }
1706 or
1707 {
1708 @conjugateVerb: “be”, “I am”, “you are”, “she is”
1709 \#{procedure} conjugateVerb: .verb$, .first$, .second$, .third$
1710 writeInfoLine: “Conjugation of 'to ”, .verb$, “':”
1711 appendInfoLine: “1sg ”, .first$
1712 appendInfoLine: “2sg ”, .second$
1713 appendInfoLine: “3sg ”, .third$
1714 \#{endproc}
1715 }
1716 For the arguments you can use expressions:
1717 {;
1718 @playOctave: 400 + 100
1719 }
1720 As with all string literals, the double quotes in literal string arguments should be doubled:
1721 {;
1722 \#{procedure} texts: .top$, .bottom$
1723 Text top: “yes”, .top$
1724 Text bottom: “yes”, .bottom$
1725 \#{endproc}
1726 @texts: """ hello"" at the top", """goodbye"" at the bottom"
1727 }
1728 unless you use curly quotes:
1729 {;
1730 @texts: “" hello" at the top”, “"goodbye" at the bottom”
1731 }
1732 or even
1733 {;
1734 @texts: ““ hello” at the top”, ““goodbye” at the bottom”
1735 }
1736
1737 Restrictions
1738 ============
1739
1740 You can have any number of procedures in your script. The name of a procedure can have any number of characters (at least 1, of course).
1741 A procedure name can consist of letters (“word characters”), underscores (“_”), periods (“.”) and digits,
1742 but it cannot start with a digit; this is the same restriction as for variable names.
1743
1744 Functions
1745 =========
1746
1747 The Praat scripting language does not have the concept of a “function” like some other scripting languages do.
1748 A function is a procedure that returns a number, a string, a vector, a matrix, or a string array.
1749 For instance, you can imagine the function `squareNumber` (),
1750 which takes a number (e.g. 5) as an argument and returns the square of that number (e.g. 25).
1751 Here is an example of how you can do that, using the global availability of local variables:
1752 {
1753 @squareNumber: 5
1754 \`{writeInfoLine}: “The square of 5 is ”, squareNumber.result, “.”
1755 \#{procedure} squareNumber: .number
1756 .result = .number ^ 2
1757 \#{endproc
1758 }
1759 Another way to emulate functions is to use a variable name as an argument:
1760 {
1761 @squareNumber2: 5, “square5”
1762 \`{writeInfoLine}: “The square of 5 is ”, square5, “.”
1763 \#{procedure} squareNumber2: .number, .squareVariableName$
1764 '.squareVariableName$' = .number ^ 2
1765 \#{endproc
1766 }
1767 However, this uses variable substitution, a trick better avoided.
1768
1769 ################################################################################
1770 "Scripting 5.6. Arrays and dictionaries"
1771 © Paul Boersma 2017-07-18,2025
1772
1773 You can use arrays of numeric and string variables:
1774 {
1775 \#{for} i \#{from} 1 \#{to} 5
1776 square [i] = i * i
1777 text$ [i] = \`{mid$} (“hello”, i)
1778 \#{endfor}
1779 }
1780 After this, the variables `square[1]`, `square[2]`, `square[3]`, `square[4]`, `square[5]`,
1781 `text$[1]`, `text$[2]`, `text$[3]`, `text$[4]`, and `text$[5]` contain
1782 the values 1, 4, 9, 16, 25, “h”, “e”, “l”, “l”, and “o”, respectively:
1783 {
1784 \`{writeInfoLine}: “Some squares:”
1785 \#{for} i \#{from} 1 \#{to} 5
1786 \`{appendInfoLine}: “The square of ”, i, “ is ”, square [i]
1787 \#{endfor}
1788 }
1789
1790 In the examples above, the %index into the array was always a number.
1791 A %hash or %dictionary is an array variable where the index is a string:
1792 {
1793 age [“John”] = 36
1794 age [“Babs”] = 39
1795 \`{writeInfoLine}: “John is ”, age [“John”], “ years old.”
1796 }
1797
1798 See also
1799 ========
1800
1801 You can use any number of array and dictionary variables in a script,
1802 but for many applications, namely whenever it were useful to look at a numeric array as a single object,
1803 it may be better to use vectors and matrices (see @@Scripting 5.7. Vectors and matrices@)
1804 or to use Matrix or Sound objects.
1805
1806 ################################################################################
1807 "Scripting 5.7. Vectors and matrices"
1808 © Paul Boersma 2023-01-30
1809
1810 1. What is a vector?
1811 ====================
1812
1813 A ##numeric vector# is an array of numbers, regarded as a single object.
1814 For instance, the squares of the first five integers can be collected in the vector { 1, 4, 9, 16, 25 }.
1815 In a Praat script, you can put a vector into a variable whose name ends in a number sign (“`#`”):
1816 {
1817 squares# = { 1, 4, 9, 16, 25 }
1818 }
1819 After this, the variable %`squares#` contains the value { 1, 4, 9, 16, 25 }.
1820 We say that the vector %`squares#` has five %dimensions, i.e. it contains five numbers.
1821
1822 Whereas in @@Scripting 3.2. Numeric variables@ we talked about a numeric variable as being analogous to a house
1823 where somebody (the numeric %value) could live, a numeric vector with five dimensions
1824 can be seen as a %street that contains five houses, which are numbered with the indexes 1, 2, 3, 4 and 5,
1825 each house containing a numeric value. Thus, the street %`squares#` contains the following five houses:
1826 %`squares#` [1], %`squares#` [2], %`squares#` [3], %`squares#` [4] and %`squares#` [5].
1827 Their values (the numbers that currently live in these houses) are 1, 4, 9, 16 and 25, respectively.
1828
1829 To list the five values with a loop, you could do:
1830 {
1831 \`{writeInfoLine}: “Some squares:”
1832 \#{for} i \#{from} 1 \#{to} size (squares#)
1833 \`{appendInfoLine}: “The square of ”, i, “ is ”, squares# [i]
1834 \#{endfor}
1835 }
1836
1837 Instead of the above procedure to get the vector %`squares#`, with a pre-computed list of five squares,
1838 you could compute the five values with a formula, as in the example of @@Scripting 5.6. Arrays and dictionaries@.
1839 However, in order to put a value into an element of the vector, you have to create the vector first
1840 (i.e., you have to build the whole street before you can put something in a house),
1841 so we start by creating a vector with five zeroes in it:
1842 {
1843 squares# = zero# (5)
1844 }
1845 After this, %`squares#` is the vector { 0, 0, 0, 0, 0 }, i.e., the value of each element is zero.
1846 Now that the vector (street) exists, we can put values into (populate) the five elements (houses):
1847 {
1848 \#{for} i \#{from} 1 \#{to} size (squares#)
1849 squares# [i] = i * i
1850 \#{endfor}
1851 }
1852 After this, the variable %`squares#` has the value { 1, 4, 9, 16, 25 }, as before,
1853 but now we had the computer compute the squares.
1854
1855 2. Creating a vector
1856 ====================
1857
1858 You can create a vector in many ways. The first way we saw was with a ##vector literal#,
1859 i.e. a series of numbers (or numeric formulas) between braces:
1860 {
1861 lengths# = { 1.83, 1.795, 1.76 }
1862 }
1863
1864 The second way we saw was to create a series of #zeroes. To create a vector consisting of 10,000 zeroes, you do
1865 {
1866 longEmptyArray# = \#`{zero#} (10000)
1867 }
1868
1869 Another important type of vector is a series of random numbers.
1870 To create a vector consisting of 10,000 values drawn from a ##Gaussian distribution#
1871 with true mean 0.0 and true standard deviation 1.0, you could do
1872 {
1873 noise# = \#`{randomGauss#} (10000, 0.0, 1.0)
1874 }
1875 To create a vector consisting of 10,000 values drawn from a ##uniform distribution of real numbers#
1876 with true minimum 0.0 and true maximum 1.0, you use
1877 {
1878 a# = \#`{randomUniform#} (10000, 0.0, 1.0)
1879 }
1880 To create a vector consisting of 10,000 values drawn from a ##uniform distribution of integer numbers#
1881 with true minimum 1 and true maximum 10, you use
1882 {
1883 a# = \#`{randomInteger#} (10000, 1, 10)
1884 }
1885 To create a vector containing the integer numbers 1 through 64, you use
1886 {
1887 a# = \#`{to#} (64)
1888 }
1889 To create a vector containing the integer numbers 10 through 20, you use
1890 {
1891 a# = \#`{from_to#} (10, 20)
1892 }
1893 To create a vector containing linearly increasing (not necessarily integer) numbers from 10 through 20 in steps of 2, you use
1894 {
1895 a# = \#`{from_to_by#} (10, 20, 2)
1896 }
1897 To create five linearly increasing numbers between 0 and 10 (i.e. { 0, 2.5, 5, 7.5, 10 }), you use
1898 {
1899 a# = \#`{from_to_count#} (0, 10, 5)
1900 }
1901 To divide the range between 0 and 12 symmetrically with step 5 (i.e. { 1, 6, 11 }), you use
1902 {
1903 a# = \#`{between_by#} (0, 12, 5)
1904 }
1905 To divide the range between 0 and 10 into five equal parts and list their centres (i.e. { 1, 3, 5, 7, 9 }), you use
1906 {
1907 a# = \#`{between_count#} (0, 10, 5)
1908 }
1909 To sort the numbers in a vector (e.g. { 7.4, 1.3, 3.6 }), you use
1910 {
1911 a# = \#`{sort#} ({ 7.4, 1.3, 3.6 })
1912 \`{writeInfoLine} (a#)
1913 }
1914 To randomly shuffle the numbers in a vector (e.g. { 7.4, 1.3, 3.6 }), you use
1915 {
1916 a# = \#`{shuffle#} ({ 7.4, 1.3, 3.6 })
1917 }
1918 which can yield { 1.3, 7.4, 3.6 } or any of the five other orders of the elements.
1919
1920 Vectors can also be created by some menu commands. For instance, to get vectors representing
1921 the times and pitch frequencies of the frames in a Pitch object, you can do
1922 {;
1923 \`{selectObject}: myPitch
1924 times# = List all frame times
1925 pitches# = List values in all frames: “hertz”
1926 }
1927
1928 3. Turning a vector into a number
1929 =================================
1930
1931 For the vector defined above, you can compute the #sum of the five values as
1932 {
1933 \#`{sum} (squares#)
1934 }
1935 which gives 55. You compute the #average of the five values as
1936 {
1937 \#`{mean} (squares#)
1938 }
1939 which gives 11. You compute the ##standard deviation# of the values as
1940 {
1941 \#`{stdev} (squares#)
1942 }
1943 which gives 9.669539802906858 (the standard deviation is undefined for vectors with fewer than 2 elements).
1944 The ##center of gravity# of the distribution defined by regarding
1945 the five values as relative frequencies as a function of the index from 1 to 5 is computed by
1946 {
1947 \#`{center} (squares#)
1948 }
1949 which gives 4.090909090909091 (for a vector with five elements, the result will always be
1950 a number between 1.0 and 5.0). You compute the ##inner product# of two equally long vectors as follows:
1951 {
1952 other# = { 2, 1.5, 1, 0.5, 0 }
1953 result = \#`{inner} (squares#, other#)
1954 }
1955 which gives 1*2 + 4*1.5 + 9*1 + 16*0.5 + 25*0 = 25.
1956 The formula for this is \su__%i=1_^5 %squares[%i] * %other[%i], so that an alternative piece of code could be
1957 {
1958 result = \#`{sumOver} (i to 5, squares# [i] * other# [i])
1959 }
1960
1961 4. Converting vectors to vectors
1962 ================================
1963 {
1964 a# = squares# + 5 ; adding a number to each element of a vector
1965 assert a# = { 6, 9, 14, 21, 30 }
1966 }
1967 causes %`a#` to become the vector { 6, 9, 14, 21, 30 }.
1968 {
1969 b# = a# + { 3.14, 2.72, 3.16, -1, 7.5 } ; adding two vectors of the same length
1970 }
1971 causes %`b#` to become the vector { 9.14, 11.72, 17.16, 20, 37.5 }.
1972 {
1973 c# = b# / 2 ; dividing each element of a vector
1974 }
1975 causes %`c#` to become the vector { 4.57, 5.86, 8.58, 10, 18.75 }.
1976 {
1977 d# = b# * c# ; elementwise multiplication
1978 }
1979 causes %`d#` to become the vector { 41.7698, 68.6792, 147.2328, 200, 703.125 }.
1980
1981 A vector can also be given to a ##menu command# that returns another vector.
1982 For instance, to get a vector representing the pitch frequencies at 0.01-second intervals in a Pitch object,
1983 you can do
1984 {;
1985 \`{selectObject}: myPitch
1986 tmin = Get start time
1987 tmax = Get end time
1988 times# = \#`{between_by#} (tmin, tmax, 0.01)
1989 pitches# = List values at times: times#, “hertz”, “linear”
1990 }
1991
1992 5. What is a matrix?
1993 ====================
1994
1995 A ##numeric matrix# is a two-indexed array of numbers, regarded as a single object.
1996 In a Praat script, you can put a matrix into a variable whose name ends in two number signs (“`##`”):
1997 {
1998 confusion## = {{ 3, 6, 2 }, { 8, 2, 1 }}
1999 }
2000 After this, the variable %`confusion##` contains the value {{ 3, 6, 2 }, { 8, 2, 1 }}.
2001 We say that the matrix %`confusion##` has two %rows and three %columns, i.e. it contains six numbers.
2002
2003 Whereas a numeric vector with five dimensions could be seen (see above) as a street that contains five houses,
2004 the matrix %`confusion##` can be seen as a city district with two avenues crossed by three streets,
2005 where everybody lives on an intersection (the analogies start to get less realistic).
2006
2007 6. Creating a matrix
2008 ====================
2009
2010 You can create a matrix in many ways. The first way we saw was with a ##matrix literal#,
2011 i.e. a series of series of numbers (or numeric formulas) between nested braces.
2012
2013 The second way is as a matrix of #zeroes. To create a matrix consisting of 100 rows of 10,000 zeroes, you do
2014 {
2015 a## = \#`{zero##} (100, 10000)
2016 }
2017 After this,
2018 {
2019 \#`{numberOfRows} (a##)
2020 }
2021 is 100, and
2022 {
2023 \#`{numberOfColumns} (a##)
2024 }
2025 is 10000.
2026
2027 Another important type of matrix is one filled with random numbers.
2028 To create a matrix consisting of 100 rows of 10,000 values drawn from a ##Gaussian distribution#
2029 with true mean 0.0 and true standard deviation 1.0, you can do
2030 {
2031 noise## = \#`{randomGauss##} (100, 10000, 0.0, 1.0)
2032 }
2033
2034 You can create a matrix as the outer product of two vectors:
2035 {;
2036 m## = \#`{outer##} (u#, v#)
2037 }
2038 which is the same as
2039 {;
2040 m## = \#`{zero##} (\`{size} (u#), \`{size} (v#))
2041 \#{for} irow \#{to} \`{size} (u#)
2042 \#{for} icol \#{to} \`{size} (v#)
2043 m## [irow, icol] = u# [irow] * v# [icol]
2044 \#{endfor}
2045 \#{endfor}
2046 }
2047 or in mathematical notation
2048 ~ %m__%ij_ = %u_%i %v_%j (%i = 1..%M, %j = 1..%N)
2049
2050 where %M is the number of rows and %N is the number of columns.
2051
2052 7. Computations with matrices
2053 =============================
2054
2055 You can add matrices:
2056 {;
2057 c## = a## + b##
2058 }
2059
2060 Elementwise multiplication:
2061 {;
2062 c## = a## * b##
2063 }
2064 which does
2065 ~ %c__%ij_ = %a__%ij_ %b__%ij_ (%i = 1..%M, %j = 1..%N)
2066
2067 Matrix multiplication:
2068 {;
2069 c## = mul## (a##, b##)
2070 }
2071 which does
2072 ~ %m__%ij_ = \\su__%k=1_^K %a__%ik_ %b__%kj_ (%i = 1..%M, %j = 1..%N)
2073
2074 where %M is the number of rows of %a, %N is the number of columns of %b,
2075 and %K is the number of columns of %a, which has to be equal to the number if rows of %b.
2076
2077 Matrix-by-vector multiplication:
2078 {;
2079 v# = mul# (m##, u#)
2080 }
2081 which does
2082 ~ %v_%i = \\su__%j=1_^N %m__%ij_ %u_%j (%i = 1..%M)
2083
2084 where %M is the number of rows of %m, and %N is the number of columns of %m,
2085 which has to be equal to the dimension of %u. Also
2086 {;
2087 v# = mul# (u#, m##)
2088 }
2089 which does
2090 ~ %v_%j = \\su__%i=1_^M %u_%i %m__%ij_ (%j = 1..%N)
2091
2092 where %M is the number of rows of %m, which has to be equal to the dimension of %u,
2093 and %N is the number of columns of %m.
2094
2095 8. String vectors
2096 =================
2097
2098 You can create string vectors (also called string arrays) in the following ways:
2099 {
2100 a$# = { “hello”, “goodbye” }
2101 }
2102 creates a vector with two strings, which you can access as `a$# [1]`, which is “hello”, and `a$# [2]`, which is “goodbye”.
2103
2104 {
2105 a$# = \#`{empty$#} (10)
2106 }
2107 creates a vector with 10 empty strings, which you can access as `a$# [1]` through `a$# [10]`.
2108
2109 {;
2110 text$# = \#`{readLinesFromFile$#} (“hello.txt”)
2111 }
2112 creates a vector with 100 strings if the file `hello.text` contains 100 lines of text.
2113 {;
2114 fileNames$# = \#`{fileNames$#} (“sound/*.wav”)
2115 }
2116 creates a vector containing the names of all WAV files in the folder `sound`.
2117 {;
2118 folderNames$# = \#`{folderNames$#} (“.”)
2119 }
2120 creates a vector containing the names of all folders in the folder where the script resides.
2121 {
2122 inks$# = \#`{splitByWhitespace$#} (“Hello, how are you?”)
2123 }
2124 creates a vector containing the strings “Hello,” (including the comma), “how”, “are”, and “you?”.
2125
2126 ################################################################################
2127 "Scripting 5.8. Including other scripts"
2128 © Paul Boersma 2017-07-18,2025
2129
2130 You can include other scripts within your script:
2131 {;
2132 a = 5
2133 include square.praat
2134 writeInfoLine: a
2135 }
2136 The Info window will show the result 25 if the file square.praat contains the following:
2137 {;
2138 a = a * a
2139 }
2140 The inclusion is done before any other part of the script is considered, so you can use the #form statement
2141 and all variables in it. Usually, however, you will put some procedure definitions in the include file, that is
2142 what it seems to be most useful for. Watch out, however, for using variable names in the include file:
2143 the example above shows that there is no such thing as a separate name space.
2144
2145 Note that you do not put quotes around the name of the include file.
2146 This is because the name of the include file has to be given explicitly; you cannot put it into a variable, for instance.
2147
2148 You can use full or relative file names. For instance, the file `square.praat` above is expected to be in the same
2149 folder as the script that says `include square.praat`. If you use the ScriptEditor, you will first have to save
2150 the script that you are editing before any relative file names become meaningful (this is the same as with other
2151 uses of relative file names in scripts).
2152
2153 You can “nest” include files, i.e., included scripts can include other scripts. However, relative file names
2154 are always evaluated relative to the folder of the outermost script.
2155
2156 The #include statement can only be at the start of a line: you cannot put any spaces in front of it.
2157
2158 ################################################################################
2159 "Scripting 5.9. Quitting"
2160 © Paul Boersma 2019-07-13
2161
2162 Usually, the execution of your script ends when the interpreter has executed the last line
2163 that is not within a procedure definition. However, you can also explicitly stop the script:
2164
2165 #exitScript ( )
2166 : stops the execution of the script in the normal way, i.e. without any messages to the user.
2167 Any settings (form) window is removed from the screen (unless Apply was clicked instead of OK).
2168
2169 #exitScript: %%error-message%
2170 : stops the execution of the script while sending an error message to the user.
2171 You can use the same argument list as with #writeInfoLine.
2172 Any settings (form) window will stay on the screen.
2173
2174 For an example, see @@Scripting 6.8. Messages to the user@.
2175
2176 ################################################################################
2177 "Scripting 6. Communication outside the script"
2178 © Paul Boersma 2024-11-16
2179
2180 , @@Scripting 6.1. Arguments to the script@ (form/endform, runScript)
2181 , @@Scripting 6.2. Writing to the Info window@ (writeInfoLine, appendInfoLine, appendInfo, tab\\$ )
2182 , @@Scripting 6.3. Query commands@ (Get, Count)
2183 , @@Scripting 6.4. Files@ (fileReadable, readFile, writeFile, deleteFile, createFolder)
2184 , @@Scripting 6.5. Calling system commands@ (runSystem, environment\\$ , stopwatch)
2185 , @@Scripting 6.6. Controlling the user@ (pause, beginPause/endPause, chooseReadFile\\$ )
2186 , @@Scripting 6.7. Sending a message to another program@
2187 , @@Scripting 6.8. Messages to the user@ (exitScript, assert, nowarn, nocheck)
2188 , @@Scripting 6.9. Calling from the command line
2189
2190 ################################################################################
2191 "Scripting 6.1. Arguments to the script"
2192 © Paul Boersma xx,2023,2024
2193
2194 You can cause a Praat script to prompt for arguments. The file `playSine.praat` may contain the following:
2195 {;
2196 \#{form}: “Play a sine wave”
2197 \#{positive}: “Sine frequency (Hz)”, “377.0”
2198 \#{positive}: “Gain (0..1)\", “0.3 (= not too loud)”
2199 \#{endform}
2200 Create Sound as pure tone: “sine” + string$ (sine_frequency),
2201 ... 1, 0, 1, 44100, sine_frequency, gain, 0.01, 0.01
2202 Play
2203 Remove
2204 }
2205
2206 When running this script, the interpreter puts a settings window (%form) on your screen,
2207 entitled “Play a sine wave”,
2208 with two fields, titled “Sine frequency (Hz)” and “Gain”, that have been provided
2209 with the standard values “377.0” and “0.3 (= not too loud)”, which you can change before clicking #OK.
2210
2211 Inside the script, the field names can be accessed as variables: these have underscores
2212 instead of spaces, and the parentheses (Hz) have been chopped off. Note that the first
2213 letter of these variables is converted to lower case, so that you can assign to them in your script.
2214
2215 Inside the script, the value “0.3 (= not too loud)” will be known as 0.3,
2216 because this is a numeric field.
2217
2218 You can use the following field types in your forms:
2219
2220 #`real`: %`variable$`, %`initialValue$`
2221 : for real numbers.
2222
2223 #`positive`: %`variable$`, %`initialValue$`
2224 : for positive real numbers: the form issues an error message if the number
2225 that you enter is negative or zero (further on in the script,
2226 you can freely change it to any real number, including negatives ones such as -1.5, or zero).
2227
2228 #`integer`: %`variable$`, %`initialValue$`
2229 : for whole numbers: the form reads the number as an integer
2230 (further on in the script, you can give it any real value, e.g. 3.14).
2231
2232 #`natural`: %`variable$`, %`initialValue$`
2233 : for positive whole numbers: the form issues an error message if the number
2234 that you enter is negative or zero (further on in the script, you can give it any real value).
2235
2236 #`word`: %`variable$`, %`initialValue$`
2237 : for a string without spaces: the form only reads up to the first space (“oh yes” becomes “oh”;
2238 further on in the script, you can give the string any value, perhaps with spaces in it).
2239
2240 #`sentence`: %`variable$`, %`initialValue$`
2241 : for any short string.
2242
2243 #`text`: %`variable$`, %`initialValue$`
2244 : for any possibly long string (the variable name will show up above the field).
2245
2246 #`text`: %`numberOfLines`, %`variable$`, %`initialValue$`
2247 : for any possibly very long string (multiple lines), %`numberOfLines` can be between 1 and 33.
2248
2249 #`boolean`: %`variable$`, %`initialValue`
2250 : a check box will be shown; the initial value is 1 (on) or 0 (off).
2251
2252 #`boolean`: %`variable$`, %`initialValue$`
2253 : a check box will be shown; to switch it on,
2254 set the initial value to `“on”`, `“yes”`, `“ON”`, `“YES”`, `“On”` or `“Yes”`;
2255 to switch it off, set it to `“off”`, `“no”`, `“OFF”`, `“NO”`, `“Off”` or `“No”`.
2256
2257 #`choice`: %`variable$`, %`initialValue`
2258 : a multiple-choice box (or “radio box”) will be shown; the value is 1 or higher. This is followed by a series of:
2259
2260 #`option`: %`text$`
2261 : an option button in a multiple-choice box (see example below).
2262
2263 #`optionmenu`: %`variable$`, %`initialValue`
2264 : a multiple-choice menu header will be shown; the value is 1 or higher.
2265 Just as `choice`, this is followed by:
2266
2267 #`option`: %`text$`
2268 : an option in a multiple-choice menu (see example below).
2269
2270 #`comment`: %`text$`
2271 : a line with any text.
2272
2273 #`infile`: %`variable$`, %`initialValue$`
2274 : for a full path to an existing file, usually for reading.
2275
2276 #`outfile`: %`variable$`, %`initialValue$`
2277 : for a full path to a new file, usually for saving.
2278
2279 #`folder`: %`variable$`, %`initialValue$`
2280 : for a full path to a folder.
2281
2282 #`realvector`: %`variable$`, %`format$`, %`initialValue$`
2283 : for a vector with real values. The format can be `“(whitespace-separated)”` or `“(formula)”`;
2284 the initial value should then probably be something like `“10 -9 80”` or `“{ 10, -9, 80 }”`, respectively.
2285
2286 #`realvector`: %`numberOfLines`, %`variable$`, %`format$`, %`initialValue$`
2287 : use this if you want a field with less or more than the standard 7 lines.
2288
2289 #`positivevector`: %`variable$`, %`format$`, %`initialValue$`
2290
2291 #`positivevector`: %`numberOfLines`, %`variable$`, %`format$`, %`initialValue$`
2292 : use either of these if you want to check that all initial elements are positive real numbers.
2293
2294 #`integervector`: %`variable$`, %`format$`, %`initialValue$`
2295
2296 #`integervector`: %`numberOfLines`, %%variable\\$ %, %%format$ %, %%initialValue$ %
2297 : use either of these if you want to check that all initial elements are whole numbers.
2298
2299 #`naturalvector`: %`variable$`, %`format$`, %`initialValue$`
2300
2301 #`naturalvector`: %`numberOfLines`, %`variable$`, %`format$`, %`initialValue$`
2302 : use either of these if you want to check that all initial elements are positive whole numbers.
2303
2304 Inside the script, strings are known as string variables, numbers as numeric variables. Consider the following form:
2305 {;
2306 \#{form}: “Sink it”
2307 \#{sentence}: “Name of the ship”, “Titanic”
2308 \#{real}: “Distance to the iceberg (m)”, “500.0”
2309 \#{natural}: “Number of people”, “1800”
2310 \#{natural}: “Number of boats”, “10”
2311 \#{endform}
2312 }
2313 In the script following this form, the variables will be known as `name_of_the_ship$`, `distance_to_the_iceberg`,
2314 `number_of_people`, and `number_of_boats`.
2315
2316 The variable associated with a radio box will get a numeric as well as a string value:
2317 {;
2318 \#{form}: “Fill attributes”
2319 \#{comment}: “Choose any colour and texture for your paintings”
2320 \#{choice}: “Colour”, 5
2321 \#{option}: “Dark red”
2322 \#{option}: “Sea green”
2323 \#{option}: “Navy blue”
2324 \#{option}: “Canary yellow”
2325 \#{option}: “Black”
2326 \#{option}: “White”
2327 \#{choice}: \"Texture”, 1
2328 \#{option}: “Smooth”
2329 \#{option}: “Rough”
2330 \#{option}: “With holes”
2331 \#{endform}
2332 \`{writeInfoLine}: “You chose the colour ”, colour$, “ and the texture ”, texture$, “.”
2333 }
2334 This shows two multiple-choice boxes. In the Colour box, the fifth button (Black) is the standard value here.
2335 If you click “Navy blue” and then #%OK, the variable %`colour` will have the value 3,
2336 and the variable %`colour$` will have the value “Navy blue”.
2337 So you can test the value of the Colour box in either of the following ways:
2338 {;
2339 if colour = 4
2340 }
2341 or
2342 {;
2343 if colour$ = “Canary yellow\"
2344 }
2345
2346 The field type #`optionmenu` is completely analogous to #`choice`,
2347 but uses up much less space on the screen:
2348 {;
2349 \#{form}: “Fill attributes”
2350 \#{comment}: “Choose any colour and texture for your paintings”
2351 \#{optionmenu}: “Colour”, 5
2352 \#{option}: “Dark red”
2353 \#{option}: “Sea green”
2354 \#{option}: “Navy blue”
2355 \#{option}: “Canary yellow”
2356 \#{option}: “Black”
2357 \#{option}: “White”
2358 \#{optionmenu}: “Texture”, 1
2359 \#{option}: “Smooth”
2360 \#{option}: “Rough”
2361 \#{option}: “With holes”
2362 \#{endform}
2363 \`{writeInfoLine}: “You chose the colour ”, colour$, “ and the texture ”, texture$, “.”
2364 }
2365
2366 For the vector types #`realvector`, #`positivevector`, #`integervector` and #`naturalvector`
2367 you have to specify an initial format, which will be shown to the user:
2368 {;
2369 \#{form}: “Interesting times”
2370 \#{comment}: “List the times that you are interested in”
2371 \#{realvector}: “Fixed times (s)”, “(whitespace-separated)”, “0.5 1.7 2.8”
2372 \#{realvector}: “Random times (s)”, “(formula)”, “randomUniform# (5, 0.0, 1.0)”
2373 \#{endform}
2374 \`{writeInfoLine}: “Your fixed times are ”, fixed_times#, “ and your random times are ”, random_times#, “.”
2375 }
2376 Clicking OK without editing the two fields may print
2377 `
2378 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.
2379 `
2380
2381 The field types #infile, #outfile and #folder always yield a full path.
2382 Consider the script `playFile.praat`, which contains the following:
2383 {;
2384 \#{form}: “Play file”
2385 \#{infile}: “File to play”, “hello.wav”
2386 \#{endform}
2387 \`{writeInfoLine}: “You chose the file ”, file_to_play$, “.”
2388 Read from file: file_to_play$
2389 Play
2390 Remove
2391 }
2392 If you just click OK and `playFile.praat` is in the folder `/Users/miep/research/usefulScripts`,
2393 then this will print
2394 `
2395 You chose the file /Users/miep/research/usefulScripts/hello.wav.
2396 `
2397 into the Info window, and play the sound in that file.
2398
2399 You can combine two short fields into one by using #`left` and #`right`:
2400 {;
2401 \#{form}: “Get duration”
2402 \#{natural}: “left Year range”, “1940”
2403 \#{natural}: “right Year range”, “1945”
2404 \#{endform}
2405 duration = right_Year_range - left_Year_range
2406 \`{writeInfoLine}: “The duration is ”, duration, “ years.”
2407 }
2408 The interpreter will only show the single text “Year range”, followed by two small text fields.
2409
2410 Calling a script from another script
2411 ====================================
2412
2413 Scripts can be nested: the file `doremi.praat` may contain the following:
2414 {;
2415 \#{runScript}: “playSine.praat”, 550, 0.9
2416 \#{runScript}: “playSine.praat”, 615, 0.9
2417 \#{runScript}: “playSine.praat”, 687, 0.9
2418 }
2419
2420 With #`runScript`, Praat will not display a form window, but simply execute the script
2421 with the two arguments that you supply on the same line (e.g. 550 and 0.9).
2422
2423 Values for #`choice` must be passed as strings:
2424 {;
2425 \#{runScript}: “fill attributes.praat”, “Navy blue”, “With holes”
2426 }
2427
2428 Values for vectors can be passed either as a vector expression or as a white-space separated string,
2429 independently of the initial format:
2430 {;
2431 \#{runScript}: “interesting times.praat”, { 0.3, 0.5, 0.7, 2.0 }, “18000 0.3”
2432 }
2433
2434 You can pass values for #boolean either as the quoted strings “yes” and “no” (or their variants) or as the unquoted numbers 1 and 0.
2435
2436 In #`runScript`, the path to the external script, as well as the paths to #`infile`, #`outfile` and #`folder` parameters
2437 are taken relative to the folder of the current script. For instance, suppose that the current script is
2438 `/Users/miep/research/project19/analyse.praat` and contains:
2439 {;
2440 \#{runScript}: “../usefulScripts/playFile.praat”, “sounds/sound3.wav”
2441 }
2442 then running the current script will run the above-mentioned script `/Users/miep/research/usefulScripts/playFile.praat`,
2443 which will play the file `/Users/miep/research/project19/sounds/sound3.wav`.
2444
2445 ################################################################################
2446 "Scripting 6.2. Writing to the Info window"
2447 © Paul Boersma 2022-12-02,2025
2448
2449 With the @Info button and several commands in the @@Query submenu@ (or with query commands in menus in the editors),
2450 you write to the @@Info window@ (if your program is run from the command line,
2451 the text goes to the console window or to %stdout instead; see @@Scripting 6.9. Calling from the command line|\\SS6.9).
2452
2453 The commands @`writeInfo`, @`writeInfoLine`, @`appendInfo` and @`appendInfoLine`
2454 allow you to write to the Info window from a script. Those with #`write` in their name clear the Info window
2455 before they write to it, those with #`append` in their name do not. Those with #`Line` in their name make sure
2456 that a following #`appendInfo` or #`appendInfoLine` will write on the next line.
2457
2458 These four functions take a variable number of numeric and/or string arguments, separated by commas.
2459 Here is an example:
2460 {
2461 \`{writeInfoLine}: “Pitch extrema:”
2462 Create Sound as pure tone: “sine”, 1, 0, 0.1, 44100, 377, 0.2, 0.01, 0.01
2463 To Pitch: 0.01, 75, 600
2464 minimum = Get minimum: 0, 0, “hertz”, “parabolic”
2465 \`{appendInfoLine}: "Minimum: ", minimum, " Hz"
2466 maximum = Get maximum: 0, 0, “hertz”, “parabolic”
2467 \`{appendInfoLine}: "Maximum: ", maximum, " Hz"
2468 }
2469
2470 The following script builds a table with statistics about a pitch contour:
2471 {
2472 \`{writeInfoLine}: “Minimum”, tab$, “Maximum”
2473 \`{appendInfo}: fixed$ (minimum, 3)
2474 \`{appendInfo}: tab$
2475 \`{appendInfo}: fixed$ (maximum, 3)
2476 \`{appendInfoLine}: “”
2477 }
2478 The little string #`tab$` is a %tab character; it allows you to create
2479 table files that can be read by some spreadsheet programs. The little string #`newline$` is a %newline character;
2480 it moves the following text to the next line.
2481
2482 You could combine the last four print statements into:
2483 {
2484 \`{appendInfoLine}: fixed$ (minimum, 3), tab$, fixed$ (maximum, 3)
2485 }
2486 which is the same as:
2487 {
2488 \`{appendInfo}: fixed$ (minimum, 3), tab$, fixed$ (maximum, 3), newline$
2489 }
2490
2491 To clear the Info window, you can do
2492 {
2493 \`{writeInfo}: “”
2494 }
2495 or
2496 {
2497 \`{clearinfo}
2498 }
2499
2500 ################################################################################
2501 "Scripting 6.3. Query commands"
2502 © Paul Boersma 2014-01-07
2503
2504 If you click the ##Get mean...# command for a Pitch object,
2505 the Info window will contain a text like “150 Hz” as a result. In a script, you would rather
2506 have this result in a variable instead of in the Info window. The solution is simple:
2507 {;
2508 mean = Get mean: 0, 0, “Hertz”, “Parabolic”
2509 }
2510 The numeric variable %mean now contains the number 150. When assigning to a numeric variable,
2511 the interpreter converts the part of the text before the first space into a number.
2512
2513 You can also assign to string variables:
2514 {;
2515 mean$ = Get mean: 0, 0, “Hertz\", “Parabolic”
2516 }
2517 The string variable %`mean$` now contains the entire string “150 Hz”.
2518
2519 This works for every command that would otherwise write into the Info window.
2520
2521 ################################################################################
2522 "Scripting 6.4. Files"
2523 © Paul Boersma 2020-12-29
2524
2525 You can read from and write to text files from a Praat script.
2526
2527 Reading a file
2528 ==============
2529
2530 You can check the availability of a file for reading with the function
2531 {;
2532 \#`{fileReadable} (\%{fileName$})
2533 }
2534 which returns 1 (true) if the file exists and can be read, and 0 (false) otherwise.
2535 Note that %`fileName$` is taken relatively to the folder where the script is saved;
2536 for instance, if your script is in the folder `Paolo/project1`, then the file name
2537 “hello.wav” refers to `Paolo/project1/hello.wav`, the file name “yesterday/hello.wav”
2538 refers to `Paolo/project1/yesterday/hello.wav`, and the file name “../project2/hello.wav”
2539 refers to `Paolo/project2/hello.wav` (“..” goes one folder up).
2540 You can also use full path names such as `C:/Users/Paolo/project1/hello.wav`
2541 on Windows and `/Users/Paolo/project1/hello.wav` on the Mac.
2542
2543 To read the contents of an existing text file into a string variable or into a numeric variable, you use
2544 {;
2545 text$ = \#`{readFile$} (“myFile.txt”)
2546 }
2547 or
2548 {;
2549 number = \#`{readFile} (“myFile.txt”)
2550 }
2551 If the file does not exist, the script terminates with an error message.
2552
2553 Example: reading a settings file
2554 ================================
2555
2556 Suppose that the file `height.inf` may contain an appropriate value for a numeric variable
2557 called `height`, which we need to use in our script. We would like to read it with
2558 {;
2559 height = \#`{readFile} (“height.inf”)
2560 }
2561
2562 However, this script will fail if the file `height.inf` does not exist. To guard
2563 against this situation, we could check the existence of the file, and supply a default
2564 value in case the file does not exist:
2565 {;
2566 fileName$ = “height.inf”
2567 \#{if} \#`{fileReadable} (fileName$)
2568 height = \#`{readFile} (fileName$)
2569 \#{else}
2570 height = 180
2571 \#{endif}
2572 }
2573
2574 Writing a file
2575 ==============
2576
2577 You write into a new text file just as you write into the Info window:
2578 {;
2579 \#`{writeFileLine}: “myFile.txt”, “The present year is ”, 2000 + 13, “.”
2580 }
2581 and likewise you use @`writeFile` if you don't want a newline symbol at the end of the file.
2582 If the file cannot be created, the script terminates with an error message.
2583
2584 To append text at the end of an existing file, you use
2585 {;
2586 \#`{appendFileLine}: “myFile.txt”, “Next year it will be ”, 2000 + 14, “.”
2587 }
2588
2589 With @`appendFileLine` (and @`appendFile`, which does not add the newline),
2590 we follow the rule that if the file does not yet exist, it is created first.
2591
2592 You can create a folder (directory) with
2593 {;
2594 \#`{createFolder}: \%{folderPath$}
2595 }
2596 where, as with file names, %`folderPath$` can be relative to the folder of the script
2597 (e.g. “`data`”, or “`yesterday/data`”, or “`../project2/yesterday/data`”)
2598 or an absolute path (e.g. `C:/Users/Paolo/project1/yesterday/data` on Windows
2599 or “/Users/Paolo/project1/yesterday/data” on the Mac).
2600 If the folder already exists, this command does nothing.
2601
2602 You can delete an existing file with the function
2603 {;
2604 \#`{deleteFile}: \%{fileName$}
2605 }
2606 If the file does not exist, this command does nothing.
2607
2608 Example: writing a table of squares
2609 ===================================
2610
2611 Suppose that we want to create a file with the following 100 lines of text:
2612 `
2613 The square of 1 is 1
2614 The square of 2 is 4
2615 The square of 3 is 9
2616 ...")
2617 The square of 100 is 10000
2618 `
2619
2620 We can do this by appending 100 lines:
2621 {;
2622 \#`{deleteFile}: “squares.txt”
2623 \#{for} i \#{to} 100
2624 \#`{appendFileLine}: “squares.txt”, “The square of ”, i, “ is ”, i * i
2625 \#{endfor}
2626 }
2627 Note that we delete the file before appending to it,
2628 in order that we do not append to an already existing file.
2629
2630 You can append the contents of the Info window to a file with
2631 {;
2632 \#`{appendFile}: “out.txt”, info$ ( )")
2633 }
2634
2635 Folder listings
2636 ===============
2637
2638 To get the names of the files if a certain type in a certain folder, use
2639 {;
2640 fileNames$# = \#`{fileNames$#}: \%{path$}
2641 }
2642
2643 For instance, to read in all the sound files in a specified folder,
2644 you could use the following script:
2645 {;
2646 folder$ = “/usr/people/miep/sounds”
2647 soundFileNames$# = \#`{fileNames$#} (folder$ + “/*.wav”)
2648 \#{for} ifile \#{to} \`{size} (soundFileNames$#)
2649 \@{Read from file:} folder$ + “/” + soundFileNames$# [ifile]
2650 \#{endfor}
2651 }
2652
2653 Alternative syntax
2654 ==================
2655
2656 If, on the basis of the syntax of commands and functions in earlier sections you expected that
2657 {;
2658 text$ = \#`{readFile$} (“myFile.txt”)
2659 number = \#`{readFile} (“myFile.txt”)
2660 }
2661 could be written as
2662 {;
2663 text$ = \#`{readFile$}: “myFile.txt”
2664 number = \#`{readFile}: “myFile.txt”
2665 }
2666 then you are right. The syntax with the colon is equivalent to the syntax with the two parentheses. Conversely, instead of
2667 {;
2668 \#`{deleteFile}: \%{fileName$}
2669 }
2670 you can also write
2671 {;
2672 \#`{deleteFile} (\%{fileName$})
2673 }
2674
2675 ################################################################################
2676 "Scripting 6.5. Calling system commands"
2677 © Paul Boersma 2020,2023,2025
2678
2679 Running a command-line-like command
2680 ===================================
2681 From a Praat script you can call system commands.
2682 These are the same commands that you would normally type into a terminal window or into the Windows command line prompt.
2683 The syntax is the same as that of the @`writeInfo` command.
2684
2685 Most system commands are different on different platforms.
2686 For instance, to throw away all WAV files in the folder whose path (relative to the script’s folder) is
2687 in the variable `folder$` (and you are VERY SURE that this name contains NO SPACES OR SPECIAL CHARACTERS),
2688 you could write
2689 {;
2690 \#`{runSystem}: "del ", folder$, "\*.wav" ; DANGEROUS
2691 }
2692 on Windows, but
2693 {;
2694 \#`{runSystem}: "rm ", folder$, "/*.wav" ; DANGEROUS
2695 }
2696 on Macintosh and Linux.
2697
2698 The script will stop running if a system command returns an error. For instance,
2699 {;
2700 \#`{runSystem}: "rm ", folder$, "/*.wav" ; DANGEROUS
2701 }
2702 will stop the script if there are no WAV files in the folder,
2703 with a message like “No such file or directory”.
2704
2705 In order to prevent this, you can tell Praat to ignore the return value of \@{runSystem}.
2706
2707 Thus, to make sure that the folder contains no WAV files, you would write
2708 {;
2709 \#{runSystem_nocheck}: "rm ", folder$, "/*.wav" ; DANGEROUS
2710 }
2711
2712 Typically, however, the use of @`runSystem` or #`runSystem_nocheck` is DANGEROUS:
2713 if the string `folder$` contains spaces and semicolons and slashes, for instance,
2714 you may lose all files on your computer or install malware without noticing it;
2715 you really have to have full control over your files and know exactly what you are doing
2716 before you use @`runSystem` or #`runSystem_nocheck`.
2717
2718 A safer way to call other programs
2719 ==================================
2720 The above is dangerous because `runSystem` has no concept of what an argument to a program is.
2721 For instance, what if `folder$` above is somehow “-rf .*; ls”? Will that remove my whole folder structure?
2722 A much safer way is to use @`runSubprocess`, in which you supply the arguments directly:
2723 {;
2724 \#`{runSubprocess}: "/usr/local/sbin/theOtherApp", folder$, "*.wav"
2725 }
2726 This will hand to `theOtherApp`: the contents of the string `folder$`
2727 (without fear of executing any commands that are inside the foldername),
2728 and the string `*.wav`. What is done with the asterisk (“*”) depends on your platform:
2729 on macOS and Linux, the string is handed verbatim as “*.wav” to `theOtherApp`,
2730 whereas on Windows the string is handed verbatim if there are no WAV files (in the folder of the script)
2731 but as e.g. “hello.wav goodbye.wav” if the script folder contains those two files.
2732
2733 In general, then, @`runSubprocess` is quite predictable on macOS and Linux, but less so on Windows.
2734
2735 Getting the values of system variables
2736 ======================================
2737 #`environment$` (%`symbol-string`)
2738 : returns the value of an environment variable, e.g.
2739 {;
2740 homeFolder$ = \#`{environment$} ("HOME")
2741 }
2742
2743 Getting system duration
2744 =======================
2745 #`stopwatch`
2746 : returns the time that has elapsed since the previous #stopwatch.
2747
2748 Here is a Praat script that measures how long it takes to do a hundred thousand assignments on your computer
2749 (if you are reading this in Praat’s own Help, not on the web):
2750 {
2751 \#`{stopwatch}
2752 for i to 100000
2753 a = 1.23456789e123
2754 endfor
2755 time = \#`{stopwatch}
2756 \`{writeInfoLine}: a, " ", fixed$ (time, 3)
2757 }
2758 How many nanoseconds is that per assignment?
2759 {
2760 \`{writeInfoLine}: round (time / 100000 * 1e9)
2761 }
2762 )~~~"
2763 MAN_PAGES_ENDcxx . . $ `sys/ManPagesM.h:28:9`!
2764
2765 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 6.6. Controlling the user", U"ppgb", 20230130)
2766 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"You can temporarily halt a Praat script:")
2767 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"#`pauseScript`: %message")
2768 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"suspends execution of the script, and allows the user to interrupt it. "
2769 "A message window will appear with the %message (you can use the same argument list as with @`writeInfoLine`) and the buttons #Stop and #Continue:")
2770 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{pauseScript}: “The next file will be ”, fileName$")
2771 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The #`pauseScript` function is useful if you want to send a simple message to the user, "
2772 "and you only want to ask the user whether she wants to proceed or not. "
2773 "More interesting interactions between your script and the user are possible with the ##%%pause window%#. "
2774 "In a pause window you can include the same kinds of arguments as in a @@Scripting 6.1. Arguments to the script|form@. "
2775 "Here is an extensive example:")
2776 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: “script”")
2777 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"compression = 1")
2778 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"number_of_channels = 2")
2779 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"worth = 3")
2780 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to 5")
2781 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\\#{beginPause}: “Hi”")
2782 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{comment}: “Type a lot of nonsense below.”")
2783 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{natural}: “Number of people”, 10")
2784 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{real}: “Worth”, worth + 1")
2785 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{positive}: “Sampling frequency (Hz)”, “44100.0 (= CD quality)”")
2786 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{word}: “hi”, “hhh”")
2787 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{sentence}: “lo”, “two words”")
2788 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{text}: “shortText”, “some one-line text here”")
2789 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{text}: 2, “longText”, “some scrollable text here, within a height of 2 lines”")
2790 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{boolean}: “You like it?”, 1")
2791 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{infile}: “Input file”, “hello.wav”")
2792 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{outfile}: 2, “Output file”, “../out.txt”")
2793 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{realvector}: 2, “Array of reals”, “(whitespace-separated)”, “20 80 60”")
2794 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"if worth < 6")
2795 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"\\#{choice}: “Compression”, compression")
2796 CODE4cxx . . $ `sys/ManPagesM.h:54:9`! (U"\\#{option}: “lossless (FLAC)”")
2797 CODE4cxx . . $ `sys/ManPagesM.h:54:9`! (U"\\#{option}: “MP3”")
2798 CODE4cxx . . $ `sys/ManPagesM.h:54:9`! (U"\\#{option}: “Ogg”")
2799 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"endif")
2800 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{optionmenu}: “Number of channels”, number_of_channels")
2801 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"\\#{option}: “mono”")
2802 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"\\#{option}: “stereo”")
2803 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"\\#{option}: “quadro”")
2804 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{comment}: “Then click Stop or one of the continuation buttons.”")
2805 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"clicked = \\#{endPause}: “Continue”, “Next”, “Proceed”, 2")
2806 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: number_of_people, “ ”, worth, “ ”, sampling_frequency, “ ”, clicked")
2807 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Compression: ”, compression, “ (”, compression$, “)”")
2808 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Number of channels: ”, number_of_channels$")
2809 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Short text: ”, shortText$")
2810 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Input file: ”, input_file$")
2811 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: “Array: ”, array_of_reals#")
2812 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
2813 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This example uses several tricks. A useful one is seen with `number_of_channels`: "
2814 "this is at the same time the value that is passed to #`optionmenu` (and therefore determines the setting of "
2815 "the ##Number of channels# menu when the window appears) and the name of the variable in which the user’s "
2816 "chosen value of ##Number of channels# is stored (because the text “number\\_ of \\_ channels” is what you get "
2817 "by replacing the spaces in “Number of channels” with underscores and turning its first letter to lower case).")
2818 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Your own pause windows are not likely to be as rich as the above example. "
2819 "For instance, the example has three continuation buttons (the second of these is the default button, "
2820 "i.e. the button that you can “click” by pressing the Enter or Return key). "
2821 "You will often use only one continuation button, for instance")
2822 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endPause}: “Continue”, 1")
2823 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
2824 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endPause}: “Finish”, 1")
2825 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
2826 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endPause}: “OK”, 1")
2827 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If your script shows multiple different pause windows, then it is in fact a %wizard, "
2828 "and it becomes useful to have")
2829 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endPause}: “Next”, 1")
2830 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"for most of them, and")
2831 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{endPause}: “Finish”, 1")
2832 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"for the last one.")
2833 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The possibility of multiple continuation buttons can save the user a mouse click. "
2834 "The following script, for instance, requires two mouse clicks per sound:")
2835 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for %i to 20")
2836 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Read from file: “sound“ + string$ (i) + “.wav”")
2837 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Play")
2838 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Remove")
2839 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"#beginPause: “Rate the quality”")
2840 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"#comment: “How good is the sound on a scale from 1 to 7?”")
2841 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"#choice: “Quality”, 4")
2842 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “1”")
2843 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “2”")
2844 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “3”")
2845 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “4”")
2846 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “5”")
2847 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “6”")
2848 CODE3cxx . . $ `sys/ManPagesM.h:53:9`! (U"#option: “7”")
2849 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\\#{endPause}: if i = 20 then “Finish” else “Next” fi, 1")
2850 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: quality")
2851 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
2852 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The following script works faster:")
2853 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to 20")
2854 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Read from file: “sound” + string$ (i) + “.wav”")
2855 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Play")
2856 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Remove")
2857 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"#beginPause: “Rate the quality\"")
2858 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"#comment: “How good is the sound on a scale from 1 to 7?”")
2859 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"quality = \\#{endPause}: “1”, “2”, “3”, “4”, “5”, “6”, “7”, 0")
2860 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: quality")
2861 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
2862 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In this example, the 0 at the end of #endPause means that there is no default button.")
2863 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"File selection")
2864 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you want the user to choose a file name for reading (opening), do")
2865 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"fileName$ = \\#{chooseReadFile$}: “Open a table file\"")
2866 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if fileName$ <> “”")
2867 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"table = Read Table from tab-separated file: fileName$")
2868 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2869 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A file selector window will appear, with (in this example) ##Open a table file# as the title. "
2870 "If the user clicks #OK, the variable %`fileName$` will contain the name of the file that the user selected; "
2871 "if the user clicks #Cancel, the variable %`fileName$` will contain the empty string (“”).")
2872 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you want the user to choose a file name for writing (saving), do")
2873 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"selectObject: mySound")
2874 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"fileName$ = \\#{chooseWriteFile$}: “Save as a WAV file”, “mySound.wav”")
2875 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if fileName$ <> “”")
2876 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"Save as WAV file: fileName$")
2877 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2878 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A file selector window will appear, with (in this example) ##Save as a WAV file# as the title "
2879 "and “mySound.wav” as the suggested file name (which the user can change). "
2880 "If the user clicks #OK, the form will ask for confirmation if the file name that the user typed already exists. "
2881 "If the user clicks #OK with a new file name, or clicks #OK in the confirmation window, "
2882 "the variable %`fileName$` will contain the file name that the user typed; "
2883 "if the user clicks #Cancel at any point, the variable %`fileName$` will contain the empty string (“”).")
2884 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you want the user to choose a folder (directory) name, do")
2885 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"folderName$ = \\#{chooseFolder$}: “Choose a folder to save all the new files in”")
2886 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if folderName$ <> “”")
2887 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"for i to numberOfSelectedSounds")
2888 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"selectObject: sound [i]")
2889 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"Save as WAV file: folderName$ + “/sound” + string$ (i) + “.wav”")
2890 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endfor")
2891 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2892 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A folder selector window will appear, with (in this example) ##Choose a folder to save all the new files in# as the title. "
2893 "If the user clicks #OK, the variable %`folderName$` will contain the name of the folder that the user selected; "
2894 "if the user clicks #Cancel, the variable %`folderName$` will contain the empty string (“”).")
2895 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"A non-pausing pause window without a #Stop button")
2896 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Especially if you use the pause window within the @@Demo window@, you may not want to give the user the capability of "
2897 "ending the script by hitting #Stop or closing the pause window. "
2898 "In that case, you can add an extra numeric argument to #`endPause` (at the end) "
2899 "that denotes the cancel button:")
2900 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{beginPause}: “Learning settings”")
2901 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\\#{positive}: “Learning rate”, “0.01”")
2902 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\\#{choice}: “Directions”, 3")
2903 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{option}: “Forward”")
2904 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{option}: “Backward”")
2905 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"\\#{option}: “Bidirectional”")
2906 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"clicked = \\#{endPause}: “Cancel”, “OK”, 2, 1")
2907 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if clicked = 2")
2908 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"learningRate = learning_rate")
2909 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"includeForward = ( directions = 1 or directions = 3 )")
2910 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"includeBackward = ( directions = 2 or directions = 3 )")
2911 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2912 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In this example, the default button is 2 (i.e. #OK), and the cancel button is 1 (i.e. #Cancel). "
2913 "The form will now contain no #Stop button, and if the user closes the window, "
2914 "this will be the same as clicking #Cancel, namely that %`clicked` will be 1 (because the #Cancel button is the first button) "
2915 "and the variables %`learning_rate`, %`directions` and %`directions$` will not be changed (i.e. they might remain undefined).")
2916 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Pausing for a fixed time without a window")
2917 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can pause Praat for 1.3 seconds by saying")
2918 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\#{sleep} (1.3)")
2919 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This is of course not about controlling the user, "
2920 "but it is mentioned here because this section is about pausing.")
2921 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
2922
2923 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 6.7. Sending a message to another program", U"ppgb", 20241116)
2924 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To send messages to running programs that use the Praat shell, "
2925 "use $sendpraat (see @@Scripting 8. Controlling Praat from another program@).")
2926 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Security measure")
2927 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"One some platforms it used to be possible to send a message to another running program that listens to a socket, "
2928 "using the $sendsocket directive. This is no longer possible in Praat version 7.")
2929 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
2930
2931 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 6.8. Messages to the user", U"ppgb", 20230122)
2932 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If the user makes a mistake (e.g. types conflicting settings into your form window), "
2933 "you can use the #exitScript function (@@Scripting 5.9. Quitting|\\SS5.9@) "
2934 "to stop the execution of the script with an error message:")
2935 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"form: “My analysis”")
2936 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"real: “Starting time (s)”, “0.0”")
2937 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"real: “Finishing time (s)”, “1.0”")
2938 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endform")
2939 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if finishing_time <= starting_time")
2940 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\\#{exitScript}: “The finishing time should exceed ”, starting_time, “ seconds.”")
2941 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2942 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"# Proceed with the analysis...")
2943 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"For things that should not normally go wrong, you can use the #`assert` directive:")
2944 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"power = Get power")
2945 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"assert power > 0")
2946 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This is the same as:")
2947 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if (power > 0) = undefined")
2948 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"exitScript: “Assertion failed in line ”, lineNumber, “ (undefined): power > 0”")
2949 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"elsif not (power > 0)")
2950 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"exitScript: “Assertion failed in line ”, lineNumber, “ (false): power > 0”")
2951 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
2952 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can prevent Praat from issuing warning messages:")
2953 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"nowarn Save as WAV file: “hello.wav”")
2954 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This prevents warning messages about clipped samples, for instance.")
2955 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can also prevent Praat from showing a progress window:")
2956 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"pitch = noprogress To Pitch: 0, 75, 500")
2957 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This prevents the progress window from popping up during lengthy operations. "
2958 "Use this only if you want to prevent the user from stopping the execution of the script.")
2959 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Finally, you can make Praat ignore error messages:")
2960 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"nocheck Remove")
2961 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This would cause the script to continue even if there is nothing to remove.")
2962 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
2963
2964 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 6.9. Calling from the command line", U"ppgb", 20220122) // 2023 2025
2965 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Previous sections of this tutorial have shown you how to run a Praat script from the Script window. "
2966 "However, you can also call a Praat script from the command line (text console) instead. "
2967 "Information that would normally show up in the Info window, then goes to %stdout, "
2968 "and error messages go to %stderr. "
2969 "You cannot use commands in your script that create windows, such as ##View & Edit#. "
2970 "Before describing how to achieve this (from section 4 below on), we first describe "
2971 "how the normal Praat, with its usual Objects and Picture (and perhaps Info) window, "
2972 "can be started from the command line.")
2973
2974 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"1. Starting Praat from the command line")
2975 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Before seeing how a Praat script can be called from the command line, "
2976 "you should first know that just calling Praat from the command line just starts up Praat "
2977 "with its usual GUI (Graphical User Interface), i.e. with its two windows. "
2978 "For instance, on Windows you can start the Command Prompt window (the “Console”), and type")
2979 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\"")
2980 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"(including the quotes) if Praat.exe is indeed in the folder `C:\\Program Files`.")
2981 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On the Mac, the executable is hidden inside the `app` file, so you open a Terminal window "
2982 "and type something like")
2983 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat")
2984 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On Linux, you type into the Terminal something like")
2985 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat")
2986
2987 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"2. Calling Praat to open data files")
2988 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On Windows, you can open Praat with a sound file and a TextGrid file by typing")
2989 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --open data\\hello.wav data\\hello.TextGrid")
2990 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"or")
2991 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --open data/hello.wav data/hello.TextGrid")
2992 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"at least if your current folder (see the Console’s `cd` and `dir` commands) "
2993 "contains the folder `data` and that folder contains those two files. "
2994 "Praat will start up, and shows the two files as a Sound and a TextGrid object in the list. "
2995 "If Praat was already running when you typed the command, "
2996 "the two files are added as objects to the existing list in Praat.")
2997 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On the Mac, you do")
2998 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --open data/hello.wav data/hello.TextGrid")
2999 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and on Linux")
3000 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --open data/hello.wav data/hello.TextGrid")
3001 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Again, if Praat was already running when you typed the command, "
3002 "the two files are added as objects to the existing list in Praat.")
3003 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note that if you want to send messages or files to a running Praat, "
3004 "the best way (on all platforms) is to use `praat --send` (see below) "
3005 "or %sendpraat (see @@Scripting 8. Controlling Praat from another program@).")
3006 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To always start up a new instance of Praat, use #`--new-open` instead of `--open`.")
3007
3008 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"3. Calling Praat to open a script")
3009 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On Windows, when you type")
3010 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --open \"my script.praat\"")
3011 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Praat will start up, opening the script `my script.praat` in a script window. "
3012 "If Praat was already running when you typed the command, "
3013 "the script window will appear within the already running instantiation of Praat.")
3014 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On the Mac, you do")
3015 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --open \"my script.praat\"")
3016 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and on Linux")
3017 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --open \"my script.praat\"")
3018 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note that on all three platforms, you have to supply quotes around the file name "
3019 "if that file name contains one or more spaces, as here between `my` and `script` "
3020 "or above between `Program` and `Files`. This is because the script languages of "
3021 "the Console or Terminal use spaces for separating commands and arguments.")
3022
3023 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"4. Calling Praat to run a script in the background")
3024 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Now we are ready to discuss how to run Praat without a GUI.")
3025 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On Windows, when you type")
3026 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --run \"my script.praat\"")
3027 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Praat will execute the script `my script.praat` without showing Praat's GUI, "
3028 "i.e. without showing its usual two windows. "
3029 "In fact, any output that would normally go to the Info window, "
3030 "will now go directly to the Console window in which you typed the command. "
3031 "If Praat was already running when you typed the command, "
3032 "its windows will not be affected. In fact, the GUI-instantiation of Praat and the Console-instantiation "
3033 "can run simultaneously without them noticing each other’s existence; "
3034 "moreover, multiple Console-instantiations of Praat can run simultaneously, each in their own Console.")
3035 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On the Mac, you type")
3036 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --run \"my script.praat\"")
3037 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and on Linux")
3038 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --run \"my script.praat\"")
3039 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"What happens on all platforms is that the Console or Terminal starts up Praat, "
3040 "then Praat executes the script, and then Praat closes itself.")
3041
3042 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"5. Calling Praat to run a script with arguments")
3043 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Consider the following script:")
3044 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"form: \"Test command line calls\"")
3045 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"sentence: \"First text\", \"I love you\"")
3046 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"real: \"Beep duration\", \"0.4\"")
3047 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"sentence: \"Second text\", \"Me too\"")
3048 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endform")
3049 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"")
3050 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"She: \"\"\", first_text$, \"\"\"\"")
3051 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendInfoLine: \"He: \"\"\", second_text$, \"\"\"\"")
3052 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"")
3053 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"synth1 = Create SpeechSynthesizer: \"English (Great Britain)\", \"Female1\"")
3054 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play text: first_text$")
3055 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound as pure tone: \"beep\", 1, 0.0, beep_duration,")
3056 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... 44100, 440, 0.2, 0.01, 0.01")
3057 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
3058 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
3059 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"synth2 = Create SpeechSynthesizer: \"English (America)\", \"Male1\"")
3060 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play text: second_text$")
3061 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When you run this script from within Praat, it writes two lines to the Info window "
3062 "and plays first a female voice speaking the first sentence, then a beep, and then a male voice "
3063 "speaking the second sentence. To make this happen from the Windows command line instead, you type")
3064 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --run testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3065 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In the Mac terminal, you type")
3066 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --run testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3067 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and in the Linux terminal, you do")
3068 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --run testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3069 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note that each argument that contains one or more spaces has to be put within quotes, "
3070 "on all three platforms. As with #runScript, Praat will not present a form window, "
3071 "but simply run the script with the arguments given on the command line "
3072 "(see @@Scripting 6.1. Arguments to the script@). What then happens on all three platforms is that a console instantiation of Praat writes "
3073 "the two lines to the Console window and plays the three sounds.")
3074 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The path to the script file as well as to `infile`, `outfile` and `folder` "
3075 "arguments will be taken relative to the current working directory of the terminal window. For instance, "
3076 "the following example from @@Scripting 6.1. Arguments to the script@ will run the script "
3077 "`/Users/miep/research/usefulScripts/playFile.praat`, which will play the sound file "
3078 "`/Users/miep/research/project19/sounds/sound3.wav`:")
3079 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"cd /Users/miep/research/project19")
3080 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --run ../usefulScripts/playFile.praat sounds/sound3.wav")
3081
3082 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"6. Calling Praat to run a script in the GUI")
3083 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can send a script to a running Praat. Praat will then execute it:")
3084 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --send testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3085 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --send testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3086 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --send testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3087 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This works the same way as `--run`, except that `--send` runs in Praat’s Graphical User Interface. "
3088 "If Praat is already running, then that instance of Praat will execute your script. "
3089 "If Praat is not running yet, then a new GUI instance of Praat will start up and execute your script. "
3090 "To always start up a new instance of Praat, use `--new-send` instead of `--send`.")
3091 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you use")
3092 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\"C:\\Program Files\\Praat.exe\" --send-or-form testCommandLineCalls.praat")
3093 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/Applications/Praat.app/Contents/MacOS/Praat --send-or-form testCommandLineCalls.praat")
3094 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"/usr/bin/praat --send-or-form testCommandLineCalls.praat")
3095 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"then Praat will present the above form that asks for the three arguments "
3096 "(if you use `--send-or-form` with a script that has no `form` in it, it will work the same as `--send`). "
3097 "This switch is useful if you want to implement a Praat connection for interfacing with an IDE such as Microsoft\\re Visual Studio Code\\tm. "
3098 "To always start up a new instance of Praat, use `--new-send-or-form` instead of `--send-or-form`.")
3099 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"See also %sendpraat (see @@Scripting 8. Controlling Praat from another program@).")
3100
3101 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"7. Calling Praat from other programs such as Python")
3102 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can run the above script from several programming languages, not just from a Console or Terminal. "
3103 "In Python, for instance, you can do it using the same syntax as you would use in the Console or Terminal:")
3104 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"import os")
3105 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"os.system ('\"C:\\\\Program Files\\\\Praat.exe\" --run testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"')")
3106 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note that you have to double the backslashes!")
3107 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"A disadvantage of the `os.system` method is that you have to use quotes within quotes. "
3108 "A somewhat cleaner approach is:")
3109 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"import subprocess")
3110 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"subprocess.call(['C:\\\\Program Files\\\\Praat.exe', '--run', 'testCommandLineCalls.praat', 'I love you', '0.4', 'Me too'])")
3111 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"This way you specify the arguments directly, with quotes only because they are all strings, "
3112 "but without having to worry about spaces. And perhaps even more importantly, this syntax "
3113 "makes it easy to use variables as arguments, as in:")
3114 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"first_line = 'I love you'")
3115 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"second_line = 'me too'")
3116 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"subprocess.call(['C:\\\\Program Files\\\\Praat.exe', '--run', 'testCommandLineCalls.praat', first_line, '0.4', second_line])")
3117 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Many other programs beside Python have a `system`-like command, so that you can run a command like")
3118 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"system ('\"C:\\\\Program Files\\\\Praat.exe\" --run testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"')")
3119
3120 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"8. What happens if I specify neither --open nor --run nor --send?")
3121 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you specify neither `--open` nor `--run` nor `--send`, Praat's behaviour is not guaranteed. "
3122 "If you type something like")
3123 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"praat testCommandLineCalls.praat \"I love you\" 0.4 \"Me too\"")
3124 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"into a Console or Terminal window by hand, Praat will typically run the script. "
3125 "Also, the `--run` option can probably be left out from the Python call above. "
3126 "However, if you redirect the output of Praat to a file or pipe, you cannot typically leave out the `--run` option; "
3127 "if you do, Praat may start its GUI and %open the file rather than run it.")
3128 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"“Why this strange behaviour?”, you may ask. The reason is that several platforms (e.g. Windows) "
3129 "use this same mechanismm when you double-click a file, or drop a file on an application icon. "
3130 "For instance, when you double-click `hello.wav` or `doSomething.praat`, "
3131 "or drop `hello.wav` or `doSomething.praat` on the Praat icon, "
3132 "you expect Praat to %open that sound file or script, not to %run it. "
3133 "However, when you double-click or drop `doSomething.praat`, "
3134 "Windows will send Praat a message %%as if% you had typed `praat doSomething.praat` into a Console window. "
3135 "This means that in this case Praat will have to interpret `praat doSomething.praat` "
3136 "as an indication that you want to %open that file, not %run it. "
3137 "Praat will try to be a bit smart, though: when receiving the message `praat doSomething.praat` from the operating system, "
3138 "Praat will try to figure out whether you meant to %run or %open the file. "
3139 "If Praat detects that you typed `praat doSomething.praat` into the Console "
3140 "by hand, Praat will %run this script; if not, then Praat will %open this script; "
3141 "this includes double-clicking and dropping (appropriately), but may also include some indirect invocations "
3142 "such as through a shell script or redirection or pipe. "
3143 "After all, if Praat is in doubt, wrongly assuming that you want to %open the script is safer than wrongly "
3144 "assuming that you want to %run the script.")
3145
3146 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"9. Running Praat interactively from the command line")
3147 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On the Mac and Linux, you have the possibility of running the program interactively from the command line:")
3148 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"> /usr/bin/praat -")
3149 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can then type in any of the fixed and dynamic commands, and commands that handle object selection, "
3150 "such as #selectObject. This method also works in pipes:")
3151 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"> echo \"Report memory use\" | /usr/bin/praat -")
3152
3153 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"10. Calling Praat from a web server")
3154 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you call Praat from a web server, you typically do not want to read and write its preferences and buttons files. "
3155 "To achieve this, you use the #`--no-pref-files` command line option before the script name:")
3156 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"system ('/users/apache/praat --run --no-pref-files /user/apache/scripts/computeAnalysis.praat 1234 blibla')")
3157 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"On Windows, you will often want to specify #`--utf8` as well, because otherwise "
3158 "Praat will write its output to BOM-less UTF-16 files, which many programs do not understand.")
3159
3160 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"11. All command line switches and options")
3161 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Switches:")
3162 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--open")
3163 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Interpret the command line arguments as files to be opened in an existing or new GUI.")
3164 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--new-open")
3165 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Start a new GUI and interpret the command line arguments as files to be opened.")
3166 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--run")
3167 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Interpret the command line arguments as a script file name and its arguments, to run without a GUI.")
3168 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--send")
3169 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Interpret the command line arguments as a script file name and its arguments, to run in an existing or new GUI.")
3170 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--new-send")
3171 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Start a new GUI and interpret the command line arguments as a script file name and its arguments.")
3172 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--version")
3173 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Print the Praat version.")
3174 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--help")
3175 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Print this list of command line options.")
3176 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Options:")
3177 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--no-pref-files#")
3178 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Ignore the preferences file and the buttons file at start-up, and don't write them when quitting (see above).")
3179 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--no-plugins#")
3180 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Don't activate the plugins at start-up.")
3181 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--pref-dir=#`/var/www/praat_plugins`")
3182 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Set the preferences folder to `/var/www/praat_plugins` (for instance). "
3183 "This can come in handy if you require access to preference files and/or plugins that are not in your home folder.")
3184 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##-8#, ##--utf8#")
3185 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Write the output (e.g. of $writeInfo$) in UTF-8 encoding. This is the default encoding on MacOS and Linux, "
3186 "but on Windows the default is the Console’s native UTF-16 Little Endian (i.e. the Console understands UTF-16 always, "
3187 "whereas it understands UTF-8 only if you type `chcp 65001` first). "
3188 "If you pipe to Windows programs that understand UTF-8 rather than UTF-16, "
3189 "or if you want to redirect the output to a UTF-8 file, use this option.")
3190 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##-a#, ##--ansi#")
3191 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Write the output (e.g. of `writeInfo`) in ISO-Latin 1 (\"ANSI\") encoding. "
3192 "This is not recommended, because it potentially loses information (characters above U+00FF will show up as \"?\"), "
3193 "but it might be necessary if you want to use Praat in a pipe with programs "
3194 "that do understand ANSI but do not understand UTF-8 or UTF-16, "
3195 "or if you want to redirect the output to an ANSI-encoded file.")
3196 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##-u#, ##--utf16#")
3197 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Write the output (e.g. of `writeInfo`) in UTF-16 Little Endian encoding, without Byte Order Mark. "
3198 "This format is the default on Windows, "
3199 "but you can use it to write the output to a UTF-16LE-encoded file on any platform.")
3200 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--trace#")
3201 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Switch tracing on at start-up.")
3202 TERMcxx . . $ `sys/ManPagesM.h:42:9`! (U"##--hide-picture#")
3203 DEFINITIONcxx . . $ `sys/ManPagesM.h:46:9`! (U"Hide the Picture window at start-up.")
3204 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3205
3206 MAN_PAGES_BEGINcxx . . $ `sys/ManPagesM.h:27:9`!
3207 R"~~~(
3208 ################################################################################
3209 "Scripting 7. Scripting the editors"
3210 © Paul Boersma 2004,2023
3211
3212 With a Praat script, you can automate your work in the editors.
3213
3214 #Warning: if the purpose of your script is to get information about
3215 analyses (pitch, formants, intensity, spectrogram) from the Sound,
3216 we do %not advise to script the Sound editor window.
3217 It is much simpler, faster, and more reproducible to create the analyses
3218 with the commands of the @@dynamic menu@, then use the Query commands of the dynamic menu
3219 to extract information from the analyses. This also applies if you want to use a TextGrid
3220 to determine the times at which you want to query the analyses.
3221 See @@Scripting examples@.
3222
3223 , @@Scripting 7.1. Scripting an editor from a shell script@ (editor/endeditor)
3224 , @@Scripting 7.2. Scripting an editor from within@
3225
3226 ################################################################################
3227 "Scripting 7.1. Scripting an editor from a shell script"
3228 © Paul Boersma 20140526
3229
3230 From a Praat shell script, you can switch to an editor and back again:
3231 {;
3232 sound$ = "hallo"
3233 start = 0.3
3234 finish = 0.7
3235 sound = \@{Read from file:} sound$ + ".aifc"
3236 \@{View & Edit}
3237 \#`{editor}: sound
3238 Zoom: start, finish
3239 \#`{endeditor}
3240 Play
3241 }
3242 This script reads a sound file from disk, pops up an editor for the resulting object,
3243 makes this editor zoom in on the part between 0.3 and 0.7 seconds,
3244 and returns to the Praat shell to play the entire sound.
3245
3246 After @`editor` you can either give the unique id of the object, as above, or its name:
3247 {;
3248 \#`{editor}: "Sound " + sound$
3249 }
3250 ################################################################################
3251 "Scripting 7.2. Scripting an editor from within"
3252 © Paul Boersma 2023
3253
3254 This section shows how you can permanently extend the functionality of an editor.
3255
3256 As an example, consider the following problem: you want to see a graphic representation
3257 of the spectrum of the sound around the cursor position in the SoundEditor. To achieve this,
3258 follow these steps:
3259
3260 1. Create a Sound.
3261 2. View it in a SoundEditor by clicking @@View & Edit@.
3262 3. Choose ##New editor script# from the @@File menu@ in the SoundEditor. The resulting @ScriptEditor
3263 will have a name like “untitled script [Sound hallo]”.
3264 4. Type the following lines into the ScriptEditor:
3265 {;
3266 cursor = Get cursor
3267 Select: cursor - 0.02, cursor + 0.02
3268 Extract selected sound (windowed): "slice", "Kaiser2", 2, "no"
3269 \#`{endeditor}
3270 To Spectrum: "yes"
3271 \@{View & Edit}
3272 }
3273 If you choose #Run from the #Run menu in the ScriptEditor, a region of 40 milliseconds around the
3274 current cursor position in the SoundEditor will become selected. This piece will be copied to the list of objects,
3275 after applying a double Kaiser window (total length 80 ms). Thus, a Sound named “slice” will appear in the list.
3276 Subsequently, a Spectrum object also called “slice” will appear in the list, and a SpectrumEditor titled
3277 “Spectrum slice” will finally appear on your screen.
3278
3279 5. Save the script to disk, e.g. as `/us/miep/spectrum.praat`. The title of the ScriptEditor will change accordingly.
3280 6. Since you will want this script to be available in all future SoundEditors, you choose ##Add to menu...# from the #File menu.
3281 For the %Window, you specify “SoundEditor” (this is preset). For the %Menu, you may want to choose “Spectrum”
3282 instead of the preset value (“File”). For the name of the %Command, you type something like “Show spectrum at cursor”
3283 (instead of “Do it...”). Then you click #OK.
3284
3285 The command will be visible in every SoundEditor that you create from now on.
3286 To see this, close the one visible SoundEditor, select the original Sound, choose ##View & Edit# again, and inspect the #Spectrogram menu.
3287 You can now view the spectrum around the cursor just by choosing this menu command.
3288
3289 After you leave Praat and start it again, the command will continue to appear in the SoundEditor.
3290 If you don't like the command any longer, you can remove it with the @ButtonEditor, which you can start
3291 by choosing #Buttons from the #Settings submenu of the @@Praat menu@.
3292
3293 Improving your script
3294 =====================
3295 The above spectrum-viewing example has a number of disadvantages. It clutters the object list with a number
3296 of indiscriminable Sounds and Spectra called "slice", and the spectrum is shown up to the Nyquist frequency
3297 while we may just be interested in the lower 5000 Hz. Furthermore, the original selection in the SoundEditor is lost.
3298
3299 To improve the script, we open it again with ##Open editor script...# from the #File menu in the SoundEditor. After every change,
3300 we can run it with #Run from the #Run menu again; alternatively, we could save it (with #Save from the #File menu) and choose our new
3301 ##Show spectrum at cursor# button (this button will always run the version on disk, never the one viewed in a ScriptEditor).
3302
3303 To zoom in on the first 5000 Hz, we add the following code at the end of our script:
3304 {;
3305 \#`{editor}: "Spectrum slice"
3306 Zoom: 0, 5000
3307 }
3308 To get rid of the “Sound slice”, we can add:
3309 {;
3310 \#`{endeditor}
3311 \`{removeObject}: "Sound slice"
3312 }
3313 Note that @`endeditor` is needed to change from the environment of a SpectrumEditor to the environment of the object & picture windows.
3314
3315 If you now choose the ##Show spectrum at cursor# button for several cursor positions, you will notice that all those editors have the same name.
3316 To remedy the ambiguity of the line `editor Spectrum slice`, we give each slice a better name. For example, if the cursor was at
3317 635 milliseconds, the slice could be named “635ms”. We can achieve this by changing the extraction in the following way:
3318 {;
3319 milliseconds = round (cursor*1000)
3320 Extract selection sound (windowed): string$ (milliseconds) + "ms", "Kaiser2", 2, "no"
3321 }
3322 The names of the Sound and Spectrum objects will now have more chance of being unique. Two lines will have to be edited trivially.
3323
3324 Finally, we will reset the selection to the original. At the top of the script,
3325 we add two lines to remember the positions of the selection markers:
3326 {;
3327 start = Get start of selection
3328 end = Get end of selection
3329 }
3330 At the bottom, we reset the selection:
3331 {;
3332 \#`{editor}
3333 Select: start, end
3334 }
3335 Note that the #`editor` directive if not followed by the name of an editor,
3336 returns the script to the original environment.
3337
3338 The complete script is:
3339 {;
3340 start = Get start of selection
3341 end = Get end of selection
3342 cursor = Get cursor
3343 Select: cursor - 0.02, cursor + 0.02
3344 # Create a name. E.g. "670ms" means at 670 milliseconds.
3345 milliseconds = round (cursor*1000)
3346 Extract windowed selection: string$ (milliseconds) + "ms", "Kaiser2", 2, "no"
3347 \#`{endeditor}
3348 To Spectrum: "yes"
3349 View & Edit
3350 \#`{editor}: "Spectrum " + string$ (milliseconds) + "ms"
3351 Zoom: 0, 5000
3352 \#`{endeditor}
3353 \`{removeObject}: "Sound " + string$ (milliseconds) + "ms"
3354 \#`{editor}
3355 Select: start, end
3356 }
3357 This script is useful as it stands. It is good enough for safe use. For instance, if the created Sound object has the same name
3358 as an already existing Sound object, it will be the newly created Sound object that will be removed by @`removeObject`,
3359 because in case of ambiguity @`removeObject` always removes the most recently created object of that name.
3360
3361 ################################################################################
3362 "sendpraat"
3363 © Paul Boersma #1997,2000
3364
3365 See @@Scripting 8. Controlling Praat from another program@.
3366
3367 ################################################################################
3368 "Scripting 8. Controlling Praat from another program"
3369 © Paul Boersma 2000,2002,2021,2022
3370
3371 Sendpraat is a function for sending messages to a %running Praat.
3372 It is also a Windows, MacOS, or Linux console program with the same purpose.
3373
3374 As sendpraat cannot start up a new instance of Praat, you may often want to use
3375 `praat --send` instead (see @@Scripting 6.9. Calling from the command line@).
3376
3377 , @@Scripting 8.1. The sendpraat subroutine@
3378 , @@Scripting 8.2. The sendpraat program@
3379
3380 ################################################################################
3381 "Scripting 8.1. The sendpraat subroutine"
3382 © Paul Boersma 2002,2003,2005,2009,2014,2015,2021,2023,2026
3383
3384 Sendpraat can be a subroutine for sending messages to a %running Praat program.
3385
3386 C syntax
3387 ========
3388 {; C
3389 \#{sendpraat7} (long \%{timeOut}, int fullTrust, const char *\%{program}, char *\%{text});
3390 }
3391 Arguments
3392 =========
3393 %`timeOut` (MacOS and Linux only)
3394 : the number of seconds that `sendpraat` will wait for an answer
3395 before writing an error message. A %`timeOut` of 0 means that
3396 the message will be sent asynchronously, i.e., that sendpraat
3397 will return immediately without issuing any error message.
3398
3399 %`fullTrust`
3400 : a boolean (0 or 1) that determines whether the script should be allowed
3401 to save files and call system commands (1), or not (0).
3402
3403 %`program`
3404 : the name of a running program that uses the Praat shell, e.g. "Praat".
3405 The first letter may be specified as lower or upper case; it will be converted
3406 to upper case for Windows or MacOS and to lower case for Linux.
3407
3408 %`text`
3409 : the script text to be sent. Sendpraat may alter this text!
3410
3411 Example 1: killing a program
3412 ============================
3413 {; C
3414 char message [100], *errorMessage;
3415 strcpy (message, "Quit");
3416 errorMessage = \#{sendpraat7} (0, 0, "praat", message);
3417 if (errorMessage) fprintf (stderr, "%s", errorMessage);
3418 }
3419 This causes the program #Praat to quit (gracefully), because #Quit is a fixed
3420 command in one of the menus of that program.
3421 On MacOS and Linux, sendpraat returns immediately; on Windows, the %timeOut argument is ignored.
3422 The return value %errorMessage is a statically allocated string internal to sendpraat,
3423 and is overwritten by the next call to sendpraat.
3424
3425 Example 2: playing a sound file in reverse
3426 ==========================================
3427 Suppose you have a sound file whose name is in the variable %`fileName`,
3428 and you want the program #Praat, which can play sounds,
3429 to play this sound backwards.
3430 {; C
3431 char message [1000], *errorMessage;
3432 snprintf (message,1000, "Read from file: ~%s\nPlay reverse\nRemove", fileName);
3433 errorMessage = \#{sendpraat7} (3000, 0, "praat", message);
3434 }
3435 This will work because ##Play reverse# is an action command
3436 that becomes available in the dynamic menu when a Sound is selected.
3437 On Linux, sendpraat will allow #Praat at most 3000 seconds to perform this.
3438
3439 Example 3: executing a large script file
3440 ========================================
3441 Sometimes, it may be unpractical to send a large script directly to #sendpraat.
3442 Fortunately, the receiving program knows #runScript:
3443 {; C
3444 char message [100], *errorMessage;
3445 strcpy (message, "runScript: \"doAll.praat\", 20");
3446 errorMessage = \#{sendpraat7} (0, 1, "praat", message);
3447 }
3448 This causes the program #Praat to run the script `doAll.praat` with an argument of "20".
3449
3450 How to download
3451 ===============
3452 You can download the source code of the sendpraat subroutine
3453 via ##https://praat.org/sendpraat.html# or from ##https://www.fon.hum.uva.nl/praat/sendpraat.html#.
3454
3455 Instead
3456 =======
3457 Instead of using `sendpraat`, you can also just take the following simple steps in your program:
3458 1. on Linux, write the Praat script that you want to run, and save it as `~/.config/Message.txt`;
3459 2. get Praat's process id from `~/.config/pid`;
3460 3. if Praat's process id is e.g. 1178, send it a SIGUSR1 signal: `kill -USR1 1178`
3461
3462 If the first line of your script is the comment “`# 999`”, where 999 stands for the process id of your program,
3463 Praat will send your program a SIGUSR2 signal back when it finishes handling the script.
3464 If you do not want to receive such a message (if your program has no handler for it, the SIGUSR2 signal will kill your program),
3465 then do not include such a line.
3466
3467 If the second line of your script is “`# --FULL-TRUST`”
3468
3469 See also
3470 ========
3471 To start a program from the command line instead and sending it a message,
3472 you would not use #sendpraat, but instead run the program with a script file as an argument.
3473 See @@Scripting 6.9. Calling from the command line@.
3474
3475 ################################################################################
3476 "Scripting 8.2. The sendpraat program"
3477 © Paul Boersma #1997,2000,2003,2005,2014,2015,2021,2023,2026
3478
3479 Sendpraat can be a Windows console or Unix (MacOS, Linux) terminal program for sending messages to a %running Praat program.
3480
3481 Command line syntax
3482 ===================
3483 {; sh
3484 \#{sendpraat7} [\%{timeOut}] [--FULL-TRUST] \%{program} \%{message} ...
3485 }
3486 For the meaning of the arguments, see @@Scripting 8.1. The sendpraat subroutine|the sendpraat subroutine@.
3487
3488 Example 1: killing a program
3489 ============================
3490 {; sh
3491 \#{sendpraat7} 0 praat Quit
3492 }
3493 Causes the program #Praat to quit (gracefully), because #Quit is a fixed command in one of its menus.
3494 On Unix, `sendpraat` returns immediately; on Windows, you leave out the %`timeOut` argument.
3495
3496 Example 2: playing a sound file in reverse
3497 ==========================================
3498 {; sh
3499 \#{sendpraat7} 1000 praat "Read from file... hello.wav" "Play reverse" "Remove"
3500 }
3501 This works because ##Play reverse# is an action command
3502 that becomes available in the dynamic menu of the #Praat program when a Sound is selected.
3503 On Unix, `sendpraat` will allow #Praat at most 1000 seconds to perform this.
3504
3505 Each line is a separate argument. Lines that contain spaces should be put inside double quotes.
3506
3507 Example 3: drawing
3508 ==================
3509 {; sh
3510 \#{sendpraat7} als "for i from 1 to 5" "Draw circle: 0.5, 0.5, i" "endfor"
3511 }
3512 This causes the program #Als to draw five concentric circles into the Picture window.
3513
3514 Example 4: running a large script
3515 {; sh
3516 \#{sendpraat7} praat "runScript: \"doAll.praat\", 20"
3517 }
3518 This causes the program #Praat to execute the script ##doAll.praat# with an argument of "20".
3519
3520 How to download
3521 ===============
3522 You can download the sendpraat program
3523 via `https://praat.org/sendpraat.html` or from `https://www.fon.hum.uva.nl/praat/sendpraat.html`.
3524
3525 ################################################################################
3526 )~~~"
3527 MAN_PAGES_ENDcxx . . $ `sys/ManPagesM.h:28:9`!
3528
3529
3530 /*
3531 ENTRY (U"How to run a script")
3532 NORMAL (U"You can run scripts from the @ScriptEditor. If you will have to use the script very often, "
3533 "it is advisable to create a button for it in the fixed menu or in a dynamic menu. See the "
3534 "@ScriptEditor manual page.")
3535 NORMAL (U"(You can also run scripts from the command line. See @@Scripting 6.9. Calling from the command line|\\SS6.9@)")
3536 */
3537
3538 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 9. Turning a script into a stand-alone program", U"ppgb", 20201229) // 2025
3539 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"You can turn your script into a double-clickable stand-alone program by including it into Praat's #main procedure. "
3540 "If you want to try this, you should already know how to compile and link the Praat program on your computer.")
3541 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"These stand-alone programs do not show the Objects window and the Picture window; "
3542 "therefore, you will usually want to use @@Demo window@ commands in your script. Here is an example:")
3543 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"#include \"praat.h\"")
3544 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"")
3545 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"const char32 myScript [ ] = U\"\"")
3546 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\"demo Text: 0.5, \\\"centre\\\", 0.5, \\\"half\\\", \\\"Hello world\\\"\\n\"")
3547 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"\"demoWaitForInput ( )\\n\"")
3548 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U";")
3549 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"")
3550 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"int main (int argc, char *argv [ ]) {")
3551 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"praat_setStandAloneScriptText (myScript);")
3552 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"praat_init (U\"Hello\", argc, argv);")
3553 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"INCLUDE_LIBRARY (praat_uvafon_init)")
3554 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"praat_run ();")
3555 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"return 0; // obligatory")
3556 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"}")
3557 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The script in this example raises the Demo window, writes “Hello world” in the middle of the window, "
3558 "waits until the user clicks the mouse or presses a key, and then closes.")
3559 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note that Praat is distributed under the General Public License (GPL). This means that if you distribute "
3560 "a Praat-based stand-alone program, you have to make it open source under the GPL as well (version 3.0 or later).")
3561 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"See also @@Programming with Praat@.")
3562 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Details")
3563 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Your program can save its preferences in a folder of its choice, "
3564 "e.g. in #`preferencesDirectory$ + “/../GuineaPigAnalyzer”` if your program is called GuineaPigAnalyzer. "
3565 "If you want to be less conspicuous and like to use the Praat preferences folder instead, "
3566 "please use the #`apps` subfolder, in this way:")
3567 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\`{createFolder}: preferencesDirectory$ + “/apps”")
3568 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"\\`{createFolder}: preferencesDirectory$ + “/apps/GuineaPigAnalyzer”")
3569 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3570
3571 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting 10. Old functions", U"ppgb", 20140112)
3572 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"The Praat scripting language improves and changes, but old scripts should continue to work correctly. "
3573 "Here are some examples of what you can see in old scripts, and what they mean:")
3574 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The meaning of")
3575 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"echo Hello, my name is 'name$' and I am 'age' years old.")
3576 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"is")
3577 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"Hello, my name is \", name$, \" and I am \", age, \" years old.\"")
3578 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The meaning of")
3579 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw... 0 0 0 0 yes Curve")
3580 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"is")
3581 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Draw: 0, 0, 0, 0, \"yes\", \"Curve\"")
3582 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The meaning of")
3583 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Read from file... 'fileName$'")
3584 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"is")
3585 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Read from file: fileName$")
3586 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3587
3588 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"ScriptEditor", U"ppgb", 20140107)
3589 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"An aid to @@scripting@.")
3590 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The ScriptEditor is a text editor that allows you to edit, save, and run "
3591 "any @@Praat script@. You can type such a script from scratch, "
3592 "but it is sometimes easier to use the @@History mechanism@, which automatically records "
3593 "all your commands and mouse clicks, and which can paste these directly "
3594 "into a ScriptEditor.")
3595 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"To add a script as a button to a fixed or dynamic menu, "
3596 "use @@Add to fixed menu...@ or @@Add to dynamic menu...@ from the @@File menu@.")
3597 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Example 1")
3598 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In this example, we create a fixed button that will play a 0.4-second sine wave with a specified frequency.")
3599 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"First, we create a ScriptEditor by choosing @@New Praat script@ from the @@Praat menu@. "
3600 "Then, we choose @@Clear history@ from the #Edit menu in the ScriptEditor. "
3601 "We then perform some actions that will create a sine wave, play it, and remove it:")
3602 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. Choose ##Create Sound as pure tone...# from the @@New menu@ and click OK.")
3603 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. Click #Play in the dynamic menu.")
3604 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. Click the fixed #Remove button.")
3605 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"We then choose @@Paste history@ from the #Edit menu in the ScriptEditor (or type @@Keyboard shortcuts|Command-H@). "
3606 "The text will now contain at least the following lines (delete any other lines):")
3607 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound as pure tone: \"tone\", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01")
3608 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
3609 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
3610 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"We can run this script again by choosing #Run from the #Run menu (or typing @@Keyboard shortcuts|Command-R@). "
3611 "However, this always plays a sine with a frequency of 440 Hz, so we will add the variable \"Frequency\" "
3612 "to the script, which then looks like:")
3613 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"#form Play a sine wave")
3614 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"#positive Frequency")
3615 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"#endform")
3616 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound as pure tone: \"tone\", 1, 0, 0.4, 44100, frequency, 0.2, 0.01, 0.01")
3617 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
3618 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
3619 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When we choose #Run, the ScriptEditor will ask us to supply a value for the \"Frequency\" variable. "
3620 "We can now play 1-second sine waves with any frequency.")
3621 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"It is advisable to supply a standard value for each argument in your script. "
3622 "If the duration should be variable, too, the final script could look like:")
3623 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"#form Play a sine wave")
3624 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"#positive Frequency 440")
3625 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"#positive Duration 1.0")
3626 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"#endform")
3627 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound as pure tone: \"tone\", 1, 0, duration, 44100, frequency, 0.2, 0.01, 0.01")
3628 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Play")
3629 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Remove")
3630 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"When you run this script, the ScriptEditor will ask you to supply values for the two variables, "
3631 "but the values \"440\" and \"1.0\" are already visible in the form window, "
3632 "so that you will get a sensible result if you just click #OK.")
3633 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If this script is useful to you, you may want to put a button for it in the @@New menu@, "
3634 "in the ##Sound# submenu:")
3635 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. Save the script to a file, with #Save from the #File menu. The file name that you supply, will "
3636 "be shown in the title bar of the ScriptEditor window.")
3637 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. Choose @@Add to fixed menu...@ from the #File menu. Supply #Objects for the %window, "
3638 "#New for the %menu, \"Play sine wave...\" for the %command, "
3639 "##Create Sound from formula...# for %%after command%, and \"1\" for the depth (because it is supposed to be in a submenu); "
3640 "the %script argument has already been set to the file name that you supplied in step 1.")
3641 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. Click #OK and ensure that the button has been added in the @@New menu@. This button will still be there "
3642 "after you leave the program and enter it again; to remove it from the menu, use the @ButtonEditor.")
3643 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Example 2")
3644 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"In this example, we will create a shortcut for the usual complex pitch-analysis command.")
3645 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"First, we perform the required actions:")
3646 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. Select a Sound object.")
3647 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. Click ##To Pitch...# and set the arguments to your personal standard values.")
3648 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. Click #OK. A new #Pitch object will appear.")
3649 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"We then paste the history into the ScriptEditor, after which this will contain at least a line like (delete all the other lines):")
3650 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Pitch: 0.01, 150, 900")
3651 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can run this script only after selecting one or more Sound objects.")
3652 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If this script is useful to you, you may want to put a button for it in the dynamic menu:")
3653 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"1. Save the script to a file, with #Save from the #File menu.")
3654 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"2. Choose @@Add to dynamic menu...@ from the #File menu. Supply \"Sound\" for %class1 "
3655 "(because the button is supposed to be available only if a Sound is selected), \"0\" for %number1 "
3656 "(because the command is supposed to work for any number of selected Sound objects), "
3657 "\"To Pitch (child)\" for the %command, "
3658 "\"To Spectrum\" for %%after command%, and \"0\" for the depth (because it is not supposed to be in a submenu); "
3659 "the %script argument has already been set to the file name that you supplied in step 1.")
3660 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"3. Click #OK and ensure that the button is clickable if you select one or more Sound objects. "
3661 "This button will still be available after you leave the program and enter it again; "
3662 "to remove it from the dynamic menus, use the @ButtonEditor.")
3663 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3664
3665 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Scripting examples", U"ppgb", 20040222)
3666 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"Here is a number of examples of how to use scripting in the Praat program. "
3667 "Refer to the @scripting tutorial when necessary.")
3668 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for listing time–F0 pairs")
3669 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for listing time–F0–intensity")
3670 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for listing F0 statistics")
3671 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for creating a frequency sweep")
3672 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for onset detection")
3673 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for TextGrid boundary drawing")
3674 LIST_ITEMcxx . . $ `sys/ManPagesM.h:38:9`! (U"@@Script for analysing pitch with a TextGrid")
3675 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3676
3677 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for listing time–F0 pairs", U"ppgb", 20140223)
3678 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"“I wish to have a list of time markers in one column and F0 in the other. "
3679 "Those times that have no voiced data should be represented as “.” in the F0 column.”")
3680 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"Time: Pitch:\"")
3681 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"numberOfFrames = Get number of frames")
3682 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for iframe to numberOfFrames")
3683 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"time = Get time from frame: iframe")
3684 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"pitch = Get value in frame: iframe, \"Hertz\"")
3685 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"if pitch = undefined")
3686 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"appendInfoLine: fixed$ (time, 6)")
3687 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"else")
3688 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"appendInfoLine: fixed$ (time, 6), \" \", fixed$ (pitch, 3)")
3689 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endif")
3690 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3691 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"If you want to see this in a text file, you can copy and paste from the Info window, or save the Info window, "
3692 "or add a line to the script like")
3693 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"appendFile: \"out.txt\", info$ ( )")
3694 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3695
3696 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for listing time–F0–intensity", U"ppgb", 20200912)
3697 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"I want a list of pitch and intensity values at the same times.\"")
3698 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Since @@Sound: To Pitch...@ and @@Sound: To Intensity...@ do not give values at the same times, "
3699 "you create separate pitch and intensity contours with high time resolution, then interpolate. "
3700 "In the following example, you get pitch and intensity values at steps of 0.01 seconds "
3701 "by interpolating curves that have a time resolution of 0.001 seconds.")
3702 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"sound = selected (\"Sound\")")
3703 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"tmin = Get start time")
3704 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"tmax = Get end time")
3705 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Pitch: 0.001, 75, 300")
3706 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Rename: \"pitch\"")
3707 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"selectObject: sound")
3708 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Intensity: 75, 0.001")
3709 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Rename: \"intensity\"")
3710 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"Here are the results:\"")
3711 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to (tmax-tmin)/0.01")
3712 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"time = tmin + i * 0.01")
3713 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"selectObject: \"Pitch pitch\"")
3714 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"pitch = Get value at time: time, \"Hertz\", \"linear\"")
3715 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"selectObject: \"Intensity intensity\"")
3716 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"intensity = Get value at time: time, \"cubic\"")
3717 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: fixed$ (time, 2), \" \", fixed$ (pitch, 3), \" \", fixed$ (intensity, 3)")
3718 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3719 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3720
3721 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for listing F0 statistics", U"ppgb", 20221202)
3722 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"I need to split the wave into 50 msec sections, and then for each of those sections "
3723 "get the F0 statistics. That is, for each 50 msec section of speech I want to get the average F0, "
3724 "min, max, and standard deviation.\"")
3725 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"First you create the complete pitch contour, i.e., you select the Sound and choose "
3726 "@@Sound: To Pitch...|To Pitch...@. You can then use the commands from the @@Query submenu@ in a loop:")
3727 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"startTime = Get start time")
3728 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endTime = Get end time")
3729 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"numberOfTimeSteps = (endTime - startTime) / 0.05")
3730 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \" tmin tmax mean fmin fmax stdev\"")
3731 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for step to numberOfTimeSteps")
3732 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"tmin = startTime + (step - 1) * 0.05")
3733 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"tmax = tmin + 0.05")
3734 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"mean = Get mean: tmin, tmax, \"Hertz\"")
3735 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"minimum = Get minimum: tmin, tmax, \"Hertz\", \"Parabolic\"")
3736 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"maximum = Get maximum: tmin, tmax, \"Hertz\", \"Parabolic\"")
3737 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"stdev = Get standard deviation: tmin, tmax, \"Hertz\"")
3738 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendInfoLine: fixed$ (tmin, 6), \" \", fixed$ (tmax, 6), \" \", fixed$ (mean, 2),")
3739 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"... \" \", fixed$ (minimum, 2), \" \", fixed$ (maximum, 2), \" \", fixed$ (stdev, 2)")
3740 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3741 ENTRYcxx . . $ `sys/ManPagesM.h:36:9`! (U"Notes")
3742 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"One should not cut the sound up into pieces of 50 ms and then do ##To Pitch...# on each of them, "
3743 "because Praat will not compute F0 values in the first or last 20 ms (or so) of each piece. "
3744 "This is because the analysis requires a window of 40 ms (or so) for every pitch frame. "
3745 "Instead, one typically does the analysis on the whole sound, then queries the resulting large Pitch object. "
3746 "In that way, the information loss of windowing only affects the two 20 ms edges of the whole sound.")
3747 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The example writes lines to the #Info window. If you want to write to a file instead, "
3748 "you start with something like")
3749 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"deleteFile: \"~/results/out.txt\"")
3750 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"and add lines in the following way:")
3751 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"appendFileLine: \"~/results/out.txt \", fixed$ (tmin, 6), \" \", fixed$ (tmax, 6), \" \",")
3752 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"... fixed$ (mean, 2), \" \", fixed$ (minimum, 2), \" \", fixed$ (maximum, 2), \" \",")
3753 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"... fixed$ (stdev, 2)")
3754 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3755
3756 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for creating a frequency sweep", U"ppgb", 20170904)
3757 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"I have to find a formula for a sinewave that sweeps from 1 kHz to 12 kHz in "
3758 "60 seconds while ramping the amplitude from 1 to 12 volts in the same amount of time.\"")
3759 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"The absolute amplitude in volts cannot be handled, of course, but linear crescendo is easy:")
3760 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"Create Sound from formula: \"sweep\", 1, 0, 60, 44100,")
3761 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"... ~ 0.05 * (1 + 11 * x/60) * sin (2*pi * (1000 + 11000/2 * x/60) * x)")
3762 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Note the \"/2\" in this formula. Here is the derivation of the formula:")
3763 EQUATIONcxx . . $ `sys/ManPagesM.h:57:9`! (U"%frequency (%t) = 1000 + 11000 %t / 60")
3764 EQUATIONcxx . . $ `sys/ManPagesM.h:57:9`! (U"%phase (%t) = \\in %frequency (%t) %dt = 1000 %t + 11000 (%t^2/2) / 60")
3765 EQUATIONcxx . . $ `sys/ManPagesM.h:57:9`! (U"%signal (%t) = sin (%phase (%t))")
3766 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3767
3768 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for onset detection", U"ppgb", 20140112)
3769 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"Can anybody provide me with a script that detects the onset of sound (i.e. the end of silence).\"")
3770 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"You can create an Intensity contour and look for the first frame that is above some predefined threshold:")
3771 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"To Intensity: 100, 0")
3772 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"n = Get number of frames")
3773 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to n")
3774 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"intensity = Get value in frame: i")
3775 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"if intensity > 40")
3776 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"time = Get time from frame: i")
3777 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"writeInfoLine: \"Onset of sound at: \", fixed$ (time, 3), \" seconds.\"")
3778 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"exit")
3779 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endif")
3780 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3781 NORMALcxx . . $ `sys/ManPagesM.h:37:9`! (U"Since the intensity is computed with rather long windows, the result may be 0.01 or 0.02 seconds "
3782 "before the actual start of sound.")
3783 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3784
3785 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for TextGrid boundary drawing", U"ppgb", 20140107)
3786 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"I want only the dotted lines of the textgrid marked on top of another analysis (e.g. pitch, intensity or so) "
3787 "without the labels being shown below it.\"")
3788 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"n = Get number of intervals: 1")
3789 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to n-1")
3790 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"t = Get end point: 1, i")
3791 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"One mark bottom: t, \"no\", \"no\", \"yes\"")
3792 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3793 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3794
3795 MAN_BEGINcxx . . $ `sys/ManPagesM.h:33:9`! (U"Script for analysing pitch with a TextGrid", U"ppgb", 20201229)
3796 INTROcxx . . $ `sys/ManPagesM.h:35:9`! (U"\"I want the mean pitch of every interval that has a non-empty label on tier 5.\"")
3797 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"if numberOfSelected (\"Sound\") <> 1 or numberOfSelected (\"TextGrid\") <> 1")
3798 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"exitScript: \"Please select a Sound and a TextGrid first.\"")
3799 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endif")
3800 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"sound = selected (\"Sound\")")
3801 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"textgrid = selected (\"TextGrid\")")
3802 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"writeInfoLine: \"Result:\"")
3803 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"selectObject: sound")
3804 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"pitch = To Pitch: 0.0, 75, 600")
3805 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"selectObject: textgrid")
3806 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"n = Get number of intervals: 5")
3807 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"for i to n")
3808 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"tekst$ = Get label of interval: 5, i")
3809 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"if tekst$ <> \"\"")
3810 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"t1 = Get starting point: 5, i")
3811 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"t2 = Get end point: 5, i")
3812 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"selectObject: pitch")
3813 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"f0 = Get mean: t1, t2, \"Hertz\"")
3814 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"appendInfoLine: fixed$ (t1, 3), \" \", fixed$ (t2, 3), \" \", round (f0), \" \", tekst$")
3815 CODE2cxx . . $ `sys/ManPagesM.h:52:9`! (U"selectObject: textgrid")
3816 CODE1cxx . . $ `sys/ManPagesM.h:51:9`! (U"endif")
3817 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"endfor")
3818 CODEcxx . . $ `sys/ManPagesM.h:50:9`! (U"selectObject: sound, textgrid")
3819 MAN_ENDcxx . . $ `sys/ManPagesM.h:61:9`!
3820
3821 }
3822
3823 /* End of file manual_scripting.cpp */