| 1 | <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 2 | <title>Scripting 3.1. Hello world</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 3.1. Hello world |
| 13 | </b></font></table></table> |
| 14 | <p>Many manuals of computer programming languages start with their answer on the following question:</p> |
| 15 | <p><i>How do I write the text “Hello world” on the screen?</i></p> |
| 16 | <p>For the Praat scripting language, there are two answers.</p> |
| 17 | <h2>1. “Hello world” in the Info window</h2> |
| 18 | <p>The simplest answer is that you open the ScriptEditor window with <b>New Praat script</b> from the <b>Praat</b> menu, then type the following line into the ScriptEditor window:</p> |
| 19 | <code> writeInfoLine: “Hello world”<br></code> |
| 20 | <p>and finally choose <b>Run</b> from the <b>Run</b> menu.</p> |
| 21 | <p>When you try this, the result should be that the Info window comes to the front, and that it shows the text <code><font size=+1>Hello world</font></code>:</p> |
| 22 | <p align=middle><img height=300 width=600 src=Scripting_3_1__Hello_world_1.png></p><p>Now suppose that you want to write two lines instead of just one, so you try a script with two lines:</p> |
| 23 | <code> writeInfoLine: “Hello world”<br></code> |
| 24 | <code> writeInfoLine: “How do you do?”<br></code> |
| 25 | <p>This turns out not to do what you want: it seems to write only the text “How do you do?”. This happens because the <b>writeInfoLine</b> function first erases the Info window, then writes the line of text. So the first line of the script did write the text “Hello world”, but the second line wiped it out and wrote “How do you do?” instead. The script that does what you want is</p> |
| 26 | <code> writeInfoLine: “Hello world”<br></code> |
| 27 | <code> appendInfoLine: “How do you do?”<br></code> |
| 28 | <p>Now the result will be</p> |
| 29 | <p align=middle><img height=300 width=600 src=Scripting_3_1__Hello_world_2.png></p><p>This works because <a href="_appendInfoLine_.html"><b><code><font size=+1>appendInfoLine</font></code></b></a> writes a line without erasing the Info window first.</p> |
| 30 | <p>Finally, try the following script:</p> |
| 31 | <code> appendInfoLine: “Another try”<br></code> |
| 32 | <code> appendInfoLine: “Goodbye”<br></code> |
| 33 | <p>The result could be</p> |
| 34 | <p align=middle><img height=300 width=600 src=Scripting_3_1__Hello_world_3.png></p><p>In other words, <a href="_appendInfoLine_.html"><b><code><font size=+1>appendInfoLine</font></code></b></a> writes lines into the Info window without erasing it, even if you run a script anew. This is why many Praat scripts that write into the Info window do a <b>writeInfoLine</b> first, and follow it with a series of <a href="_appendInfoLine_.html"><b><code><font size=+1>appendInfoLine</font></code></b></a> calls.</p> |
| 35 | <p>For more information on these commands, see <a href="Scripting_6_2__Writing_to_the_Info_window.html">Scripting 6.2. Writing to the Info window</a>.</p> |
| 36 | <h2>2. “Hello world” in the Picture window.</h2> |
| 37 | <p>You can also show text in the Picture window. If you are an experienced Praat user, you have probably used the comamnd <b>Text top...</b> before. You can use it do draw a text at the top of the current <i>viewport</i>, which is the part of the Picture window where the next drawing will occur and which is marked by the pink <i>margins</i>. Thus, when you select the top 4×3 inches of the Picture window (with the mouse), set the font size to 12 (with the Pen menu), and run the script</p> |
| 38 | <code> Text top: “yes”, “Hello world”<br></code> |
| 39 | <p>then you’ll see</p> |
| 40 | <p align=middle><img height=450 width=600 src=Scripting_3_1__Hello_world_4.png></p><p>So this works the same as when you choose <b>Text top...</b> from the <b>Margins</b> menu by hand, with <b>Far</b> switched on.</p> |
| 41 | <p>If you want your script to always show the same text at the same position, with nothing else in the picture, then you can make your script a bit more extensive:</p> |
| 42 | <code> Erase all<br></code> |
| 43 | <code> Times<br></code> |
| 44 | <code> Font size: 12<br></code> |
| 45 | <code> Select outer viewport: 0, 4, 0, 3<br></code> |
| 46 | <code> Text top: “yes”, “Hello world”<br></code> |
| 47 | <p>In this script, line 1 erases the Picture window, so that nothing besides your text can appear in the Picture window.</p> |
| 48 | <p>Line 2 executes the command <b>Times</b> from the <b>Font</b> menu, so that the script will always draw the text in Times, even if you choose <b>Helvetica</b> in the Font menu with the mouse before you run the script (after the script has run, you’ll see that <b>Times</b> is chosen in the Font menu).</p> |
| 49 | <p>Line 3 executes the command <b>Font size...</b> from the <b>Font</b> menu, setting the font size to 12 and setting the width of the pink margins accordingly.</p> |
| 50 | <p>Line 4 executes the command <a href="Select_outer_viewport___.html">Select outer viewport...</a> from the Select menu. This performs an action that you would normally do by dragging the mouse, namely selecting the part of the Picture window that runs from 0 to 4 inches horizontally and from 0 to 3 inches vertically. After running the script, the <i>viewport</i> is indeed [0, 4] × [0, 3], as you can clearly see from the pink margins above.</p> |
| 51 | <p>Line 5 finally writes the text.</p> |
| 52 | <p>For more information on these commands, see <a href="Picture_window.html">Picture window</a>.</p> |
| 53 | <h3>Links to this page</h3> |
| 54 | <ul> |
| 55 | <li><a href="Scripting.html">Scripting</a> |
| 56 | <li><a href="Scripting_3__Simple_language_elements.html">Scripting 3. Simple language elements</a> |
| 57 | </ul> |
| 58 | <hr> |
| 59 | <address> |
| 60 | <p>© ppgb 20140111</p> |
| 61 | </address> |
| 62 | </body> |
| 63 | </html> |