| 1 | /* praat_script.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2026 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 "praatP.h" |
| 20 | #include "praat_script.h" |
| 21 | #include "UiPause.h" |
| 22 | #include "DemoEditor.h" |
| 23 | |
| 24 | static integer praat_findObjectFromString (Interpreter interpreter, conststring32 string) { |
| 25 | try { |
| 26 | integer IOBJECT; |
| 27 | while (*string == U' ') |
| 28 | string ++; |
| 29 | if (*string >= U'A' && *string <= U'Z') { |
| 30 | /* |
| 31 | Find the object by its name. |
| 32 | */ |
| 33 | static MelderString buffer; |
| 34 | MelderString_copy (& buffer, string); |
| 35 | char32 *space = str32chr (buffer.string, U' '); |
| 36 | if (! space) |
| 37 | Melder_throw (U"Missing space in name."); |
| 38 | *space = U'\0'; |
| 39 | char32 *className = & buffer.string [0], *givenName = space + 1; |
| 40 | WHERE_DOWN (1) { |
| 41 | Daata object = (Daata) OBJECT; |
| 42 | if (str32equ (className, Thing_className (OBJECT)) && str32equ (givenName, object -> name.get())) |
| 43 | return IOBJECT; |
| 44 | } |
| 45 | /* |
| 46 | No object with that name. Perhaps the class name was wrong? |
| 47 | */ |
| 48 | ClassInfo klas = Thing_classFromClassName (className, nullptr); |
| 49 | WHERE_DOWN (1) { |
| 50 | Daata object = (Daata) OBJECT; |
| 51 | if (str32equ (klas -> className, Thing_className (OBJECT)) && str32equ (givenName, object -> name.get())) |
| 52 | return IOBJECT; |
| 53 | } |
| 54 | Melder_throw (U"No object with that name."); |
| 55 | } else { |
| 56 | /* |
| 57 | Find the object by its ID. |
| 58 | */ |
| 59 | double value; |
| 60 | Interpreter_numericExpression (interpreter, string, & value); |
| 61 | const integer id = (integer) value; // SMELL: rounding down |
| 62 | WHERE (ID == id) |
| 63 | return IOBJECT; |
| 64 | Melder_throw (U"No object with number ", id, U"."); |
| 65 | } |
| 66 | } catch (MelderError) { |
| 67 | Melder_throw (U"Object \"", string, U"\" does not exist."); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Editor praat_findEditorFromString (conststring32 string) { |
| 72 | integer IOBJECT; |
| 73 | while (*string == U' ') |
| 74 | string ++; |
| 75 | if (*string >= U'A' && *string <= U'Z') { |
| 76 | WHERE_DOWN (1) { |
| 77 | for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 78 | Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 79 | if (editor) { |
| 80 | Melder_assert (editor -> name); |
| 81 | const char32 *space = str32chr (editor -> name.get(), U' '); // editors tend to be called like "3. Sound kanweg" |
| 82 | if (space) { // but not all |
| 83 | conststring32 name = space + 1; |
| 84 | if (str32equ (name, string)) |
| 85 | return editor; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } else { |
| 91 | WHERE_DOWN (1) { |
| 92 | for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 93 | Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 94 | if (editor && str32equ (editor -> name.get(), string)) |
| 95 | return editor; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | Melder_throw (U"Editor \"", string, U"\" does not exist."); |
| 100 | } |
| 101 | |
| 102 | Editor praat_findEditorById (integer id) { |
| 103 | integer IOBJECT; |
| 104 | WHERE (1) { |
| 105 | if (ID == id) { |
| 106 | for (int ieditor = 0; ieditor < praat_MAXNUM_EDITORS; ieditor ++) { |
| 107 | Editor editor = theCurrentPraatObjects -> list [IOBJECT]. editors [ieditor]; |
| 108 | if (editor) |
| 109 | return editor; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | Melder_throw (U"Editor ", id, U" does not exist."); |
| 114 | } |
| 115 | |
| 116 | static int parseCommaSeparatedArguments (Interpreter interpreter, char32 *arguments, structStackel *args) { |
| 117 | int narg = 0, depth = 0; |
| 118 | for (char32 *p = arguments; ; p ++) { |
| 119 | const bool endOfArguments = ( *p == U'\0' ); |
| 120 | if (endOfArguments || (*p == U',' && depth == 0)) { |
| 121 | if (narg == MAXIMUM_NUMBER_OF_FIELDS) |
| 122 | Melder_throw (U"Cannot have more than ", MAXIMUM_NUMBER_OF_FIELDS, U" arguments"); |
| 123 | *p = U'\0'; |
| 124 | Formula_Result result; |
| 125 | Interpreter_anyExpression (interpreter, arguments, & result); |
| 126 | narg ++; |
| 127 | /* |
| 128 | First remove the old contents. |
| 129 | */ |
| 130 | args [narg]. reset(); |
| 131 | #if STACKEL_VARIANTS_ARE_PACKED_IN_A_UNION |
| 132 | memset (& args [narg], 0, sizeof (structStackel)); |
| 133 | #endif |
| 134 | /* |
| 135 | Then copy in the new contents. |
| 136 | */ |
| 137 | switch (result. expressionType) { |
| 138 | case kFormula_EXPRESSION_TYPE_NUMERIC: { |
| 139 | args [narg]. which = Stackel_NUMBER; |
| 140 | args [narg]. number = result. numericResult; |
| 141 | } break; case kFormula_EXPRESSION_TYPE_STRING: { |
| 142 | args [narg]. setString (result. stringResult.move()); |
| 143 | } break; case kFormula_EXPRESSION_TYPE_NUMERIC_VECTOR: { |
| 144 | args [narg]. which = Stackel_NUMERIC_VECTOR; |
| 145 | args [narg]. numericVector = result. numericVectorResult; |
| 146 | args [narg]. owned = result. owned; |
| 147 | result. owned = false; |
| 148 | } break; case kFormula_EXPRESSION_TYPE_NUMERIC_MATRIX: { |
| 149 | args [narg]. which = Stackel_NUMERIC_MATRIX; |
| 150 | args [narg]. numericMatrix = result. numericMatrixResult; |
| 151 | args [narg]. owned = result. owned; |
| 152 | result. owned = false; |
| 153 | } break; case kFormula_EXPRESSION_TYPE_STRING_ARRAY: { |
| 154 | args [narg]. which = Stackel_STRING_ARRAY; |
| 155 | args [narg]. stringArray = result. stringArrayResult; |
| 156 | args [narg]. owned = result. owned; |
| 157 | result. owned = false; |
| 158 | } break; |
| 159 | } |
| 160 | arguments = p + 1; |
| 161 | } else if (*p == U'(' || *p == U'[' || *p == U'{') { |
| 162 | depth ++; |
| 163 | } else if (*p == U')' || *p == U']' || *p == U'}') { |
| 164 | depth --; |
| 165 | } else if (*p == U'\"') { |
| 166 | for (;;) { |
| 167 | p ++; |
| 168 | if (*p == U'\"') { |
| 169 | if (p [1] == U'\"') |
| 170 | p ++; |
| 171 | else |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | if (endOfArguments) |
| 177 | break; |
| 178 | } |
| 179 | return narg; |
| 180 | } |
| 181 | |
| 182 | bool praat_executeCommand (Interpreter interpreter, char32 *command) { |
| 183 | if (interpreter) |
| 184 | interpreter -> returnType = kInterpreter_ReturnType::VOID_; // clear return type to its default |
| 185 | |
| 186 | static struct structStackel args [1 + MAXIMUM_NUMBER_OF_FIELDS]; |
| 187 | //trace (U"praat_executeCommand: ", Melder_pointer (interpreter), U": ", command); |
| 188 | if (command [0] == U'\0' || command [0] == U'#' || command [0] == U'!' || command [0] == U';') |
| 189 | /* Skip empty lines and comments. */; |
| 190 | else if ((command [0] == U'.' || command [0] == U'+' || command [0] == U'-') && Melder_isAsciiUpperCaseLetter (command [1])) { // selection? |
| 191 | integer IOBJECT = praat_findObjectFromString (interpreter, command + 1); |
| 192 | if (command [0] == '.') |
| 193 | praat_deselectAll (); |
| 194 | if (command [0] == '-') |
| 195 | praat_deselect (IOBJECT); |
| 196 | else |
| 197 | praat_select (IOBJECT); |
| 198 | praat_show (); |
| 199 | } else if (Melder_isLetter (command [0]) && ! Melder_isUpperCaseLetter (command [0])) { // all directives start with an ASCII lower-case letter |
| 200 | if (str32nequ (command, U"select ", 7)) { |
| 201 | if (str32nequ (command + 7, U"all", 3) && (command [10] == U'\0' || command [10] == U' ' || command [10] == U'\t')) { |
| 202 | praat_selectAll (); |
| 203 | praat_show (); |
| 204 | } else { |
| 205 | integer IOBJECT = praat_findObjectFromString (interpreter, command + 7); |
| 206 | praat_deselectAll (); |
| 207 | praat_select (IOBJECT); |
| 208 | praat_show (); |
| 209 | } |
| 210 | } else if (str32nequ (command, U"plus ", 5)) { |
| 211 | integer IOBJECT = praat_findObjectFromString (interpreter, command + 5); |
| 212 | praat_select (IOBJECT); |
| 213 | praat_show (); |
| 214 | } else if (str32nequ (command, U"minus ", 6)) { |
| 215 | integer IOBJECT = praat_findObjectFromString (interpreter, command + 6); |
| 216 | praat_deselect (IOBJECT); |
| 217 | praat_show (); |
| 218 | } else if (str32nequ (command, U"echo ", 5)) { |
| 219 | MelderInfo_open (); |
| 220 | MelderInfo_write (command + 5); |
| 221 | MelderInfo_close (); |
| 222 | } else if (str32nequ (command, U"clearinfo", 9)) { |
| 223 | Melder_clearInfo (); |
| 224 | } else if (str32nequ (command, U"print ", 6)) { |
| 225 | MelderInfo_write (command + 6); |
| 226 | MelderInfo_drain (); |
| 227 | } else if (str32nequ (command, U"printtab", 8)) { |
| 228 | MelderInfo_write (U"\t"); |
| 229 | MelderInfo_drain (); |
| 230 | } else if (str32nequ (command, U"printline", 9)) { |
| 231 | if (command [9] == ' ') MelderInfo_write (command + 10); |
| 232 | MelderInfo_write (U"\n"); |
| 233 | MelderInfo_drain (); |
| 234 | } else if (str32nequ (command, U"fappendinfo ", 12)) { |
| 235 | if (theCurrentPraatObjects != & theForegroundPraatObjects) |
| 236 | Melder_throw (U"The script command “fappendinfo” is not available inside pictures."); |
| 237 | structMelderFile file { }; |
| 238 | Melder_relativePathToFile (command + 12, & file); |
| 239 | MelderFile_appendText_e (& file, Melder_getInfo ()); |
| 240 | } else if (str32nequ (command, U"unix ", 5)) { |
| 241 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 242 | U"The script command “unix” is not available inside manuals."); |
| 243 | try { |
| 244 | Melder_runSystem (command + 5); |
| 245 | } catch (MelderError) { |
| 246 | Melder_throw (U"Unix command “", command + 5, U"” returned error status;\n" |
| 247 | U"if you want to ignore this, use `unix_nocheck' instead of `unix'."); |
| 248 | } |
| 249 | } else if (str32nequ (command, U"unix_nocheck ", 13)) { |
| 250 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 251 | U"The script command “unix_nocheck” is not available inside manuals."); |
| 252 | try { |
| 253 | Melder_runSystem (command + 13); |
| 254 | } catch (MelderError) { |
| 255 | Melder_clearError (); |
| 256 | } |
| 257 | } else if (str32nequ (command, U"system ", 7)) { |
| 258 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 259 | U"The script command “system” is not available inside manuals."); |
| 260 | try { |
| 261 | Melder_runSystem (command + 7); |
| 262 | } catch (MelderError) { |
| 263 | Melder_throw (U"System command “", command + 7, U"” returned error status;\n" |
| 264 | U"if you want to ignore this, use “system_nocheck” instead of “system”."); |
| 265 | } |
| 266 | } else if (str32nequ (command, U"system_nocheck ", 15)) { |
| 267 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 268 | U"The script command “system_nocheck” is not available inside manuals."); |
| 269 | try { |
| 270 | Melder_runSystem (command + 15); |
| 271 | } catch (MelderError) { |
| 272 | Melder_clearError (); |
| 273 | } |
| 274 | } else if (str32nequ (command, U"nowarn ", 7)) { |
| 275 | autoMelderWarningOff nowarn; |
| 276 | return praat_executeCommand (interpreter, command + 7); |
| 277 | } else if (str32nequ (command, U"noprogress ", 11)) { |
| 278 | autoMelderProgressOff noprogress; |
| 279 | return praat_executeCommand (interpreter, command + 11); |
| 280 | } else if (str32nequ (command, U"nocheck ", 8)) { |
| 281 | try { |
| 282 | return praat_executeCommand (interpreter, command + 8); |
| 283 | } catch (MelderError) { |
| 284 | Melder_clearError (); |
| 285 | return false; |
| 286 | } |
| 287 | } else if (str32nequ (command, U"demo ", 5)) { |
| 288 | autoDemoOpen demo; |
| 289 | return praat_executeCommand (interpreter, command + 5); |
| 290 | } else if (str32nequ (command, U"asynchronous ", 13)) { |
| 291 | autoMelderAsynchronous asynchronous; |
| 292 | return praat_executeCommand (interpreter, command + 13); |
| 293 | } else if (str32nequ (command, U"pause ", 6) || str32equ (command, U"pause")) { |
| 294 | if (theCurrentPraatApplication -> batch) |
| 295 | return true; // in batch we ignore pause statements |
| 296 | const Editor optionalPauseWindowOwningEditor = interpreter -> optionalDynamicEnvironmentEditor(); |
| 297 | const GuiWindow parentShell = ( optionalPauseWindowOwningEditor ? optionalPauseWindowOwningEditor -> windowForm : theCurrentPraatApplication -> topShell ); |
| 298 | UiPause_pauseScript (parentShell, optionalPauseWindowOwningEditor, interpreter, str32equ (command, U"pause") ? U"..." : command + 6); |
| 299 | } else if (str32nequ (command, U"execute ", 8)) { |
| 300 | //TRACE |
| 301 | trace (U"interpreter: ", Melder_pointer (interpreter)); |
| 302 | if (interpreter) |
| 303 | trace (U"stack: ", Melder_pointer (interpreter -> owningInterpreterStack)); |
| 304 | praat_runOldExecuteCommand (interpreter ? interpreter -> owningInterpreterStack : nullptr, command + 8); |
| 305 | } else if (str32nequ (command, U"editor", 6)) { // deprecated |
| 306 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 307 | U"The script command “editor” is not available inside manuals."); |
| 308 | if (command [6] == U' ' && Melder_isLetter (command [7])) { |
| 309 | interpreter -> setDynamicEditorEnvironmentFromEditor (praat_findEditorFromString (command + 7)); |
| 310 | } else if (command [6] == U'\0') { |
| 311 | if (interpreter && interpreter -> wasStartedFromEditorEnvironment()) |
| 312 | /* |
| 313 | The script directive |
| 314 | editor |
| 315 | (without arguments) should set the interpreter's environment back to the editor that started the script. |
| 316 | */ |
| 317 | interpreter -> setDynamicFromOwningEditorEnvironment(); // TODO: what if the owener has been orphaned? |
| 318 | else |
| 319 | Melder_throw (U"The script command “editor” requires an argument when called from outside an editor."); |
| 320 | } else { |
| 321 | Interpreter_voidExpression (interpreter, command); |
| 322 | } |
| 323 | } else if (str32nequ (command, U"endeditor", 9)) { |
| 324 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 325 | U"The script command “endeditor” is not available inside manuals."); |
| 326 | interpreter -> nullifyDynamicEditorEnvironment(); |
| 327 | } else if (str32nequ (command, U"filedelete ", 11)) { |
| 328 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 329 | U"The script command “filedelete” is not available inside manuals."); |
| 330 | const char32 *p = command + 11; |
| 331 | structMelderFile file { }; |
| 332 | while (*p == U' ' || *p == U'\t') p ++; |
| 333 | if (*p == U'\0') |
| 334 | Melder_throw (U"Missing file name after “filedelete”."); |
| 335 | Melder_relativePathToFile (p, & file); |
| 336 | MelderFile_delete (& file); |
| 337 | } else if (str32nequ (command, U"fileappend ", 11)) { |
| 338 | Melder_require (praat_commandsWithExternalSideEffectsAreAllowed (), |
| 339 | U"The script command “fileappend” is not available inside manuals."); |
| 340 | const char32 *p = command + 11; |
| 341 | char32 path [kMelder_MAXPATH+1], *q = & path [0]; |
| 342 | while (*p == U' ' || *p == U'\t') p ++; |
| 343 | if (*p == U'\0') |
| 344 | Melder_throw (U"Missing file name after “fileappend”."); |
| 345 | if (*p == '\"') { |
| 346 | for (;;) { |
| 347 | char32 kar = * ++ p; |
| 348 | if (kar == U'\"') if (* ++ p == U'\"') *q ++ = U'\"'; else break; |
| 349 | else if (kar == U'\0') break; |
| 350 | else *q ++ = kar; |
| 351 | } |
| 352 | } else { |
| 353 | for (;;) { |
| 354 | char32 kar = * p; |
| 355 | if (kar == U'\0' || kar == U' ' || kar == U'\t') break; |
| 356 | *q ++ = kar; |
| 357 | p ++; |
| 358 | } |
| 359 | } |
| 360 | *q = U'\0'; |
| 361 | if (*p == U' ' || *p == U'\t') { |
| 362 | structMelderFile file { }; |
| 363 | Melder_relativePathToFile (path, & file); |
| 364 | MelderFile_appendText_e (& file, p + 1); |
| 365 | } |
| 366 | } else { |
| 367 | /* |
| 368 | This must be a formula command: |
| 369 | proc (args) |
| 370 | */ |
| 371 | Interpreter_voidExpression (interpreter, command); |
| 372 | } |
| 373 | } else { /* Simulate menu choice. */ |
| 374 | bool hasDots = false, hasColon = false; |
| 375 | |
| 376 | /* Parse command line into command and arguments. */ |
| 377 | /* The separation is formed by the three dots or a colon. */ |
| 378 | |
| 379 | char32 *arguments = & command [0]; |
| 380 | for (arguments = & command [0]; *arguments != U'\0'; arguments ++) { |
| 381 | if (*arguments == U':') { |
| 382 | hasColon = true; |
| 383 | if (arguments [1] == U'\0') { |
| 384 | arguments = & arguments [1]; // empty string |
| 385 | } else { |
| 386 | if (arguments [1] != U' ') |
| 387 | Melder_throw (U"There should be a space after the colon."); |
| 388 | arguments [1] = U'\0'; // new end of "command" |
| 389 | arguments += 2; // the arguments start after the space |
| 390 | } |
| 391 | break; |
| 392 | } |
| 393 | if (*arguments == U'.' && arguments [1] == U'.' && arguments [2] == U'.') { |
| 394 | hasDots = true; |
| 395 | arguments += 3; |
| 396 | if (*arguments == U'\0') { |
| 397 | // empty string |
| 398 | } else { |
| 399 | if (*arguments != U' ') |
| 400 | Melder_throw (U"There should be a space after the three dots."); |
| 401 | *arguments = U'\0'; // new end of "command" |
| 402 | arguments ++; // the arguments start after the space |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* See if command exists and is available; ignore separators. */ |
| 409 | /* First try loose commands, then fixed commands. */ |
| 410 | |
| 411 | integer narg; |
| 412 | char32 command2 [200]; |
| 413 | if (hasColon) { |
| 414 | narg = parseCommaSeparatedArguments (interpreter, arguments, args); |
| 415 | str32cpy (command2, command); |
| 416 | char32 *colon = str32chr (command2, U':'); |
| 417 | colon [0] = colon [1] = colon [2] = U'.'; |
| 418 | colon [3] = U'\0'; |
| 419 | } |
| 420 | if (theCurrentPraatObjects == & theForegroundPraatObjects && interpreter && interpreter -> hasDynamicEnvironmentEditor()) { |
| 421 | if (hasColon) |
| 422 | Editor_doMenuCommand (interpreter -> optionalDynamicEnvironmentEditor(), command2, narg, args, nullptr, interpreter); |
| 423 | else |
| 424 | Editor_doMenuCommand (interpreter -> optionalDynamicEnvironmentEditor(), command, 0, nullptr, arguments, interpreter); |
| 425 | } else if (! praat_commandsWithExternalSideEffectsAreAllowed () && |
| 426 | (str32nequ (command, U"Save ", 5) || |
| 427 | str32nequ (command, U"Write ", 6) || |
| 428 | str32nequ (command, U"Append to ", 10) || |
| 429 | str32nequ (command, U"Quit", 4))) |
| 430 | { |
| 431 | Melder_throw (U"Commands that write files (including Quit) are not available inside manuals."); |
| 432 | } else { |
| 433 | bool theCommandIsAnExistingAction = false; |
| 434 | try { |
| 435 | if (hasColon) |
| 436 | theCommandIsAnExistingAction = praat_doAction (command2, narg, args, interpreter); |
| 437 | else |
| 438 | theCommandIsAnExistingAction = praat_doAction (command, arguments, interpreter); |
| 439 | } catch (MelderError) { |
| 440 | /* |
| 441 | We only get here if the command *was* an existing action. |
| 442 | Anything could have gone wrong in its execution, |
| 443 | but one invisible problem can be checked here. |
| 444 | */ |
| 445 | if (hasDots && arguments [0] != U'\0' && arguments [Melder_length (arguments) - 1] == U' ') |
| 446 | Melder_throw (U"It may be helpful to remove the trailing spaces in \"", arguments, U"\"."); |
| 447 | else |
| 448 | throw; |
| 449 | } |
| 450 | if (! theCommandIsAnExistingAction) { |
| 451 | bool theCommandIsAnExistingMenuCommand = false; |
| 452 | try { |
| 453 | if (hasColon) |
| 454 | theCommandIsAnExistingMenuCommand = praat_doMenuCommand (command2, narg, args, interpreter); |
| 455 | else |
| 456 | theCommandIsAnExistingMenuCommand = praat_doMenuCommand (command, arguments, interpreter); |
| 457 | } catch (MelderError) { |
| 458 | /* |
| 459 | We only get here if the command *was* an existing menu command. |
| 460 | Anything could have gone wrong in its execution, |
| 461 | but one invisible problem can be checked here. |
| 462 | */ |
| 463 | if (hasDots && arguments [0] != U'\0' && arguments [Melder_length (arguments) - 1] == U' ') |
| 464 | Melder_throw (U"It may be helpful to remove the trailing spaces in “", arguments, U"”."); |
| 465 | else |
| 466 | throw; |
| 467 | } |
| 468 | if (! theCommandIsAnExistingMenuCommand) { |
| 469 | const integer length = Melder_length (command); |
| 470 | if (str32nequ (command, U"ARGS ", 5)) |
| 471 | Melder_throw (U"Command “ARGS” no longer supported. Instead use “form” and “endform”."); |
| 472 | else if (str32chr (command, U'=')) |
| 473 | if (command [0] == U'_') |
| 474 | Melder_throw (U"Command “", command, U"” not recognized.\n" |
| 475 | U"Probable cause: you are trying to use a variable name that starts with an underscore."); |
| 476 | else |
| 477 | Melder_throw (U"Command “", command, U"” not recognized.\n" |
| 478 | U"Probable cause: you are trying to use a variable name that starts with a capital letter."); |
| 479 | else if (length >= 1 && Melder_isHorizontalSpace (command [length - 1])) |
| 480 | Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 481 | U"It may be helpful to remove the trailing spaces."); |
| 482 | else if (length >= 2 && Melder_isHorizontalSpace (command [length - 2]) && command [length - 1] == U':') |
| 483 | Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 484 | U"It may be helpful to remove the space before the colon."); |
| 485 | else if (str32nequ (command, U"\"ooTextFile\"", 12)) |
| 486 | Melder_throw (U"Command “", command, U"” not available for current selection. " |
| 487 | U"It is possible that this file is not a Praat script but a Praat data file that you can open with “Read from file...”."); |
| 488 | else { |
| 489 | if (interpreter && interpreter -> wasStartedFromEditorEnvironment()) |
| 490 | if (interpreter -> hasDynamicEnvironmentEditor()) |
| 491 | Melder_throw (U"Command “", command, U"” not available in ", interpreter -> optionalDynamicEditorEnvironmentClassName(), U"."); |
| 492 | else |
| 493 | Melder_throw (U"Command “", command, U"” not available for current selection.\n" |
| 494 | U"Perhaps this is a ", interpreter -> optionalOwningEditorEnvironmentClassName(), U" command?"); |
| 495 | else |
| 496 | Melder_throw (U"Command “", command, U"” not available for current selection."); |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | praat_updateSelection (); |
| 502 | } |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | void praat_executeCommandFromStandardInput (conststring32 programName) { |
| 507 | char command8 [1000]; // can be recursive |
| 508 | /* |
| 509 | FIXME: implement for Windows. |
| 510 | */ |
| 511 | for (;;) { |
| 512 | printf ("%s > ", Melder_peek32to8 (programName)); |
| 513 | if (! fgets (command8, 999, stdin)) |
| 514 | Melder_throw (U"Cannot read input."); |
| 515 | char *newLine = strchr (command8, '\n'); |
| 516 | if (newLine) |
| 517 | *newLine = '\0'; |
| 518 | autostring32 command32 = Melder_8to32_e (command8); |
| 519 | try { |
| 520 | (void) praat_executeCommand (nullptr, command32.get()); |
| 521 | } catch (MelderError) { |
| 522 | Melder_flushError (programName, U": Command “", Melder_peek8to32_u (command8), U"” not executed."); |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void praat_executeScript_noGUI (MelderFile file, bool fullTrust) { |
| 528 | /* |
| 529 | The problem here occurs when plug-in initialization (typically, `setup.praat`) posts a pause window, |
| 530 | as happens e.g. in the VocalToolkit plug-in if the Praat version is too old. |
| 531 | This function will return while the pause window is on the screen, and |
| 532 | Praat will happily initialize the next plug-in. |
| 533 | Quick fix: allow up to 10 InterpreterStacks. |
| 534 | Long-term solution TODO: make pause windows in plug-in initializers modal. |
| 535 | */ |
| 536 | constexpr integer NUMBER_OF_STACKS = 10; |
| 537 | static autoInterpreterStack interpreterStacks [NUMBER_OF_STACKS]; |
| 538 | static integer stackNumber = 0; |
| 539 | stackNumber = (stackNumber + 1) % NUMBER_OF_STACKS; |
| 540 | autoInterpreterStack& interpreterStack = interpreterStacks [stackNumber]; |
| 541 | try { |
| 542 | interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 543 | autostring32 text = MelderFile_readText (file); |
| 544 | {// scope |
| 545 | autoMelderFileSetCurrentFolder folder (file); // so that script-relative file names can be used for including include files |
| 546 | Melder_includeIncludeFiles (& text); |
| 547 | } // back to the default directory of the caller |
| 548 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 549 | interpreterStack.get(), // owner always needed |
| 550 | Editor (nullptr), // never used inside an Editor window |
| 551 | file // so that script-relative file names can be used inside the script |
| 552 | ); |
| 553 | interpreterStack -> emptyAll (); |
| 554 | Interpreter_rememberScript_override (interpreter.get(), file, fullTrust && str32str (text.get(), U"\n# --FULL-TRUST\n")); |
| 555 | interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 556 | } catch (MelderError) { |
| 557 | Melder_throw (U"Script ", file, U" not completed."); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | void praat_runScript (InterpreterStack interpreterStack, conststring32 fileName, integer narg, Stackel args, Editor optionalInterpreterOwningEditor) { |
| 562 | Melder_assert (interpreterStack); |
| 563 | //Melder_assert (Melder_backgrounding); |
| 564 | |
| 565 | Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 566 | if (parentInterpreter -> isInSecondPass) { |
| 567 | interpreterStack -> resumeNextLevelInSecondPass (); |
| 568 | } else { |
| 569 | Melder_assert (MelderFolder_equal (Melder_peekWorkingDirectory (), & parentInterpreter -> workingDirectory)); |
| 570 | //autoMelderSetCurrentFolder folder (& parentInterpreter -> workingDirectory); // TODO: is this superfluous, i.e. does the previous assertion never fire? |
| 571 | |
| 572 | structMelderFile file { }; |
| 573 | try { |
| 574 | Melder_relativePathToFile (fileName, & file); |
| 575 | autostring32 text = MelderFile_readText (& file); |
| 576 | /* |
| 577 | We switch between default directories no fewer than four times: |
| 578 | 1. runScript() tends to be called from a script that we call the "caller"; |
| 579 | when we enter runScript(), the default directory is the caller's folder, |
| 580 | as was appropriate for the use of file names in the caller before runScript(), |
| 581 | which had to be interpreted relative to the caller's folder. |
| 582 | 2. runScript() will call a script that we call the "callee"; |
| 583 | include files have to be included from the callee's folder. |
| 584 | 3. For expanding any infile/outfile/folder arguments to runScript(), |
| 585 | we have to be back in the caller's folder. |
| 586 | 4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 587 | 5. After runScript() finishes, we will have to be back in the caller's folder, |
| 588 | so that the use of file names in the caller after runScript() |
| 589 | will be interpreted relative to the caller's folder again. |
| 590 | */ |
| 591 | {// scope |
| 592 | autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 593 | Melder_includeIncludeFiles (& text); |
| 594 | } // back to the default directory of the caller |
| 595 | autoInterpreter me = Interpreter_createFromEnvironment ( |
| 596 | interpreterStack, // owner always needed |
| 597 | optionalInterpreterOwningEditor, |
| 598 | & file // so that callee-relative file names can be used inside the script |
| 599 | ); |
| 600 | Interpreter_readParameters (me.get(), text.get()); // TODO: should become a field of structInterpreter |
| 601 | my text = text.move(); |
| 602 | Interpreter_getArgumentsFromArgs (me.get(), narg, args); // interpret caller-relative paths for infile/outfile/folder arguments |
| 603 | Interpreter_rememberScript (me.get(), & file, ! parentInterpreter -> scriptReference || parentInterpreter -> scriptReference -> trusted); |
| 604 | interpreterStack -> runDown (me.move(), autostring32(), false); // back to the default directory of the caller |
| 605 | } catch (MelderError) { |
| 606 | Melder_throw (U"Script ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void praat_runNotebook (InterpreterStack interpreterStack, conststring32 fileName, integer narg, Stackel args, Editor optionalInterpreterOwningEditor) { |
| 612 | Melder_assert (interpreterStack); |
| 613 | //Melder_assert (Melder_backgrounding); |
| 614 | |
| 615 | //TRACE |
| 616 | Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 617 | if (parentInterpreter -> isInSecondPass) { |
| 618 | interpreterStack -> resumeNextLevelInSecondPass (); |
| 619 | } else { |
| 620 | structMelderFolder workingDirectory; |
| 621 | Melder_getCurrentFolder (& workingDirectory); |
| 622 | Melder_assert (MelderFolder_equal (& workingDirectory, & parentInterpreter -> workingDirectory)); |
| 623 | |
| 624 | structMelderFile file { }; |
| 625 | try { |
| 626 | Melder_relativePathToFile (fileName, & file); |
| 627 | autostring32 text = MelderFile_readText (& file); |
| 628 | if (! Melder_startsWith (text.get(), U"\"")) |
| 629 | Melder_throw (U"File ", & file, U" is not a Praat notebook."); |
| 630 | /* |
| 631 | We switch between default directories no fewer than four times: |
| 632 | 1. runScript() tends to be called from a script that we call the "caller"; |
| 633 | when we enter runScript(), the working directory is the caller's folder |
| 634 | (set upon Interpreter creation as the folder where the calling script is, |
| 635 | though perhaps changed later in that script by `setWorkingDirectory`), |
| 636 | as was appropriate for the use of file names in the caller before runScript(), |
| 637 | which had to be interpreted relative to the caller's folder. |
| 638 | 2. runScript() will call a script that we call the "callee"; |
| 639 | include files have to be included from the callee's folder. |
| 640 | 3. For expanding any infile/outfile/folder arguments to runScript(), |
| 641 | we have to be back in the caller's folder. |
| 642 | 4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 643 | 5. After runScript() finishes, we will have to be back in the caller's folder, |
| 644 | so that the use of file names in the caller after runScript() |
| 645 | will be interpreted relative to the caller's folder again. |
| 646 | */ |
| 647 | {// scope |
| 648 | autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 649 | Melder_includeIncludeFiles (& text); |
| 650 | } // back to the default directory of the caller |
| 651 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 652 | interpreterStack, |
| 653 | optionalInterpreterOwningEditor, |
| 654 | & file |
| 655 | ); |
| 656 | Interpreter_readParameters (interpreter.get(), text.get()); // TODO: should become a field of structInterpreter |
| 657 | autoMelderReadText readText = MelderReadText_createFromText (text.move()); |
| 658 | autoManPages manPages = ManPages_createFromText (readText.get(), & file); |
| 659 | ManPage firstPage = manPages -> pages.at [1]; |
| 660 | autoManual manual = Manual_create (firstPage -> title.get(), interpreter.get(), manPages.releaseToAmbiguousOwner(), true, true); |
| 661 | manual.releaseToUser (); |
| 662 | } catch (MelderError) { |
| 663 | Melder_throw (U"Notebook ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void praat_executeScriptFromCommandLine (conststring32 fileName, integer argc, char **argv, bool fullTrust) { |
| 669 | static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 670 | structMelderFile file { }; |
| 671 | Melder_relativePathToFile (fileName, & file); |
| 672 | try { |
| 673 | autostring32 text = MelderFile_readText (& file); |
| 674 | {// scope |
| 675 | autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used for including include files |
| 676 | Melder_includeIncludeFiles (& text); |
| 677 | } // back to the default directory of the caller |
| 678 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 679 | interpreterStack.get(), |
| 680 | Editor (nullptr), |
| 681 | & file |
| 682 | ); |
| 683 | Interpreter_readParameters (interpreter.get(), text.get()); |
| 684 | Interpreter_getArgumentsFromCommandLine (interpreter.get(), argc, argv); // interpret caller-relative paths for infile/outfile/folder arguments |
| 685 | autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used inside the script |
| 686 | Interpreter_rememberScript (interpreter.get(), & file, fullTrust); |
| 687 | interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 688 | } catch (MelderError) { |
| 689 | Melder_throw (U"Script ", & file, U" not completed."); // don't refer to 'fileName', because its contents may have changed |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | void praat_executeScriptFromFileNameWithArguments (conststring32 nameAndArguments) { |
| 694 | structMelderFile file { }; |
| 695 | try { |
| 696 | char32 path [256]; |
| 697 | const char32 *p, *arguments; |
| 698 | /* |
| 699 | Split into file name and arguments. |
| 700 | */ |
| 701 | p = nameAndArguments; |
| 702 | while (*p == U' ' || *p == U'\t') |
| 703 | p ++; |
| 704 | if (*p == U'\"') { |
| 705 | char32 *q = path; |
| 706 | p ++; // skip quote |
| 707 | while (*p != U'\"' && *p != U'\0') |
| 708 | * q ++ = * p ++; |
| 709 | *q = U'\0'; |
| 710 | arguments = p; |
| 711 | if (*arguments == U'\"') |
| 712 | arguments ++; |
| 713 | if (*arguments == U' ') |
| 714 | arguments ++; |
| 715 | } else { |
| 716 | char32 *q = path; |
| 717 | while (*p != U' ' && *p != U'\0') |
| 718 | * q ++ = * p ++; |
| 719 | *q = U'\0'; |
| 720 | arguments = p; |
| 721 | if (*arguments == U' ') |
| 722 | arguments ++; |
| 723 | } |
| 724 | Melder_relativePathToFile (path, & file); |
| 725 | |
| 726 | static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); |
| 727 | autostring32 text = MelderFile_readText (& file); |
| 728 | {// scope |
| 729 | autoMelderFileSetCurrentFolder folder (& file); // so that script-relative file names can be used for including include files |
| 730 | Melder_includeIncludeFiles (& text); |
| 731 | } // back to the default directory of the caller |
| 732 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 733 | interpreterStack.get(), |
| 734 | Editor (nullptr), |
| 735 | & file // so that script-relative file names can be used inside the script |
| 736 | ); |
| 737 | if (arguments) { |
| 738 | Interpreter_readParameters (interpreter.get(), text.get()); |
| 739 | Interpreter_getArgumentsFromString (interpreter.get(), arguments); // interpret caller-relative paths for infile/outfile/folder arguments |
| 740 | } |
| 741 | Interpreter_rememberScript (interpreter.get(), & file, false); |
| 742 | interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 743 | } catch (MelderError) { |
| 744 | Melder_throw (U"Script ", & file, U" not completed."); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | void praat_runOldExecuteCommand (InterpreterStack interpreterStack, conststring32 nameAndArguments) { |
| 749 | Melder_assert (interpreterStack); |
| 750 | |
| 751 | char32 path [256]; |
| 752 | const char32 *p, *arguments; |
| 753 | structMelderFile file { }; |
| 754 | /* |
| 755 | Split into file name and arguments. |
| 756 | */ |
| 757 | p = nameAndArguments; |
| 758 | while (*p == U' ' || *p == U'\t') |
| 759 | p ++; |
| 760 | if (*p == U'\"') { |
| 761 | char32 *q = path; |
| 762 | p ++; // skip quote |
| 763 | while (*p != U'\"' && *p != U'\0') |
| 764 | * q ++ = * p ++; |
| 765 | *q = U'\0'; |
| 766 | arguments = p; |
| 767 | if (*arguments == U'\"') |
| 768 | arguments ++; |
| 769 | if (*arguments == U' ') |
| 770 | arguments ++; |
| 771 | } else { |
| 772 | char32 *q = path; |
| 773 | while (*p != U' ' && *p != U'\0') |
| 774 | * q ++ = * p ++; |
| 775 | *q = U'\0'; |
| 776 | arguments = p; |
| 777 | if (*arguments == U' ') |
| 778 | arguments ++; |
| 779 | } |
| 780 | Melder_relativePathToFile (path, & file); |
| 781 | |
| 782 | Interpreter parentInterpreter = interpreterStack -> current_a (); |
| 783 | if (parentInterpreter -> isInSecondPass) { |
| 784 | interpreterStack -> resumeNextLevelInSecondPass (); |
| 785 | } else { |
| 786 | try { |
| 787 | autostring32 text = MelderFile_readText (& file); |
| 788 | {// scope |
| 789 | autoMelderFileSetCurrentFolder folder (& file); // so that callee-relative file names can be used for including include files |
| 790 | Melder_includeIncludeFiles (& text); |
| 791 | } // back to the default directory of the caller |
| 792 | autoInterpreter me = Interpreter_createFromEnvironment ( |
| 793 | interpreterStack, |
| 794 | Editor (nullptr), |
| 795 | & file // so that callee-relative file names can be used inside the script |
| 796 | ); |
| 797 | if (arguments) { |
| 798 | Interpreter_readParameters (me.get(), text.get()); |
| 799 | Interpreter_getArgumentsFromString (me.get(), arguments); // interpret caller-relative paths for infile/outfile/folder arguments |
| 800 | } |
| 801 | my text = text.move(); |
| 802 | Interpreter_rememberScript (me.get(), & file, ! parentInterpreter -> scriptReference || parentInterpreter -> scriptReference -> trusted); |
| 803 | interpreterStack -> runDown (me.move(), autostring32(), false); // callee-relative file names can be used inside the script |
| 804 | // back to the default directory of the caller |
| 805 | } catch (MelderError) { |
| 806 | Melder_throw (U"Script ", & file, U" not completed."); |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | extern "C" void praatlib_executeScript (const char *text8) { |
| 812 | static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); // TODO: never from an editor? |
| 813 | try { |
| 814 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 815 | interpreterStack.get(), |
| 816 | Editor (nullptr), |
| 817 | MelderFile (nullptr) // TODO: look for a file... |
| 818 | ); |
| 819 | autostring32 string = Melder_8to32_e (text8); |
| 820 | interpreterStack -> runDown (interpreter.move(), string.move(), false); |
| 821 | } catch (MelderError) { |
| 822 | Melder_throw (U"Script not completed."); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | void praat_executeScriptFromText (conststring32 text) { |
| 827 | static autoInterpreterStack interpreterStack = InterpreterStack_create (Editor (nullptr)); // never from an editor (last checked 2026-02-01) |
| 828 | try { |
| 829 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 830 | interpreterStack.get(), |
| 831 | Editor (nullptr), // never from an editor (last checked 2026-02-01) |
| 832 | MelderFile (nullptr) |
| 833 | ); |
| 834 | Melder_getCurrentFolder (& interpreter -> workingDirectory); |
| 835 | interpreterStack -> emptyAll (); |
| 836 | //Interpreter_rememberScript (interpreter.get(), & file, true); // will be OR-ed |
| 837 | interpreterStack -> runDown (interpreter.move(), Melder_dup (text), false); |
| 838 | } catch (MelderError) { |
| 839 | Melder_throw (U"Script not completed."); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | static void secondPassThroughScript (UiForm sendingForm, integer /* narg */, Stackel /* args */, |
| 844 | conststring32 /* sendingString_dummy */, Interpreter /* interpreter_dummy */, |
| 845 | conststring32 /* invokingButtonTitle */, bool /* modified */, void * /* closure */, Editor optionalInterpreterOwningEditor) |
| 846 | { |
| 847 | static autoInterpreterStack interpreterStack; |
| 848 | interpreterStack = InterpreterStack_create (optionalInterpreterOwningEditor); // each time anew |
| 849 | structMelderFile file { }; |
| 850 | Melder_pathToFile (sendingForm -> scriptFilePath.get(), & file); |
| 851 | autostring32 text = MelderFile_readText (& file); |
| 852 | {// scope |
| 853 | autoMelderFileSetCurrentFolder folder (& file); |
| 854 | Melder_includeIncludeFiles (& text); |
| 855 | } |
| 856 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 857 | interpreterStack.get(), |
| 858 | optionalInterpreterOwningEditor, |
| 859 | & file |
| 860 | ); |
| 861 | Interpreter_readParameters (interpreter.get(), text.get()); |
| 862 | Interpreter_getArgumentsFromDialog (interpreter.get(), sendingForm); |
| 863 | autoPraatBackground background; |
| 864 | interpreterStack -> emptyAll (); |
| 865 | Interpreter_rememberScript (interpreter.get(), & file, false); // will be OR-ed |
| 866 | interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 867 | } |
| 868 | |
| 869 | static void firstPassThroughScript (MelderFile file, Editor optionalInterpreterOwningEditor, EditorCommand optionalCommand, bool fullTrust) { |
| 870 | static autoInterpreterStack interpreterStack; |
| 871 | //UiPause_cleanUp (); // TODO: needed? |
| 872 | try { |
| 873 | autostring32 text = MelderFile_readText (file); |
| 874 | /* |
| 875 | We switch between default directories no fewer than four times: |
| 876 | 1. runScript() tends to be called from a script that we call the "caller"; |
| 877 | when we enter runScript(), the default directory is the caller's folder, |
| 878 | as was appropriate for the use of file names in the caller before runScript(), |
| 879 | which had to be interpreted relative to the caller's folder. |
| 880 | 2. runScript() will call a script that we call the "callee"; |
| 881 | include files have to be included from the callee's folder. |
| 882 | 3. For expanding any infile/outfile/folder arguments to runScript(), |
| 883 | we have to be back in the caller's folder. |
| 884 | 4. Inside the callee, file names will have to be interpreted relative to the callee's folder. |
| 885 | 5. After runScript() finishes, we will have to be back in the caller's folder, |
| 886 | so that the use of file names in the caller after runScript() |
| 887 | will be interpreted relative to the caller's folder again. |
| 888 | */ |
| 889 | {// scope |
| 890 | autoMelderFileSetCurrentFolder folder (file); // so that callee-relative file names can be used for including include files |
| 891 | Melder_includeIncludeFiles (& text); |
| 892 | } // back to the default directory of the caller |
| 893 | |
| 894 | interpreterStack = InterpreterStack_create (optionalInterpreterOwningEditor); |
| 895 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 896 | interpreterStack.get(), |
| 897 | optionalInterpreterOwningEditor, |
| 898 | file // so that callee-relative file names can be used inside the script |
| 899 | ); |
| 900 | Interpreter_rememberScript (interpreter.get(), file, fullTrust); // this is early, but possible here because the name of the script cannot change (and NEEDED for "passing" to second pass) |
| 901 | |
| 902 | const integer numberOfParameters = Interpreter_readParameters (interpreter.get(), text.get()); |
| 903 | if (numberOfParameters > 0) { |
| 904 | const GuiWindow parentShell = ( optionalInterpreterOwningEditor ? optionalInterpreterOwningEditor -> windowForm : theCurrentPraatApplication -> topShell ); |
| 905 | autoUiForm form = Interpreter_createForm (interpreter.get(), parentShell, optionalInterpreterOwningEditor, |
| 906 | MelderFile_peekPath (file), secondPassThroughScript, nullptr, false); |
| 907 | UiForm_do (form.get(), false); |
| 908 | if (optionalCommand) { |
| 909 | optionalCommand -> d_uiform = form.move(); |
| 910 | } else { |
| 911 | UiForm_destroyWhenUnmanaged (form.get()); |
| 912 | form. releaseToUser(); |
| 913 | } |
| 914 | } else { |
| 915 | {// scope |
| 916 | autoPraatBackground background; |
| 917 | interpreterStack -> emptyAll (); |
| 918 | //Interpreter_rememberScript (interpreter.get(), file, false); // superfluous |
| 919 | interpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 920 | } |
| 921 | } |
| 922 | } catch (MelderError) { |
| 923 | Melder_throw (U"Script ", file, U" not completed."); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | void DO_RunTheScriptFromAnyAddedMenuCommand (UiForm /* sendingForm_dummy */, integer /* narg */, Stackel /* args */, |
| 928 | conststring32 scriptPath, Interpreter /* interpreter */, |
| 929 | conststring32 /* invokingButtonTitle */, bool /* modified */, void *, Editor optionalInterpreterOwningEditor) |
| 930 | { |
| 931 | structMelderFile file { }; |
| 932 | Melder_relativePathToFile (scriptPath, & file); |
| 933 | firstPassThroughScript (& file, optionalInterpreterOwningEditor, nullptr, false); |
| 934 | } |
| 935 | |
| 936 | void praat_runScriptWithForm (conststring32 fileName, bool fullTrust) { |
| 937 | structMelderFile file { }; |
| 938 | Melder_relativePathToFile (fileName, & file); |
| 939 | firstPassThroughScript (& file, nullptr, nullptr, fullTrust); |
| 940 | } |
| 941 | |
| 942 | void praat_executeScriptFromEditorCommand (Editor interpreterOwningEditor, EditorCommand command, conststring32 scriptPath) { |
| 943 | Melder_assert (interpreterOwningEditor); |
| 944 | Melder_assert (command); |
| 945 | Melder_assert (command -> d_editor == interpreterOwningEditor); |
| 946 | structMelderFile file { }; |
| 947 | Melder_relativePathToFile (scriptPath, & file); |
| 948 | firstPassThroughScript (& file, interpreterOwningEditor, command, false); |
| 949 | } |
| 950 | |
| 951 | /* End of file praat_script.cpp */ |