Compiling
AppleScript is pre-compiled before you save. When you compile, the following "parts of speech" are formatted with distinct fonts and colors:
Uncompiled text
Operators
Language keywords
Application keywords
Comments
Values
Variables
References
You can change the fonts and colors in Macintosh Script Editor by doing the following:
On the Script Editor menu, click Preferences.
In the General dialog box, click Formatting, and then make the changes you want.
Code that you have just typed appears in the format for uncompiled code, as shown below:
tell app "Microsoft Word" save as active document file name myDocName file format format text end tell
If your script is written with proper syntax, after you click Compile, your code appears in the format for compiled code, which uses the fonts and colors you see in the Formatting pane.
Note Other methods to compile in Script Editor are:
Press ⌘+K
Press ENTER
When you compile, if you don't have any syntax errors, AppleScript searches for terms in its own language dictionary and parser, in the application dictionary of the
tell
block, and in installed scripting addition dictionaries. It determines which terms are AppleScript keywords, application keywords, quoted text, and other terms, and then it assumes anything left over is a variable.
Compiling also formats your code into blocks, using indentations.
Compiling errors
If there is an error, the script does not compile, and you get an error message. AppleScript error messages can by cryptic. Most of the time, an error is caused by one of two problems:
Typographical error A language or application keyword is interpreted as a variable. The words following the error, or preceding it if you mistyped the last or middle word of a multi-word keyword, in a statement now make no sense.
Text wrapping in copied and pasted code Code from a document or e-mail message contains long lines that have been hard-wrapped to a new line, causing syntax errors.
In the second case, you need to remove the infiltrating carriage return or add a continuation character (¬) to the end of the first broken line.
Occasionally, you may have all your keywords correct, and the script compiles without any problems, but then you get an error in execution when you run the script because your syntax is incorrect.
Note To make a continuation character (¬), press
OPTION+L.
Running scripts
Once you've got a script working, how do you save it and run it? You can test run your script in Script Editor by clicking Run, but you also need to save it and run it in Microsoft Word.
Unlike Visual Basic for Applications (VBA) macros, you cannot save a script in the document itself. Instead, you have the following options for saving a script:
Save as an application or application bundle
Save as a script or script bundle
Save as text
A script application is useful when you are developing a workflow that involves several applications, or when you create a "droplet" on which you can drop files. You create droplets by including an
on open
subroutine, and then saving it as an application.
However, script applications have the same liability as any application: After the file is double-clicked, there is a delay of a few seconds until it launches, as its icon bounces up and down in the Dock.
By contrast, scripts (.scpt) carry no such overhead when launched from an appropriate place, such as the Script menu. They run immediately, just like macros.
Saving your script as text is useful when you have to interrupt your work, but your script in incomplete and won't compile. You cannot use the other options if your script doesn't compile.
The Script menu
Unlike Microsoft Entourage, which has always had a Script menu, Microsoft Word 2004 for Mac, Excel 2004, and PowerPoint 2004 do not have their own script menus. However, the operating system has its own script menu. To enable it, follow the steps below for your operating system.
In Mac OS X v10.4 (Tiger):
In the Finder, click Applications, and then double-click AppleScript Utility.
In the AppleScript Utility dialog box, select the Show Script Menu in menu bar check box.
In Mac OS X v10.3 (Panther):
In the Finder, click Applications, click AppleScript, and then double-click Script Menu.
In both versions, a black "S" icon appears on the right side of the menu bar, near the clock.
To store your scripts, do the following:
On the Script menu, point to Open Scripts Folder, and then click Open User Scripts Folder.
This command creates and opens a Scripts folder at /Users/username/Library/Scripts. This is where you store your scripts to make them appear on the Script menu.
To run a script, you click its name on the Script menu.
If the menu starts to get too long, you can make subfolders inside your Scripts folder. You can also control the scripts that appear on the menu by showing only those that are associated with the application that is active and in the front of your screen. To do so, do the following:
Make the application that you want the active application.
On the Script menu, point to Open Scripts Folder, and then click Open application name Scripts Folder.
This procedure automatically creates an Applications folder inside of your Scripts folder, and a folder with the name of the active application inside of the Applications folder. Don't put scripts in the active application's folder that you want to call from another application, or in the Finder to start the active application.
Note If you don't have Mac OS X v10.4 or later, you must make these folders manually. You must name the active application's folder using the application name exactly as it appears on your computer.
This is one great advantage that AppleScript has over VBA. You can call a script for one application from another application. For example, you can call a Word script from Entourage that takes text selected in Entourage and uses it in Word. This action can even be performed automatically, in the background.
If you are used to initiating scripts from a button on a toolbar or from a keyboard shortcut, using the Script menu may seem like a limitation, but it is an excellent device for running scripts with no overhead.


