Chapter 12. Scripting Programs That Don't Have Dictionaries
In every script
since Chapter 1, you've used
AppleScript commands that come from programs'
dictionaries. Dictionaries are the cornerstone of application control
(Section 3.2.2), and they're great
for discovering new commands. The only problem is, not every program
has as a dictionary.
Some programs, like Chess, are too unimportant for Apple to create a
dictionary. Others, like System Preferences, have such limited
dictionaries that they're almost not worth
mentioning. Still other programs are written by overworked
third-party programmers, who might not fully comprehend the benefits
of making their application AppleScriptable. And programs like Final
Cut Pro and GarageBand really ought to have
AppleScript dictionaries, but Apple hasn't gotten
around to writing them. If you want to script such programs,
you're left out in the coldor so you might
think.
The fact is, every programeven one
without dictionariessupports three
universal
commands. They're not particularly powerful, but
they form the backbone of most scripts.
Table 12-1. Commands you can send to any program
Command
|
What it does
|
---|
activate
|
Brings the target program forward, launching it if necessary.
|
launch
|
Opens the target program but doesn't bring it
forward.
|
quit
|
Closes the target program, asking you to save any unsaved documents
in the process.
|
If you only used those three commands, however,
you'd be left with some pretty dinky scripts. Of
course, if the program you're controlling has a
dictionary, the commands from that dictionary are likely to make up
the vast majority of your script.
Still, even if a program
doesn't have a dictionary, you
can control the program with AppleScript; it's just
a little harder than usual. The trick is to use Mac OS
X's GUI Scripting feature, through which you can
control a program's interface
(GUI, in fact, stands for Graphical
User Interface). In essence, you use GUI Scripting to
automate the clicking of buttons or the typing of keys, rather than
directing programs with special commands like
make or
count.
 |
Even though you can control any program with GUI Scripting, you
should use it only if the program you want to control
doesn't have an AppleScript dictionary.
That's because GUI Scripting is
always slower and less reliable than scripting a
program with direct AppleScript commands (as you've
done in the last 11 chapters).
|
|
Gem in the Rough Checking Whether a Program Has a Dictionary | When deciding how to script a program, it's
important to know whether that program has a
dictionary. If the program
does, you can use any of the commands from that
dictionary, automating your Mac with ease. On the other hand, if a
program lacks a dictionary, you have to use GUI Scripting if you want
to control the program with AppleScript. But you already knew all that. To figure out whether a program has a
dictionary, you can make an educated guessor you can use the
Finder's has scripting
terminology property. You give that command a file alias,
and you get back a Boolean value: true if the
program has a dictionary or false if it
doesn't. For example, you can use the following
script to check
whether TextEdit has a dictionary: tell application "Finder"
if has scripting terminology of alias ¬
"Macintosh HD:Applications:¬
TextEdit.app:" then
display dialog "Yes, TextEdit has ¬
a dictionary."
else
display dialog ¬
"TextEdit has no dictionary."
end if
end tell
To prove that the Chess program
doesn't have a dictionary,
simply replace the word "TextEdit"
with "Chess" everywhere in the
previous script. Or, if you'd like to check another
program, substitute that program's name in the
script instead. At a certain point, however, you'll get tired of
editing your entire script just to check a different program. By
making the following changes, however, you you'll be
able to pick the program you want to check right from a dialog box: set selectedProgram to (choose file)
. tell application "Finder"
if has scripting terminology of selectedProgram thendisplay dialog ¬
"Yes, that program has a dictionary."
else
display dialog ¬ "That program does not have a dictionary."
end if
end tell
Incidentally, the benefit of this script over the
File Open Dictionary
command is that it lets you pinpoint a
particular program to check for a dictionary. If
you used the Open Dictionary dialog box, you'd have
to sort through dozens of programs just to find the one you want. |
 |
The example scripts from this chapter can be found on the AppleScript
Examples CD (see Sidebar 2.1 for instructions).
|
|
 |