Creating a Simple Script


An easy way to see how a script looks and works is to type a simple script into a new script window. If Script Editor is not already the active application, open it or switch to it. If you need to create a new script window, choose File New Script. In the script-editing area at the bottom of the new script window, type the following statements:

tell application "Finder" activate set the bounds of the first Finder window to {128, 74, 671, 479} set the current view of the first Finder window to icon view set the icon size of the icon view options of the first Finder window to 32 select the first item of the first Finder window end tell 

Check your script for typographical errors by clicking the Compile button in the script window. If Script Editor reports an error, carefully compare the statement you typed in the script window to the same statement in the book. Pay particular attention to spelling, punctuation, omitted words, and omitted spaces.

When you click Compile, Script Editor formats your script, changing the text formatting as it compiles the script using different type styles to show different kinds of terms. The statements that you typed probably changed from Courier font to Verdana font after you clicked Compile. Script Editor normally formats text that hasn’t been compiled as 10-point Courier. Most other words, including commands from scripting additions and application dictionaries, are normally formatted in plain 10-point Verdana. Verdana 10-point Bold normally indicates native words in the AppleScript language. Figure 23-2 shows how Script Editor formats the script that you typed.

click to expand
Figure 23-2: Check your script for errors and format it for readability by clicking the Compile button.

Before running this script, return to the Finder and make sure that a Finder window is displayed. If you really want to see the script in action, set the front Finder window to list view and resize the window so that it is very small.

After setting the stage for the script, switch back to Script Editor and click Run in your script’s window. AppleScript executes each script statement in turn. When the script finishes running, the Finder window should be a standard size and set to icon view. The item that comes first alphabetically in the window should be selected.

Switch to Script Editor again and examine the script. You find that the script is fairly understandable. It may not be fluent English, but many of the commands should make sense as you read them.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net