| 1 |
/* sendpraat7.c */ |
| 2 |
/* by Paul Boersma */ |
| 3 |
/* 13 November 2021 */ |
| 4 |
/* 26 August 2025: changed WM_USER to WM_APP (needed since February 2025) */ |
| 5 |
/* 2 August 2026: new preference-directory locations, and added flag for "full trust" */ |
| 6 |
|
| 7 |
/* |
| 8 |
* The sendpraat7 subroutine (macOS; Windows; Linux) sends a message |
| 9 |
* to a running Praat (or another program that uses the Praat shell). |
| 10 |
* The sendpraat program behaves identically from a macOS terminal window, |
| 11 |
* from a Windows console, or from a Linux command shell. |
| 12 |
* |
| 13 |
* Newer versions of sendpraat may be found at https://praat.org/sendpraat.html or https://www.fon.hum.uva.nl/praat/sendpraat.html |
| 14 |
* |
| 15 |
* On macOS X, this version works only with Praat version 7.0 (August 2026) or newer. |
| 16 |
* On Windows, this version works only with Praat version 7.0 (August 2026) or newer. |
| 17 |
* On Linux, this version works only only with Praat version 7.0 (August 2026) or newer. |
| 18 |
* Newer versions of Praat may respond faster or more reliably. |
| 19 |
*/ |
| 20 |
|
| 21 |
/******************************************************************* |
| 22 |
|
| 23 |
THIS CODE CAN BE COPIED, USED, AND DISTRIBUTED FREELY. |
| 24 |
IF YOU MODIFY IT, PLEASE KEEP MY NAME AND MARK THE CHANGES. |
| 25 |
IF YOU IMPROVE IT, PLEASE NOTIFY ME AT [email protected]. |
| 26 |
|
| 27 |
*******************************************************************/ |
| 28 |
|
| 29 |
#if defined (macintosh) |
| 30 |
#include <Carbon/Carbon.h> |
| 31 |
#define unix 0 |
| 32 |
#define win 0 |
| 33 |
#define mac 1 |
| 34 |
#elif defined (_WIN32) |
| 35 |
#include <windows.h> |
| 36 |
#include <stdio.h> |
| 37 |
#define unix 0 |
| 38 |
#define win 1 |
| 39 |
#define mac 0 |
| 40 |
#elif defined (UNIX) |
| 41 |
#include <signal.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <ctype.h> |
| 47 |
#define unix 1 |
| 48 |
#define win 0 |
| 49 |
#define mac 0 |
| 50 |
#else |
| 51 |
#define unix 0 |
| 52 |
#define win 0 |
| 53 |
#define mac 0 |
| 54 |
#endif |
| 55 |
|
| 56 |
/* |
| 57 |
* The way to call the sendpraat subroutine from another program. |
| 58 |
*/ |
| 59 |
char *sendpraat7 (long timeOut, int fullTrust, const char *programName, const char *text); |
| 60 |
/* |
| 61 |
* Parameters: |
| 62 |
* `timeOut` is the time (in seconds) after which sendpraat will return with a time-out error message |
| 63 |
* if the receiving program sends no notification of completion. |
| 64 |
* On macOS and Linux, the message is sent asynchronously if `timeOut` is 0; |
| 65 |
* this means that sendpraat will return OK (NULL) without waiting for the receiving program |
| 66 |
* to handle the message. |
| 67 |
* On Windows, the time out is ignored. |
| 68 |
* `fullTrust` is a boolean (0 or 1) that determines whether the script should be allowed |
| 69 |
* to save files and call system commands. |
| 70 |
* `programName` is the name of the program that receives the message. |
| 71 |
* This program must have been built with the Praat shell (the most common such programs are Praat and ALS). |
| 72 |
* On macOS, `programName` must be "Praat", "praat", "ALS", or the Mac signature of any other program. |
| 73 |
* On Windows, you can use either "Praat", "praat", or the name of any other program. |
| 74 |
* On Linux, the program name is usually all lower case, e.g. "praat" or "als", or the name of any other program. |
| 75 |
* `text` contains the contents of the Praat script to be sent to the receiving program, encoded as UTF-8. |
| 76 |
*/ |
| 77 |
|
| 78 |
static char errorMessage [1000]; |
| 79 |
#if unix |
| 80 |
static long theTimeOut; |
| 81 |
static void handleCompletion (int message) { (void) message; } |
| 82 |
static void handleTimeOut (int message) { (void) message; sprintf (errorMessage, "Timed out after %ld seconds.", theTimeOut); } |
| 83 |
#endif |
| 84 |
|
| 85 |
char *sendpraat7 (long timeOut, int fullTrust, const char *programName, const char *text) { |
| 86 |
char nativeProgramName [100], preferencesDirectory [256], messageFileName [256]; |
| 87 |
#if mac |
| 88 |
char *home; |
| 89 |
AEDesc programDescriptor; |
| 90 |
AppleEvent event, reply; |
| 91 |
OSStatus err; |
| 92 |
UInt32 signature; |
| 93 |
#elif win |
| 94 |
char homeDirectory [256], windowName [256]; |
| 95 |
HWND window; |
| 96 |
(void) timeOut; |
| 97 |
#elif unix |
| 98 |
char *home, *xdgHome, pidFileName [256]; |
| 99 |
FILE *pidFile; |
| 100 |
long pid, wid = 0; |
| 101 |
#endif |
| 102 |
|
| 103 |
/* |
| 104 |
* Clean up from an earlier call. |
| 105 |
*/ |
| 106 |
errorMessage [0] = '\0'; |
| 107 |
|
| 108 |
/* |
| 109 |
* Handle case differences. |
| 110 |
*/ |
| 111 |
strcpy (nativeProgramName, programName); |
| 112 |
#if unix |
| 113 |
nativeProgramName [0] = (char) tolower (nativeProgramName [0]); |
| 114 |
#else |
| 115 |
nativeProgramName [0] = (char) toupper (nativeProgramName [0]); |
| 116 |
#endif |
| 117 |
|
| 118 |
/* |
| 119 |
* If the text is going to be sent in a file, create its name. |
| 120 |
* The file is going to be written into the preferences folder of the receiving program. |
| 121 |
* On Linux, the name will be something like /home/jane/.config/praat/Message.txt. |
| 122 |
* On Windows, the name will be something like C:\Users\Jane\AppData\Roaming\Praat\Message.txt. |
| 123 |
* On macOS, the name will be something like /Users/Jane/Library/Application Support/Praat/Message.txt. |
| 124 |
*/ |
| 125 |
#if mac |
| 126 |
if ((home = getenv ("HOME")) == NULL) { |
| 127 |
sprintf (errorMessage, "HOME environment variable not set."); |
| 128 |
return errorMessage; |
| 129 |
} |
| 130 |
sprintf (preferencesDirectory, "%s/Library/Application Support/.%s", home, nativeProgramName); |
| 131 |
sprintf (messageFileName, "%s/%s", preferencesDirectory, "Message.txt"); |
| 132 |
#elif win |
| 133 |
if (! GetEnvironmentVariableA ("USERPROFILE", homeDirectory, 255)) { |
| 134 |
sprintf (errorMessage, "USERPROFILE environment variable not set."); |
| 135 |
return errorMessage; |
| 136 |
} |
| 137 |
sprintf (preferencesDirectory, "%s\\AppData\\Roaming\\%s", homeDirectory, nativeProgramName); |
| 138 |
sprintf (messageFileName, "%s\\%s", preferencesDirectory, "Message.txt"); |
| 139 |
#elif unix |
| 140 |
if ((home = getenv ("HOME")) == NULL) { |
| 141 |
sprintf (errorMessage, "HOME environment variable not set."); |
| 142 |
return errorMessage; |
| 143 |
} |
| 144 |
xdgHome = getenv ("XDG_CONFIG_HOME"); |
| 145 |
if (xdgHome) |
| 146 |
sprintf (preferencesDirectory, "%s/%s", xdgHome, nativeProgramName); |
| 147 |
else |
| 148 |
sprintf (preferencesDirectory, "%s/.config/%s", home, nativeProgramName); |
| 149 |
sprintf (messageFileName, "%s/%s", preferencesDirectory, "Message.txt"); |
| 150 |
#endif |
| 151 |
|
| 152 |
/* |
| 153 |
* Save the message file. |
| 154 |
*/ |
| 155 |
FILE *messageFile; |
| 156 |
if ((messageFile = fopen (messageFileName, "w")) == NULL) { |
| 157 |
sprintf (errorMessage, "Cannot create message file \"%s\" " |
| 158 |
"(no privilege to write to folder, or disk full, or program is not called %s).\n", messageFileName, programName); |
| 159 |
return errorMessage; |
| 160 |
} |
| 161 |
#if unix |
| 162 |
if (timeOut) |
| 163 |
fprintf (messageFile, "#%ld\n", (long) getpid ()); /* Write own process ID for callback. */ |
| 164 |
#endif |
| 165 |
if (fullTrust) |
| 166 |
fprintf (messageFile, "\n# --FULL-TRUST\n"); |
| 167 |
fprintf (messageFile, "%s", text); |
| 168 |
fclose (messageFile); |
| 169 |
|
| 170 |
/* |
| 171 |
* Where shall we send the message? |
| 172 |
*/ |
| 173 |
#if mac |
| 174 |
/* |
| 175 |
* Convert the program name to a macOS signature. |
| 176 |
* I know of no system routine for this, so I'll just translate the two most common names: |
| 177 |
*/ |
| 178 |
if (! strcmp (programName, "praat") || ! strcmp (programName, "Praat") || ! strcmp (programName, "PRAAT")) |
| 179 |
signature = 'PpgB'; |
| 180 |
else if (! strcmp (programName, "als") || ! strcmp (programName, "Als") || ! strcmp (programName, "ALS")) |
| 181 |
signature = 'CclA'; |
| 182 |
else |
| 183 |
signature = 0; |
| 184 |
AECreateDesc (typeApplSignature, & signature, 4, & programDescriptor); |
| 185 |
#elif win |
| 186 |
/* |
| 187 |
* Get the window handle of the "Objects" window of a running Praat-shell program. |
| 188 |
*/ |
| 189 |
sprintf (windowName, "PraatShell1 %s", programName); |
| 190 |
window = FindWindowA (windowName, NULL); |
| 191 |
if (! window) { |
| 192 |
sprintf (errorMessage, "Program %s not running (or an old version).", programName); |
| 193 |
return errorMessage; |
| 194 |
} |
| 195 |
#elif unix |
| 196 |
/* |
| 197 |
* Get the process ID and the window ID of a running Praat-shell program. |
| 198 |
*/ |
| 199 |
sprintf (pidFileName, "%s/pid", preferencesDirectory); |
| 200 |
if ((pidFile = fopen (pidFileName, "r")) == NULL) { |
| 201 |
sprintf (errorMessage, "Program %s not running.", programName); |
| 202 |
return errorMessage; |
| 203 |
} |
| 204 |
if (fscanf (pidFile, "%ld%ld", & pid, & wid) < 1) { |
| 205 |
fclose (pidFile); |
| 206 |
sprintf (errorMessage, "Program %s not running, or disk has been full.", programName); |
| 207 |
return errorMessage; |
| 208 |
} |
| 209 |
fclose (pidFile); |
| 210 |
#endif |
| 211 |
|
| 212 |
/* |
| 213 |
* Send the message. |
| 214 |
*/ |
| 215 |
#if mac |
| 216 |
/* |
| 217 |
* Notify the running program by sending it an Apple event of the magic class 758934755. |
| 218 |
*/ |
| 219 |
AECreateAppleEvent (758934755, 0, & programDescriptor, kAutoGenerateReturnID, 1, & event); |
| 220 |
//AEPutParamPtr (& event, 1, typeChar, text, (Size) strlen (text) + 1); |
| 221 |
err = AESendMessage (& event, & reply, |
| 222 |
( timeOut == 0 ? kAENoReply : kAEWaitReply ) | kAECanInteract | kAECanSwitchLayer, |
| 223 |
timeOut == 0 ? kNoTimeOut : 60 * timeOut); |
| 224 |
if (err != noErr) { |
| 225 |
if (err == procNotFound || err == connectionInvalid) |
| 226 |
sprintf (errorMessage, "Could not send message to program \"%s\".\n" |
| 227 |
"The program is probably not running (or an old version).", programName); |
| 228 |
else if (err == errAETimeout) |
| 229 |
sprintf (errorMessage, "Message to program \"%s\" timed out " |
| 230 |
"after %ld seconds, before completion.", programName, timeOut); |
| 231 |
else |
| 232 |
sprintf (errorMessage, "Unexpected sendpraat error %d.\nNotify the author.", err); |
| 233 |
} |
| 234 |
AEDisposeDesc (& programDescriptor); |
| 235 |
AEDisposeDesc (& event); |
| 236 |
AEDisposeDesc (& reply); |
| 237 |
#elif win |
| 238 |
/* |
| 239 |
* Notify the running program by sending a WM_APP message to its main window. |
| 240 |
*/ |
| 241 |
if (SendMessage (window, WM_APP, 0, 0)) { |
| 242 |
sprintf (errorMessage, "Program %s returns error.", programName); // BUG? |
| 243 |
return errorMessage; |
| 244 |
} |
| 245 |
#elif unix |
| 246 |
/* |
| 247 |
* Be ready to receive notification of completion. |
| 248 |
*/ |
| 249 |
if (timeOut) |
| 250 |
signal (SIGUSR2, handleCompletion); |
| 251 |
/* |
| 252 |
* Notify running program. |
| 253 |
*/ |
| 254 |
if (kill (pid, SIGUSR1)) { |
| 255 |
sprintf (errorMessage, "Cannot send message to %s (process %ld). " |
| 256 |
"The program %s may have been started by a different user, " |
| 257 |
"or may have crashed.", programName, pid, programName); |
| 258 |
return errorMessage; |
| 259 |
} |
| 260 |
/* |
| 261 |
* Wait for the running program to notify us of completion, |
| 262 |
* but do not wait for more than 'timeOut' seconds. |
| 263 |
*/ |
| 264 |
if (timeOut) { |
| 265 |
signal (SIGALRM, handleTimeOut); |
| 266 |
alarm (timeOut); |
| 267 |
theTimeOut = timeOut; // hand an argument to handleTimeOut () in a static variable |
| 268 |
errorMessage [0] = '\0'; |
| 269 |
pause (); |
| 270 |
if (errorMessage [0] != '\0') |
| 271 |
return errorMessage; |
| 272 |
} |
| 273 |
#endif |
| 274 |
|
| 275 |
/* |
| 276 |
* Notify the caller of success (NULL pointer) or failure (string with an error message). |
| 277 |
*/ |
| 278 |
return errorMessage [0] == '\0' ? NULL : errorMessage; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* To compile sendpraat7 as a stand-alone program, use the -DSTAND_ALONE option to the C compiler. |
| 283 |
* Because of the awkward syntax, this is recommended only for testing the sendpraat7 subroutine; |
| 284 |
* for sending scripts to a running Praat, use "praat --SEND <scriptFileName>" instead. |
| 285 |
*/ |
| 286 |
#ifdef STAND_ALONE |
| 287 |
/* |
| 288 |
To compile on MacOS X (ARM64 and x86_64 architectures combined): |
| 289 |
cc sendpraat7.c -arch arm64 -arch x86_64 -o sendpraat7-mac -DSTAND_ALONE -Dmacintosh -framework CoreServices -I/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/Headers -I/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers |
| 290 |
To compile on Windows from the MSYS2 CLANG64 shell: |
| 291 |
cc -std=gnu99 sendpraat7.c -o sendpraat7-win-x64.exe -DSTAND_ALONE -mconsole |
| 292 |
To compile on Windows from the MSYS2 CLANGARM64 shell: |
| 293 |
cc -std=gnu99 sendpraat7.c -o sendpraat7-win-arm64.exe -DSTAND_ALONE -mconsole |
| 294 |
To compile on Linux from Ubuntu x86_64: |
| 295 |
cc -std=gnu99 sendpraat7.c -o sendpraat7-linux-x64 -DSTAND_ALONE -DUNIX |
| 296 |
To compile on Linux from Ubuntu ARM64: |
| 297 |
cc -std=gnu99 sendpraat7.c -o sendpraat7-linux-arm64 -DSTAND_ALONE -DUNIX |
| 298 |
*/ |
| 299 |
int main (int argc, char **argv) { |
| 300 |
int iarg, line, length = 0, fullTrust = 0; |
| 301 |
long timeOut = 10; /* Default. */ |
| 302 |
char programName [64], *message, *result; |
| 303 |
if (argc == 1) { |
| 304 |
printf ("Syntax:\n"); |
| 305 |
printf (" sendpraat7 [<timeOut>] [--FULL-TRUST] <program> <message>\n"); |
| 306 |
printf ("\n"); |
| 307 |
printf ("Arguments:\n"); |
| 308 |
printf (" <timeOut>: the number of seconds that sendpraat7 will wait for an answer\n"); |
| 309 |
printf (" before writing an error message. A <timeOut> of 0 means that\n"); |
| 310 |
printf (" the message will be sent asynchronously, i.e., that sendpraat7\n"); |
| 311 |
printf (" will return immediately without issuing any error message.\n"); |
| 312 |
printf (" This option may be ignored on Windows.\n"); |
| 313 |
printf (" --FULL-TRUST: an optional flag that determines whether the script\n"); |
| 314 |
printf (" should be allowed to save files and call system commands.\n"); |
| 315 |
printf (" <program>: the name of a running program that uses the Praat shell.\n"); |
| 316 |
printf (" <message>: a sequence of Praat shell lines (commands and directives).\n"); |
| 317 |
printf ("\n"); |
| 318 |
printf ("Usage:\n"); |
| 319 |
printf (" Each line is a separate argument.\n"); |
| 320 |
printf (" Lines that contain spaces should be put inside double quotes.\n"); |
| 321 |
printf ("\n"); |
| 322 |
printf ("Examples:\n"); |
| 323 |
printf ("\n"); |
| 324 |
printf (" sendpraat7 praat Quit\n"); |
| 325 |
printf (" Causes the program \"praat\" to quit (gracefully).\n"); |
| 326 |
printf (" This works because \"Quit\" is a fixed command in Praat's Praat menu.\n"); |
| 327 |
printf (" Sendpraat will return immediately.\n"); |
| 328 |
printf ("\n"); |
| 329 |
printf (" sendpraat7 1000 praat \"Play reverse\"\n"); |
| 330 |
printf (" Causes the program \"praat\", which can play sounds,\n"); |
| 331 |
printf (" to play the selected Sound objects backwards.\n"); |
| 332 |
printf (" This works because \"Play reverse\" is an action command\n"); |
| 333 |
printf (" that becomes available in Praat's dynamic menu when Sounds are selected.\n"); |
| 334 |
printf (" Sendpraat will allow \"praat\" at most 1000 seconds to perform this.\n"); |
| 335 |
printf ("\n"); |
| 336 |
printf (" sendpraat7 praat --FULL-TRUST \"runScript: ~~/MyResearch/MyProject/MyScript.praat\"\n"); |
| 337 |
printf (" Causes the program \"praat\" to execute a script (obsolete; use `praat --SEND` instead).\n"); |
| 338 |
printf (" Sendpraat will allow \"praat\" at most 10 seconds (the default time out).\n"); |
| 339 |
exit (0); |
| 340 |
} |
| 341 |
iarg = 1; |
| 342 |
|
| 343 |
/* |
| 344 |
* Get time-out. |
| 345 |
*/ |
| 346 |
if (isdigit (argv [iarg] [0])) |
| 347 |
timeOut = atol (argv [iarg ++]); |
| 348 |
|
| 349 |
if (iarg >= argc) { |
| 350 |
fprintf (stderr, "sendpraat7: missing program name. Type \"sendpraat\" to get help.\n"); |
| 351 |
exit (1); |
| 352 |
} |
| 353 |
|
| 354 |
if (! strcmp (argv [iarg], "--FULL-TRUST")) { |
| 355 |
fullTrust = 1; |
| 356 |
iarg ++; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* Get program name. |
| 361 |
*/ |
| 362 |
if (iarg >= argc) { |
| 363 |
fprintf (stderr, "sendpraat7: missing program name. Type \"sendpraat7\" to get help.\n"); |
| 364 |
exit (1); |
| 365 |
} |
| 366 |
strcpy (programName, argv [iarg ++]); |
| 367 |
|
| 368 |
/* |
| 369 |
* Create the message string. |
| 370 |
*/ |
| 371 |
for (line = iarg; line < argc; line ++) |
| 372 |
length += strlen (argv [line]) + 1; |
| 373 |
length --; |
| 374 |
message = malloc (length + 1); |
| 375 |
message [0] = '\0'; |
| 376 |
for (line = iarg; line < argc; line ++) { |
| 377 |
strcat (message, argv [line]); |
| 378 |
if (line < argc - 1) |
| 379 |
strcat (message, "\n"); |
| 380 |
} |
| 381 |
|
| 382 |
/* |
| 383 |
* Send message. |
| 384 |
*/ |
| 385 |
result = sendpraat7 (timeOut, fullTrust, programName, message); |
| 386 |
if (result != NULL) { |
| 387 |
fprintf (stderr, "sendpraat7: %s\n", result); |
| 388 |
exit (1); |
| 389 |
} |
| 390 |
|
| 391 |
exit (0); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
#endif |
| 395 |
|
| 396 |
/* End of file sendpraat7.c */ |