Debian Codegraph @

docs/manual/Scripting_1__Your_first_scripts.html

raw symbols SCIP
1 <html><head><meta name="robots" content="index,follow"><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
2 <title>Scripting 1. Your first 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 Scripting 1. Your first scripts
13 </b></font></table></table>
14 <p>This page tells you how to create, run and save a script. To get a feel for how it works, you are advised to try out all the steps.</p>
15 <h2>1. A minimal script</h2>
16 <p>Suppose that you want to create a script that allows you to play a selected Sound object twice. You first create an empty script, by choosing <a href="New_Praat_script.html">New Praat script</a> from the Praat menu in the Objects window. A <a href="ScriptEditor.html">ScriptEditor</a> window will appear on your screen:</p>
17 <p align=middle><img height=400 width=600 src=Scripting_1__Your_first_scripts_1.png></p><p>In this window, you type</p>
18 <code> Play<br></code>
19 <code> Play<br></code>
20 <p>Now select a Sound in the Objects window. As you expect from selecting a Sound, a <b>Play</b> button will appear in the dynamic menu. If you now choose <b>Run</b> from the Run menu in the ScriptEditor, Praat will play the sound twice. This works because <b>Play</b> is a command that becomes available in the dynamic menu when you select a Sound.</p>
21 <h2>2. Some more commands</h2>
22 <p>In the above example, you could use <code><font size=+1>Play</font></code> in your script because the <b>Play</b> command was available, i.e. because &#8220;Play&#8221; was the text on a button currently available in the dynamic menu. Apart from these selection-dependent (dynamic) commands, you can also use all fixed commands from the menus of the <a href="Objects_window.html">Objects window</a> and the <a href="Picture_window.html">Picture window</a>. For instance, try the following script:</p>
23 <code> Erase all<br></code>
24 <code> Draw inner box<br></code>
25 <code> Play<br></code>
26 <code> Play<br></code>
27 <code> Erase all<br></code>
28 <p>When you run this script, you&#8217;ll see a rectangle appear in the Picture window (that&#8217;s what the command <b>Draw inner box</b> in the <b>Margins</b> menu does), then you&#8217;ll hear the Sound play twice, then you&#8217;ll see the rectangle disappear from the Picture window (that&#8217;s what the command <b>Erase all</b> from the Edit menu does).</p>
29 <p>Here we see that the Praat scripting language is an example of a <i>procedural programming language</i>, which means that the five <i>statements</i> are executed in the order in which they appear in the script, i.e. first <b>Erase all</b>, then <b>Draw inner box</b>, then <b>Play</b> twice, and finally <b>Erase all</b>.</p>
30 <h2>3. Experimenting with your script</h2>
31 <p>You don't have to be afraid of making mistakes. Here are a couple that you can try to make.</p>
32 <p>First, try to run the script when a Sound is not selected (e.g. you create a Pitch object from it and keep that selected, or you throw away the Sound). You will notice that Praat gives you an error message saying &#8220;The command &#8220;Play&#8221; is not available for the current selection&#8221;. Indeed, if you select a Pitch or if you select nothing, then no command <b>Play</b> appears in the dynamic menu, so the script cannot execute it. Note that the commands <b>Erase all</b> and <b>Draw inner box</b> are still available, because they continue to be present in the menus of the Picture window; therefore, the script will execute the first two lines (<code><font size=+1>Erase all</font></code> and <code><font size=+1>Draw inner box</font></code>) and stop running at the third line, i.e. at your first <code><font size=+1>Play</font></code>. The result is that the &#8220;box&#8221; will stay visible in the Picture window, because the fifth line of the script, which should erase the box, is never executed.</p>
33 <p>Second, try to mistype a command (there&#8217;s a good chance you already did it by accident), e.g. write <code><font size=+1>PLay</font></code> instead of <code><font size=+1>Play</font></code>, or <code><font size=+1>Draw inner bocks</font></code> or whatever. Again, you are likely to get a message saying that that command is not available. Such messages are the most common messages that you&#8217;ll see when writing scripts; now you know that they mean either that you mistyped something or that you made the wrong selection.</p>
34 <h2>4. Saving your script</h2>
35 <p>The File menu of the ScriptEditor has a command <b>Save</b>, with which you can save your script as a file on disk, for instance under the name &#8220;test.praat&#8221;.</p>
36 <p>Please try this with the five-line script you just typed. After saving the script, the name of the script file will appear in the window title:</p>
37 <p align=middle><img height=400 width=600 src=Scripting_1__Your_first_scripts_2.png></p><p>After you save your script, you can close the ScriptEditor window without losing the script: you can reopen the script file by using <a href="Open_Praat_script___.html">Open Praat script...</a> from the Praat menu, or by choosing <b>New Praat script</b> again, followed by <b>Open...</b> from the ScriptEditor&#8217;s File menu.</p>
38 <p>It advisable to use &#8220;.praat&#8221; as the extension for script file names. On the Mac, if you double-click a &#8220;.praat&#8221; file, Praat will automatically start up and show the script. On the Mac and on Windows, if you drag a &#8220;.praat&#8221; file on the Praat icon, Praat will also start up and show the script.</p>
39 <h3>Links to this page</h3>
40 <ul>
41 <li><a href="Scripting.html">Scripting</a>
42 <li><a href="Scripting_2__How_to_script_settings_windows.html">Scripting 2. How to script settings windows</a>
43 <li><a href="Scripting_3__Simple_language_elements.html">Scripting 3. Simple language elements</a>
44 </ul>
45 <hr>
46 <address>
47 <p>© Paul Boersma 2000,2002,2004,2005,2008(“My first script”),2011,2013,2014,2020,2023</p>
48 </address>
49 </body>
50 </html>