| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 8.1. The sendpraat subroutine</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Scripting 8.1. The sendpraat subroutine |
| 13 | </b></font></table></table> |
| 14 | <p>Sendpraat can be a subroutine for sending messages to a <i>running</i> Praat program.</p> |
| 15 | <h2>C syntax</h2> |
| 16 | <code> <b>sendpraat7</b> (long <i>timeOut</i>, int fullTrust, const char *<i>program</i>, char *<i>text</i>);<br></code> |
| 17 | <h2>Arguments</h2> |
| 18 | <dl> |
| 19 | <dt><i><code><font size=+1>timeOut</font></code></i> (MacOS and Linux only) |
| 20 | <dd>the number of seconds that <code><font size=+1>sendpraat</font></code> will wait for an answer before writing an error message. A <i><code><font size=+1>timeOut</font></code></i> of 0 means that the message will be sent asynchronously, i.e., that sendpraat will return immediately without issuing any error message.</dd> |
| 21 | <dt><i><code><font size=+1>fullTrust</font></code></i> |
| 22 | <dd>a boolean (0 or 1) that determines whether the script should be allowed to save files and call system commands (1), or not (0).</dd> |
| 23 | <dt><i><code><font size=+1>program</font></code></i> |
| 24 | <dd>the name of a running program that uses the Praat shell, e.g. "Praat". The first letter may be specified as lower or upper case; it will be converted to upper case for Windows or MacOS and to lower case for Linux.</dd> |
| 25 | <dt><i><code><font size=+1>text</font></code></i> |
| 26 | <dd>the script text to be sent. Sendpraat may alter this text!</dd> |
| 27 | </dl> |
| 28 | <h2>Example 1: killing a program</h2> |
| 29 | <code> char message [100], *errorMessage;<br></code> |
| 30 | <code> strcpy (message, "Quit");<br></code> |
| 31 | <code> errorMessage = <b>sendpraat7</b> (0, 0, "praat", message);<br></code> |
| 32 | <code> if (errorMessage) fprintf (stderr, "%s", errorMessage);<br></code> |
| 33 | <p>This causes the program <b>Praat</b> to quit (gracefully), because <b>Quit</b> is a fixed command in one of the menus of that program. On MacOS and Linux, sendpraat returns immediately; on Windows, the <i>timeOut</i> argument is ignored. The return value <i>errorMessage</i> is a statically allocated string internal to sendpraat, and is overwritten by the next call to sendpraat.</p> |
| 34 | <h2>Example 2: playing a sound file in reverse</h2> |
| 35 | <p>Suppose you have a sound file whose name is in the variable <i><code><font size=+1>fileName</font></code></i>, and you want the program <b>Praat</b>, which can play sounds, to play this sound backwards.</p> |
| 36 | <code> char message [1000], *errorMessage;<br></code> |
| 37 | <code> snprintf (message,1000, "Read from file: ~%s\nPlay reverse\nRemove", fileName);<br></code> |
| 38 | <code> errorMessage = <b>sendpraat7</b> (3000, 0, "praat", message);<br></code> |
| 39 | <p>This will work because <b>Play reverse</b> is an action command that becomes available in the dynamic menu when a Sound is selected. On Linux, sendpraat will allow <b>Praat</b> at most 3000 seconds to perform this.</p> |
| 40 | <h2>Example 3: executing a large script file</h2> |
| 41 | <p>Sometimes, it may be unpractical to send a large script directly to <b>sendpraat</b>. Fortunately, the receiving program knows <b>runScript</b>:</p> |
| 42 | <code> char message [100], *errorMessage;<br></code> |
| 43 | <code> strcpy (message, "runScript: \"doAll.praat\", 20");<br></code> |
| 44 | <code> errorMessage = <b>sendpraat7</b> (0, 1, "praat", message);<br></code> |
| 45 | <p>This causes the program <b>Praat</b> to run the script <code><font size=+1>doAll.praat</font></code> with an argument of "20".</p> |
| 46 | <h2>How to download</h2> |
| 47 | <p>You can download the source code of the sendpraat subroutine via <b>https://praat.org/sendpraat.html</b> or from <b>https://www.fon.hum.uva.nl/praat/sendpraat.html</b>.</p> |
| 48 | <h2>Instead</h2> |
| 49 | <p>Instead of using <code><font size=+1>sendpraat</font></code>, you can also just take the following simple steps in your program:</p> |
| 50 | <dl> |
| 51 | <dd style="position:relative;padding-left:1em;text-indent:-2em">1. on Linux, write the Praat script that you want to run, and save it as <code><font size=+1>~/.config/Message.txt</font></code>;</dd> |
| 52 | <dd style="position:relative;padding-left:1em;text-indent:-2em">2. get Praat's process id from <code><font size=+1>~/.config/pid</font></code>;</dd> |
| 53 | <dd style="position:relative;padding-left:1em;text-indent:-2em">3. if Praat's process id is e.g. 1178, send it a SIGUSR1 signal: <code><font size=+1>kill -USR1 1178</font></code></dd> |
| 54 | </dl> |
| 55 | <p>If the first line of your script is the comment “<code><font size=+1># 999</font></code>”, where 999 stands for the process id of your program, Praat will send your program a SIGUSR2 signal back when it finishes handling the script. 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), then do not include such a line.</p> |
| 56 | <p>If the second line of your script is “<code><font size=+1># --FULL-TRUST</font></code>”</p> |
| 57 | <h2>See also</h2> |
| 58 | <p>To start a program from the command line instead and sending it a message, you would not use <b>sendpraat</b>, but instead run the program with a script file as an argument. See <a href="Scripting_6_9__Calling_from_the_command_line.html">Scripting 6.9. Calling from the command line</a>.</p> |
| 59 | <h3>Links to this page</h3> |
| 60 | <ul> |
| 61 | <li><a href="Scripting.html">Scripting</a> |
| 62 | <li><a href="Scripting_8__Controlling_Praat_from_another_program.html">Scripting 8. Controlling Praat from another program</a> |
| 63 | <li><a href="Scripting_8_2__The_sendpraat_program.html">Scripting 8.2. The sendpraat program</a> |
| 64 | </ul> |
| 65 | <hr> |
| 66 | <address> |
| 67 | <p>© Paul Boersma 2002,2003,2005,2009,2014,2015,2021,2023,2026</p> |
| 68 | </address> |
| 69 | </body> |
| 70 | </html> |