| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 6.6. Controlling the user</title> |
| 3 | <style> |
| 4 | td { padding-left: 5pt; padding-right: 5pt; } |
| 5 | th { padding-left: 5pt; padding-right: 5pt; } |
| 6 | code { white-space: pre-wrap; } |
| 7 | dd { white-space: pre-wrap; } |
| 8 | </style> |
| 9 | </head><body bgcolor="#FFFFFF"> |
| 10 | |
| 11 | <table border=0 cellpadding=0 cellspacing=0><tr><td bgcolor="#CCCC00"><table border=4 cellpadding=9><tr><td align=middle bgcolor="#000000"><font face="Palatino,Times" size=6 color="#999900"><b> |
| 12 | Scripting 6.6. Controlling the user |
| 13 | </b></font></table></table> |
| 14 | <p>You can temporarily halt a Praat script:</p> |
| 15 | <dl> |
| 16 | <dt><b><code><font size=+1>pauseScript</font></code></b>: <i>message</i> |
| 17 | <dd>suspends execution of the script, and allows the user to interrupt it. A message window will appear with the <i>message</i> (you can use the same argument list as with <a href="_writeInfoLine_.html"><b><code><font size=+1>writeInfoLine</font></code></b></a>) and the buttons <b>Stop</b> and <b>Continue</b>:</dd> |
| 18 | </dl> |
| 19 | <code> <b>pauseScript</b>: “The next file will be ”, fileName$<br></code> |
| 20 | <p>The <b><code><font size=+1>pauseScript</font></code></b> function is useful if you want to send a simple message to the user, and you only want to ask the user whether she wants to proceed or not. More interesting interactions between your script and the user are possible with the <b><i>pause window</i></b>. In a pause window you can include the same kinds of arguments as in a <a href="Scripting_6_1__Arguments_to_the_script.html">form</a>. Here is an extensive example:</p> |
| 21 | <code> writeInfoLine: “script”<br></code> |
| 22 | <code> compression = 1<br></code> |
| 23 | <code> number_of_channels = 2<br></code> |
| 24 | <code> worth = 3<br></code> |
| 25 | <code> for i to 5<br></code> |
| 26 | <code> <b>beginPause</b>: “Hi”<br></code> |
| 27 | <code> <b>comment</b>: “Type a lot of nonsense below.”<br></code> |
| 28 | <code> <b>natural</b>: “Number of people”, 10<br></code> |
| 29 | <code> <b>real</b>: “Worth”, worth + 1<br></code> |
| 30 | <code> <b>positive</b>: “Sampling frequency (Hz)”, “44100.0 (= CD quality)”<br></code> |
| 31 | <code> <b>word</b>: “hi”, “hhh”<br></code> |
| 32 | <code> <b>sentence</b>: “lo”, “two words”<br></code> |
| 33 | <code> <b>text</b>: “shortText”, “some one-line text here”<br></code> |
| 34 | <code> <b>text</b>: 2, “longText”, “some scrollable text here, within a height of 2 lines”<br></code> |
| 35 | <code> <b>boolean</b>: “You like it?”, 1<br></code> |
| 36 | <code> <b>infile</b>: “Input file”, “hello.wav”<br></code> |
| 37 | <code> <b>outfile</b>: 2, “Output file”, “../out.txt”<br></code> |
| 38 | <code> <b>realvector</b>: 2, “Array of reals”, “(whitespace-separated)”, “20 80 60”<br></code> |
| 39 | <code> if worth < 6<br></code> |
| 40 | <code> <b>choice</b>: “Compression”, compression<br></code> |
| 41 | <code> <b>option</b>: “lossless (FLAC)”<br></code> |
| 42 | <code> <b>option</b>: “MP3”<br></code> |
| 43 | <code> <b>option</b>: “Ogg”<br></code> |
| 44 | <code> endif<br></code> |
| 45 | <code> <b>optionmenu</b>: “Number of channels”, number_of_channels<br></code> |
| 46 | <code> <b>option</b>: “mono”<br></code> |
| 47 | <code> <b>option</b>: “stereo”<br></code> |
| 48 | <code> <b>option</b>: “quadro”<br></code> |
| 49 | <code> <b>comment</b>: “Then click Stop or one of the continuation buttons.”<br></code> |
| 50 | <code> clicked = <b>endPause</b>: “Continue”, “Next”, “Proceed”, 2<br></code> |
| 51 | <code> appendInfoLine: number_of_people, “ ”, worth, “ ”, sampling_frequency, “ ”, clicked<br></code> |
| 52 | <code> appendInfoLine: “Compression: ”, compression, “ (”, compression$, “)”<br></code> |
| 53 | <code> appendInfoLine: “Number of channels: ”, number_of_channels$<br></code> |
| 54 | <code> appendInfoLine: “Short text: ”, shortText$<br></code> |
| 55 | <code> appendInfoLine: “Input file: ”, input_file$<br></code> |
| 56 | <code> appendInfoLine: “Array: ”, array_of_reals#<br></code> |
| 57 | <code> endfor<br></code> |
| 58 | <p>This example uses several tricks. A useful one is seen with <code><font size=+1>number_of_channels</font></code>: this is at the same time the value that is passed to <b><code><font size=+1>optionmenu</font></code></b> (and therefore determines the setting of the <b>Number of channels</b> menu when the window appears) and the name of the variable in which the user’s chosen value of <b>Number of channels</b> is stored (because the text “number_of _channels” is what you get by replacing the spaces in “Number of channels” with underscores and turning its first letter to lower case).</p> |
| 59 | <p>Your own pause windows are not likely to be as rich as the above example. For instance, the example has three continuation buttons (the second of these is the default button, i.e. the button that you can “click” by pressing the Enter or Return key). You will often use only one continuation button, for instance</p> |
| 60 | <code> <b>endPause</b>: “Continue”, 1<br></code> |
| 61 | <p>or</p> |
| 62 | <code> <b>endPause</b>: “Finish”, 1<br></code> |
| 63 | <p>or</p> |
| 64 | <code> <b>endPause</b>: “OK”, 1<br></code> |
| 65 | <p>If your script shows multiple different pause windows, then it is in fact a <i>wizard</i>, and it becomes useful to have</p> |
| 66 | <code> <b>endPause</b>: “Next”, 1<br></code> |
| 67 | <p>for most of them, and</p> |
| 68 | <code> <b>endPause</b>: “Finish”, 1<br></code> |
| 69 | <p>for the last one.</p> |
| 70 | <p>The possibility of multiple continuation buttons can save the user a mouse click. The following script, for instance, requires two mouse clicks per sound:</p> |
| 71 | <code> for %i to 20<br></code> |
| 72 | <code> Read from file: “sound“ + string$ (i) + “.wav”<br></code> |
| 73 | <code> Play<br></code> |
| 74 | <code> Remove<br></code> |
| 75 | <code> #beginPause: “Rate the quality”<br></code> |
| 76 | <code> #comment: “How good is the sound on a scale from 1 to 7?”<br></code> |
| 77 | <code> #choice: “Quality”, 4<br></code> |
| 78 | <code> #option: “1”<br></code> |
| 79 | <code> #option: “2”<br></code> |
| 80 | <code> #option: “3”<br></code> |
| 81 | <code> #option: “4”<br></code> |
| 82 | <code> #option: “5”<br></code> |
| 83 | <code> #option: “6”<br></code> |
| 84 | <code> #option: “7”<br></code> |
| 85 | <code> <b>endPause</b>: if i = 20 then “Finish” else “Next” fi, 1<br></code> |
| 86 | <code> appendInfoLine: quality<br></code> |
| 87 | <code> endfor<br></code> |
| 88 | <p>The following script works faster:</p> |
| 89 | <code> for i to 20<br></code> |
| 90 | <code> Read from file: “sound” + string$ (i) + “.wav”<br></code> |
| 91 | <code> Play<br></code> |
| 92 | <code> Remove<br></code> |
| 93 | <code> #beginPause: “Rate the quality"<br></code> |
| 94 | <code> #comment: “How good is the sound on a scale from 1 to 7?”<br></code> |
| 95 | <code> quality = <b>endPause</b>: “1”, “2”, “3”, “4”, “5”, “6”, “7”, 0<br></code> |
| 96 | <code> appendInfoLine: quality<br></code> |
| 97 | <code> endfor<br></code> |
| 98 | <p>In this example, the 0 at the end of <b>endPause</b> means that there is no default button.</p> |
| 99 | <h2>File selection</h2> |
| 100 | <p>If you want the user to choose a file name for reading (opening), do</p> |
| 101 | <code> fileName$ = <b>chooseReadFile$</b>: “Open a table file"<br></code> |
| 102 | <code> if fileName$ <> “”<br></code> |
| 103 | <code> table = Read Table from tab-separated file: fileName$<br></code> |
| 104 | <code> endif<br></code> |
| 105 | <p>A file selector window will appear, with (in this example) <b>Open a table file</b> as the title. If the user clicks <b>OK</b>, the variable <i><code><font size=+1>fileName$</font></code></i> will contain the name of the file that the user selected; if the user clicks <b>Cancel</b>, the variable <i><code><font size=+1>fileName$</font></code></i> will contain the empty string (“”).</p> |
| 106 | <p>If you want the user to choose a file name for writing (saving), do</p> |
| 107 | <code> selectObject: mySound<br></code> |
| 108 | <code> fileName$ = <b>chooseWriteFile$</b>: “Save as a WAV file”, “mySound.wav”<br></code> |
| 109 | <code> if fileName$ <> “”<br></code> |
| 110 | <code> Save as WAV file: fileName$<br></code> |
| 111 | <code> endif<br></code> |
| 112 | <p>A file selector window will appear, with (in this example) <b>Save as a WAV file</b> as the title and “mySound.wav” as the suggested file name (which the user can change). If the user clicks <b>OK</b>, the form will ask for confirmation if the file name that the user typed already exists. If the user clicks <b>OK</b> with a new file name, or clicks <b>OK</b> in the confirmation window, the variable <i><code><font size=+1>fileName$</font></code></i> will contain the file name that the user typed; if the user clicks <b>Cancel</b> at any point, the variable <i><code><font size=+1>fileName$</font></code></i> will contain the empty string (“”).</p> |
| 113 | <p>If you want the user to choose a folder (directory) name, do</p> |
| 114 | <code> folderName$ = <b>chooseFolder$</b>: “Choose a folder to save all the new files in”<br></code> |
| 115 | <code> if folderName$ <> “”<br></code> |
| 116 | <code> for i to numberOfSelectedSounds<br></code> |
| 117 | <code> selectObject: sound [i]<br></code> |
| 118 | <code> Save as WAV file: folderName$ + “/sound” + string$ (i) + “.wav”<br></code> |
| 119 | <code> endfor<br></code> |
| 120 | <code> endif<br></code> |
| 121 | <p>A folder selector window will appear, with (in this example) <b>Choose a folder to save all the new files in</b> as the title. If the user clicks <b>OK</b>, the variable <i><code><font size=+1>folderName$</font></code></i> will contain the name of the folder that the user selected; if the user clicks <b>Cancel</b>, the variable <i><code><font size=+1>folderName$</font></code></i> will contain the empty string (“”).</p> |
| 122 | <h2>A non-pausing pause window without a <b>Stop</b> button</h2> |
| 123 | <p>Especially if you use the pause window within the <a href="Demo_window.html">Demo window</a>, you may not want to give the user the capability of ending the script by hitting <b>Stop</b> or closing the pause window. In that case, you can add an extra numeric argument to <b><code><font size=+1>endPause</font></code></b> (at the end) that denotes the cancel button:</p> |
| 124 | <code> <b>beginPause</b>: “Learning settings”<br></code> |
| 125 | <code> <b>positive</b>: “Learning rate”, “0.01”<br></code> |
| 126 | <code> <b>choice</b>: “Directions”, 3<br></code> |
| 127 | <code> <b>option</b>: “Forward”<br></code> |
| 128 | <code> <b>option</b>: “Backward”<br></code> |
| 129 | <code> <b>option</b>: “Bidirectional”<br></code> |
| 130 | <code> clicked = <b>endPause</b>: “Cancel”, “OK”, 2, 1<br></code> |
| 131 | <code> if clicked = 2<br></code> |
| 132 | <code> learningRate = learning_rate<br></code> |
| 133 | <code> includeForward = ( directions = 1 or directions = 3 )<br></code> |
| 134 | <code> includeBackward = ( directions = 2 or directions = 3 )<br></code> |
| 135 | <code> endif<br></code> |
| 136 | <p>In this example, the default button is 2 (i.e. <b>OK</b>), and the cancel button is 1 (i.e. <b>Cancel</b>). The form will now contain no <b>Stop</b> button, and if the user closes the window, this will be the same as clicking <b>Cancel</b>, namely that <i><code><font size=+1>clicked</font></code></i> will be 1 (because the <b>Cancel</b> button is the first button) and the variables <i><code><font size=+1>learning_rate</font></code></i>, <i><code><font size=+1>directions</font></code></i> and <i><code><font size=+1>directions$</font></code></i> will not be changed (i.e. they might remain undefined).</p> |
| 137 | <h2>Pausing for a fixed time without a window</h2> |
| 138 | <p>You can pause Praat for 1.3 seconds by saying</p> |
| 139 | <code> <b>sleep</b> (1.3)<br></code> |
| 140 | <p>This is of course not about controlling the user, but it is mentioned here because this section is about pausing.</p> |
| 141 | <h3>Links to this page</h3> |
| 142 | <ul> |
| 143 | <li><a href="_pauseScript_.html"><code><font size=+1>pauseScript</font></code></a> |
| 144 | <li><a href="pause_window.html">pause window</a> |
| 145 | <li><a href="Scripting.html">Scripting</a> |
| 146 | <li><a href="Scripting_6__Communication_outside_the_script.html">Scripting 6. Communication outside the script</a> |
| 147 | <li><a href="What_was_new_in_5_1_.html">What was new in 5.1?</a> |
| 148 | <li><a href="What_was_new_in_5_2_.html">What was new in 5.2?</a> |
| 149 | <li><a href="What_was_new_in_5_3_.html">What was new in 5.3?</a> |
| 150 | </ul> |
| 151 | <hr> |
| 152 | <address> |
| 153 | <p>© ppgb 20230130</p> |
| 154 | </address> |
| 155 | </body> |
| 156 | </html> |