Satimage Previous | Next
Run a script in an AppleScript Terminal
Home Documentation Smile Smile Tutorial About AppleScript Run a script in an AppleScript Terminal  
Running one line

  • Make a new AppleScript terminal (or text window): pull down the File menu, or use the shortcut ⌘N. We shall call that window your AppleScript terminal. You can save that window at any time by selecting File ▸ Save in the menu bar.
  • Enter the following text: 1+2+3 (only that, no carriage return after the text).
  • Run the line using the ⌘R shortcut. The styles of the line changes, and spaces get inserted: the line first compiles, then it runs.
  • You should see the result of the run, 6, in a window named Console. If it was not visible before, the Console window should open automatically to display that result. (Use the Window menu to retrieve the Console.) By default, script results go to the end of the Console window - the other option is to have the results print at the end of the very window which contains the script. In the following, the console refers to the Console window.

Running a script

  • Enter a new line in your AppleScript terminal with the following simple instruction: beep.
  • Select the whole text (should be two lines), then press ⌘R.
    This runs the two lines. Any script returns as its result the result of its last instruction. Since beep just beeps and does not return a result, no new text gets printed in the console.
  • Now, set the cursor in the first line (at any location).
    • Press ⌘R once. The first line runs (its result goes to the console), and the cursor jumps to the next line.
    • Press ⌘R again. The second line runs.

    This is what running a script line by line refers to. When you run a script line by line, the console displays the results of each line, which may be convenient when you are tracking some bug. (You can send the result to the console even if you expect large data: the console will truncate any too large result to its 10,000 first characters.)

Commenting your script
A script may include comments: text following two successive hyphens -- and up to the end of the line is not considered.
1+2+3 -- your comments go here
beep
Evaluating an expression on the fly

  • Now, select only the following piece of the first line: 2+3, then press ⌘R.
    The result (5) prints to the console: when you press ⌘R, you really made the selected text into a (short) script, that you ran.

  • Now append a (bogus) + to the first instruction, select the whole line then press ⌘R.
    An error occurs while executing the script, and an error message is displayed. AppleScript hilites the piece of text which makes the problem: the end of the line is empty while it was expecting a number, or an expression.

Now on ...

You now can run a script in (or, "from") an AppleScript terminal (or, "text window"), line by line, block by block, as a whole, or by selecting a particular piece of the text: just press ⌘R. Now on we shall use the expression "run the script" to describe these operations, entering the script in an AppleScript terminal and pressing ⌘R.

If you are eager to make your own data visualization script, you may want to jump directly to the Chapter 2 of the tutorial.
The next pages describe how to handle variables in AppleScript.

Copyright ©2008 Paris, Satimage