ScriptEditor

An aid to scripting.

The ScriptEditor is a text editor that allows you to edit, save, and run any Praat script. You can type such a script from scratch, but it is sometimes easier to use the History mechanism, which automatically records all your commands and mouse clicks, and which can paste these directly into a ScriptEditor.

To add a script as a button to a fixed or dynamic menu, use Add to fixed menu... or Add to dynamic menu... from the File menu.

Example 1

In this example, we create a fixed button that will play a 0.4-second sine wave with a specified frequency.

First, we create a ScriptEditor by choosing New Praat script from the Praat menu. Then, we choose Clear history from the Edit menu in the ScriptEditor. We then perform some actions that will create a sine wave, play it, and remove it:

1. Choose Create Sound as pure tone... from the New menu and click OK.
2. Click Play in the dynamic menu.
3. Click the fixed Remove button.

We then choose Paste history from the Edit menu in the ScriptEditor (or type Command-H). The text will now contain at least the following lines (delete any other lines):

    Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01
    Play
    Remove

We can run this script again by choosing Run from the Run menu (or typing Command-R). However, this always plays a sine with a frequency of 440 Hz, so we will add the variable "Frequency" to the script, which then looks like:

    #form Play a sine wave
       #positive Frequency
    #endform
    Create Sound as pure tone: "tone", 1, 0, 0.4, 44100, frequency, 0.2, 0.01, 0.01
    Play
    Remove

When we choose Run, the ScriptEditor will ask us to supply a value for the "Frequency" variable. We can now play 1-second sine waves with any frequency.

It is advisable to supply a standard value for each argument in your script. If the duration should be variable, too, the final script could look like:

    #form Play a sine wave
       #positive Frequency 440
       #positive Duration 1.0
    #endform
    Create Sound as pure tone: "tone", 1, 0, duration, 44100, frequency, 0.2, 0.01, 0.01
    Play
    Remove

When you run this script, the ScriptEditor will ask you to supply values for the two variables, but the values "440" and "1.0" are already visible in the form window, so that you will get a sensible result if you just click OK.

If this script is useful to you, you may want to put a button for it in the New menu, in the Sound submenu:

1. Save the script to a file, with Save from the File menu. The file name that you supply, will be shown in the title bar of the ScriptEditor window.
2. Choose Add to fixed menu... from the File menu. Supply Objects for the window, New for the menu, "Play sine wave..." for the command, Create Sound from formula... for after command, and "1" for the depth (because it is supposed to be in a submenu); the script argument has already been set to the file name that you supplied in step 1.
3. Click OK and ensure that the button has been added in the New menu. This button will still be there after you leave the program and enter it again; to remove it from the menu, use the ButtonEditor.

Example 2

In this example, we will create a shortcut for the usual complex pitch-analysis command.

First, we perform the required actions:

1. Select a Sound object.
2. Click To Pitch... and set the arguments to your personal standard values.
3. Click OK. A new Pitch object will appear.

We then paste the history into the ScriptEditor, after which this will contain at least a line like (delete all the other lines):

    To Pitch: 0.01, 150, 900

You can run this script only after selecting one or more Sound objects.

If this script is useful to you, you may want to put a button for it in the dynamic menu:

1. Save the script to a file, with Save from the File menu.
2. Choose Add to dynamic menu... from the File menu. Supply "Sound" for class1 (because the button is supposed to be available only if a Sound is selected), "0" for number1 (because the command is supposed to work for any number of selected Sound objects), "To Pitch (child)" for the command, "To Spectrum" for after command, and "0" for the depth (because it is not supposed to be in a submenu); the script argument has already been set to the file name that you supplied in step 1.
3. Click OK and ensure that the button is clickable if you select one or more Sound objects. This button will still be available after you leave the program and enter it again; to remove it from the dynamic menus, use the ButtonEditor.

Links to this page


© ppgb 20140107