In Mac OS and Mac OS X, what is AppleScript, and how do I use it?
AppleScript is an object-oriented scripting language with a syntax resembling ordinary English. You can use it to automate repetitive tasks in applications, the Finder, and other parts of Mac OS and Mac OS X.
Here is a simple example of an AppleScript, which tells the Finder to empty the Trash:
Tell application "Finder" activate empty trash end tellCreating AppleScripts
If you are interested in writing your own AppleScript, the primary
tool you will use is the Script Editor, which is part of a standard
installation of Mac OS and Mac OS X. Scriptable applications contain
an AppleScript dictionary which lists available commands. To view a
dictionary in Script Editor, from the File menu, choose
Open Dictionary... . Then select the application in the
Open dialog box. Even if an application is not
extensively scriptable, it should contain the standard set of commands
which tell it to launch, quit, make a new document, and save a
document.
The simplest way to create a new script is to use the Record feature of the Script Editor, but this will work only if an application is both scriptable and recordable. To record a script, do the following:
- In the Script Editor, click the
Recordbutton.
- Perform the actions you want to record.
- Click the
Stopbutton.
When you have finished recording, you can click the Run
button to reproduce the actions that are recordable through
AppleScript.
Note: Not all commands in an AppleScript dictionary may be recordable.
Scripts can also be typed directly into the Script Editor. For help
with AppleScript syntax, consult either the built-in Script Editor
Help option, available from the Help menu, or Apple's AppleScript
Language Guide.
Saving AppleScripts
Once an AppleScript is recorded or written, you can save it in the following ways:
-
Text: This saves the script in text form,
uncompiled.
-
Compiled script: If your script is written with
the correct syntax, the Script Editor will compile the script after
you click the
Check Syntaxbutton or if you try to execute the script. Saved this way, the AppleScript can be run, but only by the Script Editor.
-
Application (Applet): Saved as an
applet, the script can be run as a stand-alone application.
To avoid a dialog box that asks if you want to run or quit the script
when it is launched, check the box that says
Never Show Startup Screenwhen saving the script.
-
Script bundle and application bundle: A bundle is
a folder designed to appear as a single file. Inside the folder bundle
are the code and resources used to execute the script. Both bundle
formats have the same save options as their non-bundle versions.
Note: Scripts saved in bundle format will not run on systems prior to Mac OS X 10.3.
Last modified on August 31, 2009.







