Application Scripting

Scriptable applications provide a varying degree of support for AppleScript. Some simply allow scripts to emulate menu items whilst others provide more advanced controls.

Core Suite

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.

activate application "Tex-Edit Plus" -- brings application to front

quit application "Tex-Edit Plus" -- quits application

quit application "Tex-Edit Plus" saving no -- quits application without saving

tell application "Tex-Edit Plus"

  open file "Macintosh HD:Documents:The Letter" -- opens file

end tell

print every window of application "Tex-Edit Plus" -- prints all open documents

print last window of application "Tex-Edit Plus" -- prints last window

close every window of application "Tex-Edit Plus" -- closes all windows

close windows 2 through 4 of application "Tex-Edit Plus" -- closes selected windows

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

Other Suites

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 "Scriptable Text Editor"

  set numWndws to count windows -- counts number of windows

  if not (front window exists) then display dialog "No window is open."

  set theText to selection -- allows selected text to be extracted

  set beginning of selection to "dog" -- adds "dog" to start of selected text

  set end of selection to "mouse" -- adds "mouse" to end of selected text

  set wndwName to name of window 1 -- gets name of window

  set name of window 1 to "Window Name" -- changes name of window

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 of window 1 -- counts number of words

  delete first word -- removes word

  delete (every character whose color is red) -- removes red text

end tell

get font of third word of window 1 -- returns ‘Times’ or similar

get position of first window -- returns {125, 250} or similar

tell window 1

  set color of last word to red -- changes text colour

  set name to "New File Name" -- changes name of window

  set font of paragraphs to "Monaco" -- changes font of all text

  set size of paragraphs to 24 -- changes size of all text

  set size of character 1 of paragraphs to 36 -- changes first characters only

end tell

make new window behind window 1 -- creates window in background

make new line at end of text of window 1 with data "Dear Harry" -- adds line

save first window in "Macintosh HD:Documents:New Letter" -- saves document

select last document -- selects window

select text from character 1 to character 60000 of window 2 -- selects text

copy -- copies selection

copy unstyled -- copies selection without style information

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 "Tex-Edit Plus"

  set size of (text from character 1 to word 10) to 24 -- 24 point font

  set size of line 4 to 16 -- selects 16 point font

  set style of (text from word 2 to word 4) to bold -- bold text

  set style of character 1 to {bold, italic, underline} -- list of properties

  set color of last character to red -- sets text colour

  set color of paragraph 2 to blue -- sets text colour of paragraph

  get font of character 1

end tell

This also illustrates how an object such as style can use a list of properties.

Scripting Variations

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 (display dialog "Enter a URL to open:" default answer "")

tell application "Netscape 6" to OpenURL theURL

and the following identical script does the same thing with Internet Explorer:-

set theURL to text returned of (display dialog "Enter a URL to open:" default answer "")

tell application "Internet Explorer" to OpenURL theURL

However, to open the URL in a new window in Netscape requires:-

set theURL to text returned of (display dialog "Enter a URL to open:" default answer "")

tell application "Netscape 6"

  OpenURL theURL to window (make new window)

end tell

whilst Explorer needs a different and rather obscure approach:-

set theURL to text returned of (display dialog "Enter a URL to open:" default answer "")

tell application "Internet Explorer" to OpenURL theURL to window 1

This is rather typical of AppleScript and is just something that scripters must learn to endure.

Copying Data between Applications

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 -- puts data in ‘theData’ onto the Clipboard

set theData to the clipboard -- transfers data from Clipboard to ‘theData’

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.

Reference

Tex-Edit documentation, Tom Bender

©Ray White 2002.