| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Formulas 1.8. Formulas in scripts</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 | Formulas 1.8. Formulas in scripts |
| 13 | </b></font></table></table> |
| 14 | <p>In scripts, you can assign numeric expressions to numeric variables, string expressions to string variables, and array expressions to array variables. You can also use numeric, string and array variables in expressions.</p> |
| 15 | <h2>Example: report a square</h2> |
| 16 | <p>Choose <a href="New_Praat_script.html">New Praat script</a> from the <a href="Praat_menu.html">Praat menu</a>. A script editor window will become visible. Type the following lines into that window:</p> |
| 17 | <code> x = 99<br></code> |
| 18 | <code> x2 = x * x<br></code> |
| 19 | <code> writeInfoLine: “The square of ”, x, “ is ”, x2, “.”<br></code> |
| 20 | <p>This is an example of a simple <a href="Praat_script.html">Praat script</a>; it assigns the results of the numeric formulas <code><font size=+1>99</font></code> and <code><font size=+1>x * x</font></code> to the numeric variables <i><code><font size=+1>x</font></code></i> and <i><code><font size=+1>x2</font></code></i>. Note that the formula <code><font size=+1>x * x</font></code> itself refers to the variable <i><code><font size=+1>x</font></code></i>. To run (execute) this script, type <a href="Keyboard_shortcuts.html">Command-R</a> or choose <b>Run</b> from the Run menu. Praat will then write the following text into the Info window:</p> |
| 21 | <code> The square of 99 is 9801.<br></code> |
| 22 | <p>For more information on scripts, see the <a href="Scripting.html">Scripting</a> tutorial.</p> |
| 23 | <h2>Example: rename the city of Washington</h2> |
| 24 | <p>Type the following text into the script editor window:</p> |
| 25 | <code> current$ = “Bush”<br></code> |
| 26 | <code> previous$ = “Clinton”<br></code> |
| 27 | <code> famous$ = “Lincoln”<br></code> |
| 28 | <code> newCapital$ = current$ + mid$ (famous$, 2, 3) + right$ (previous$, 3)<br></code> |
| 29 | <code> writeInfoLine: “The new capital will be ”, newCapital$, “.”<br></code> |
| 30 | <p>This script assigns the results of four string expressions to the four string variables <code><font size=+1>current$</font></code>, <code><font size=+1>previous$</font></code>, <code><font size=+1>famous$</font></code>, and <code><font size=+1>newCapital$</font></code>. The dollar sign is the notation for a string variable or for a function whose result is a string (like <code><font size=+1>left$</font></code>). Note that the formula in the fourth line refers to three existing variables.</p> |
| 31 | <p>To see what the new name of the capital will be, choose <b>Run</b>.</p> |
| 32 | <h2>Example: report five squares</h2> |
| 33 | <p>Type the following script:</p> |
| 34 | <code> x# = { 1, 2, 3, 4, 5 }<br></code> |
| 35 | <code> x2# = x# * x#<br></code> |
| 36 | <code> writeInfoLine: “The squares of ”, x#, “ are ”, x2#, “.”<br></code> |
| 37 | <p>Praat will then write the following text into the Info window:</p> |
| 38 | <code> The squares of 1 2 3 4 5 are 1 4 9 16 25.<br></code> |
| 39 | <h2>Example: numeric expressions in settings in scripts</h2> |
| 40 | <p>As in real settings windows, you can use numeric expressions in all numeric fields. The example of two pages back becomes:</p> |
| 41 | <code> Create Sound from formula: “sine”, 1, 0, 10000 / 44100, 44100, ~ 0.9 * sin (2*pi*377*x)<br></code> |
| 42 | <h2>Example: string expressions in settings in scripts</h2> |
| 43 | <p>As in real settings windows, you can use string expressions in all text fields:</p> |
| 44 | <code> soundName$ = “hello”<br></code> |
| 45 | <code> Read from file: soundName$ + “.wav”<br></code> |
| 46 | <h2>Example: numeric expressions in creation in scripts</h2> |
| 47 | <p>Suppose you want to generate a sine wave whose frequency is held in a variable. This is the way:</p> |
| 48 | <code> frequency = 377<br></code> |
| 49 | <code> Create Sound from formula: “sine”, 1, 0.0, 1.0, 44100, ~ 0.9 * sin (2*pi*frequency*x)<br></code> |
| 50 | <p>In this example, Praat will protest if <code><font size=+1>x</font></code> is a variable as well (<i><code><font size=+1>x</font></code></i>), because that would be ambiguous with the <code><font size=+1>x</font></code> that refers to the time in the sound (see <a href="Formulas_1_7__Formulas_for_modification.html">Formulas 1.7. Formulas for modification</a>).</p> |
| 51 | <h3>Links to this page</h3> |
| 52 | <ul> |
| 53 | <li><a href="Formulas.html">Formulas</a> |
| 54 | <li><a href="Formulas_1__My_first_formulas.html">Formulas 1. My first formulas</a> |
| 55 | <li><a href="Formulas_1_5__Formulas_in_settings_windows.html">Formulas 1.5. Formulas in settings windows</a> |
| 56 | </ul> |
| 57 | <hr> |
| 58 | <address> |
| 59 | <p>© ppgb 20170916</p> |
| 60 | </address> |
| 61 | </body> |
| 62 | </html> |