Application ScriptingScriptable applications provide a varying degree of support for AppleScript. Some simply allow scripts to emulate menu items whilst others provide more advanced controls.
Most applications support a core suite of commands that let you open, save, print and get information about documents. Some programs also provide extra features within this suite.
Use of the core suite is demonstrated below, in this instance with a useful scriptable text-editing application known as Tex-Edit Plus (Tom Bender). Some are shown in pairs, although this wouldn’t be a sensible thing to do in a real script.
quit application
quit application
tell application
open file "Macintosh HD:
end tell
print every window of
print last window of
close every window of
close windows 2 through 4 of
Here’s an example script that closes down the iTunes application after a prescribed time, which is given in seconds:-
tell application "iTunes"
delay 5400
quit
end tell
The Apple Event Object Model (AEOM) is used in the core suite and other application-specific suites. The following script shows some the form of these instructions:-
tell application
set numWndws to count
if not (front window exists)
set theText to selection
set beginning of selection to
set end of selection to "mouse"
set wndwName to name of window 1
set name of window 1 to
end tell
As you can see, this example produces a dialogue if a window isn’t open and then proceeds to modify the selected text. All of the following can also be used within a tell statement to Tex-Edit Plus:-
tell window 1
set numWords to count words
delete first word
delete (every character whose
end tell
get font of third word of
get position of first window
tell window 1
set color of last word to red
set name to "New File Name"
set font of paragraphs to
set size of paragraphs to 24
set size of character 1 of
end tell
make new window behind
make new line at end of text of
select last document
select text from character 1
copy -- copies selection
copy unstyled
The undo, cut, paste and revert commands can be used in a similar way to the last example.
The following script demonstrates other ways in which text can be manipulated:-
tell window 3 of application
set size of (text from
set size of line 4 to 16
set style of (text from word
set style of character 1 to
set color of last character
set color of paragraph 2
get font of character 1
end tell
This also illustrates how an object such as style can use a list of properties.
The suites used in different applications are often entirely different, requiring you to rewrite a script for a new application or new version of a program. In some instances, however, applications support similar but slightly different instructions. For example, the following script tells Netscape to open a specified location on the Internet:-
set theURL to text returned of
tell application "Netscape 6"
and the following identical script does the same thing with Internet Explorer:-
set theURL to text returned of
tell application "Internet
However, to open the URL in a new window in Netscape requires:-
set theURL to text returned of
tell application "Netscape 6"
OpenURL theURL to window
end tell
whilst Explorer needs a different and rather obscure approach:-
set theURL to text returned of
tell application "Internet
This is rather typical of AppleScript and is just something that scripters must learn to endure.
Transferring data between applications is tricky. The original AppleScript Language Guide allowed copy to be used in AppleScript or as an application command, the latter copying the current selection to the clipboard. However, in AppleScript 1.3.7 and above, the use of copy in applications is discouraged, presumably to avoid confusion.Hence different applications often employ different instructions for copying. For example, AppleWorks uses copy to clipboard to put the current selection on the clipboard whilst BBEdit uses copy followed by a reference to the material to be copied.
Moving data between the Clipboard and a script is also difficult, although this is accommodated by the Standard Additions scripting addition. Prior to transferring the data you must first bring the appropriate application to the front, usually with an activate command. Having done this, you can use one of the following to copy or paste the information:-
set the clipboard to theData
set theData to the clipboard
The word the, which is usually optional in AppleScript, normally appears as a keyword. However, in the context of the clipboard command it must be used and isn’t shown as a keyword.
Tex-Edit documentation, Tom Bender
©Ray White 2002.