3.2. Commanding Other Programs
Unless you plan to display dialog boxes in Script Editor all day,
you're probably feeling pretty constrained by
AppleScript right now. Yeah, you can display cool
stuff in your dialog boxes, but that
won't be enough to automate your Mac.
Luckily, you can use AppleScript to do other thingsand one of
the most powerful is the ability to control other applications. This
feature alone unlocks a world of possibilities:
Automate your favorite word
processor, so you can save all unsaved documents with a
single click (Section 5.7.3). Apply color-correction settings
automatically using Adobe Photoshop (Section 7.2.2). Convert song files in iTunes
automatically while you recline in your lounge chair
(Section 8.1.4).
Before you can write any of those complicated scripts, however, you
must learn the basics of program control. This section, therefore,
introduces the basics of how to send commands to specific programs,
rather than just to Mac OS X itself.
3.2.1. Using tell Statements
When you want to target a program with your commands, you have to let
AppleScript know which program to talk to. For such purposes, the
tell statement is your best friend.
Say you want the Finder to open your Home folder, so you can copy
some files onto your desktop. To do this, enter these commands in
Script Editor, then click Run:
tell application "Finder"
activate
open home
end tell
Everything after the tell command is directed at
the program you specifyin this case, the Finder. The
activate command brings the program forward,
while the open home command tells the Finder to
open a new Finder window and for your Home folder. Finally, once
AppleScript reaches the end tell command, it
lets the Finder off the hook and stops sending it commands.
|
If you want to send a single command to a
program, you can put the entire tell statement
on one line, like this:
|
|
tell application "Finder" to activate
|
That way, you don't have to spell out the entire
thing on multiple lines, like this:
tell application "Finder"
activate
end tell
|
|
3.2.2. Opening a Dictionary
If you hope to do real work with the Finder, being able to open
windows or display dialog boxes is really just small potatoes. For
more complex tasks, you have to read the Finder's
dictionarya list of all the commands it can
accept. Every program that AppleScript can command has a dictionary,
too, so it's helpful to take some time to explore.
To access a program's dictionary, follow these steps:
Open Script Editor, and choose
File
Open Dictionary
(Shift--O). A dialog box appears, with
a list of every program on your Mac that AppleScript can control. Scroll down to the program whose dictionary you
want to open. Alternatively, type the first few
letters of the program's name. For example, just
type "Fin" to jump down to the
Finder. |
If you want to open the dictionary of a program, but you
don't see its name in the Open Dictionary dialog
box, click Browse. You'll get a standard Open dialog
box, which you can use to navigate to the appropriate program on your
hard drive.
|
|
Power Users' Clinic Apple Events | When you tell the Finder to activate, Mac OS X
sends a tiny virtual packetknown as an Apple
Eventfrom Script Editor to the Finder. Such
packets are the foundation of AppleScript; every single command you
write gets translated into an Apple Event before
it's sent off to the appropriate program. Not only does Mac OS X send Apple Events between programs, but Apple
Events are often sent within the same program.
For example, Script Editor's Recording feature
(Section 2.1.1.1) works by picking up Apple Events that
occur as you interact with individual programs on your Mac. If you'd like to see the actual guts of an Apple
Eventnot a pretty sighta program like
AE Monitor (http://software.oxalyn.com/aemonitor/) can
help. With AE Monitor installed, you can track all the Apple Events
flying around your systemto see where they're
coming from and where they're heading to, for
example. If you have a spare afternoon, set up AE Monitor and just
use your Mac
as usual; you'll be amazed at the number of Apple
Events your Mac is sending. Of course, this is all possible because Apple Events are a standard
for communicating between Mac programs. That means they work in Mac
OS 9 and Mac OS Xand
that's why you can send AppleScript commands back
and forth between programs running in the Classic environment (a Mac
OS 9 emulator) and in Mac OS X. Most of all, this
standards-compliance means that AppleScript isn't
the only language you can use for controlling
programs. Other languages, such as Late Night
Software's version of JavaScript (http://latenightsw.com/freeware/JavaScriptOSA/index.html)
can control other programs using Apple Events, too. Now, that's not to say you should start using other
languages to control Mac programsAppleScript is still the
easiest and most supported Mac scripting language around. However, if
you happen to have JavaScript code that you've
written for a Web site, the ability to also automate
programs with JavaScript is a big plus. |
Click Choose. The
program's dictionary opens, as shown in Figure 3-2.
|
If you're a speed freak, there are two other
ways to open a program's dictionary. The first is to
drag the program's icon onto Script
Editor's Dock icon. No dialog boxes, no
navigatingjust a quick way to bring up the
program's dictionary. The second trick
is even sneakier than the first. If the program whose dictionary you
want to open is already in the Dock, just
-drag that program's icon onto Script
Editor's icon. (For instance, if you wanted to open
the Finder's dictionary, you could just
-drag its Dock icon onto Script
Editor's). Time saved: 2.7 seconds.
|
|
| Figure 3-2. The dictionary window has two parts. The flippy triangles on the left (also known as disclosure triangles because they reveal additional information) let you browse through commands, kind of like an index. The pane on the right displays the entries themselves: definitions for the commands and keywords that the program allows. |
|
3.2.3. Looking for the Commands You Want
Now, suppose you want to make a script that checks whether you have a
Home
Current
Work folder and then displays a dialog box that tells you its
findings. To make such a script, you must first figure out the
appropriate AppleScript commands.
For this job, the Finder's dictionary is your best
reference. That's because the dictionary
determines which commands you can use, so
you're likely to find the best command for your job
there.
When you're looking through a dictionary,
it's always a good idea to check the
dictionary's
Standard Suite first. In the world of
AppleScript, the Standard Suite is a set of commands common to most
scriptable programs. The commands listed there are AppleScript
mainstays (open, close, and
delete, for example)the sorts of commands
that give AppleScript its reputation for being like English. To open
the Standard Suite, just click the flippy triangle next to its name.
Then, to browse the list of possible commands, click the triangle
next to commands.
|
If the mouse is too slow for you, you can navigate a dictionary
using just the keyboard. Use the arrow keys to scroll over a
particular suite, and reveal the suite's contents
with the right arrow key. If you want to pop open the
suite's commands at the same
time, press Option-right arrow while the suite's
heading is selected. When you're done
sifting through the commands with the up and down arrow keys, select
the heading of the suite again and press the left arrow key. In an
instant, the contents of the suite disappear.
|
|
3.2.3.1 Checking whether the folder is already there
As your eyes wander down the left column in the
Finder's dictionary, you'll come
across the exists command. Click it once, and
you'll see a brief description of how it works:
"Result: boolean true if it exists, false if
not." This is precisely the command
you'd use to check if a folder is already there.
|
A Boolean value is simply one that can be only
true or false.
|
|
Gem in the Rough Script Editor's Library | As you write more scripts, you'll spend a lot of
time reading dictionaries. Unfortunately, the method Apple suggests
for examining them (choosing
File
Open
Dictionary, then scrolling pages and pages to find the right program)
can be time-consuming, especially if you have a lot of programs. Luckily, Script Editor includes a shortcut for
examining dictionaries: the Library window, which you can open by
choosing Window
Library (Shift--L). The Library window contains a list of common scriptable Mac OS X
programs. You can open a program's dictionary by
selecting its name and clicking the bookshelf icon, or just by
double-clicking the program's name. If you often find yourself reading a particular
program's dictionary, you can add the program to the
Library window by clicking the + button in the toolbar, and then
navigating to the program in the Open dialog box. Conversely, you can
remove a program from the Library window by selecting its name and
pressing the - button in the toolbar. Finally, the script icon in the toolbar (the one farthest to the
right) is a convenient way to create a new script for an application
in the Library list. For example, if you select iCal and then click
the Script icon, a new script window pops open
with the following code, ready for you to
enter additional
commands: tell application "iCal"
end tell
|
Enter the following
script to test out the
exists command:
tell application "Finder"
if the folder "Current Work" of home exists then
display dialog "The Current Work folder is already there."
end if
end tell
This might not look quite like English, but it's
pretty close. Here's how the commands work:
tell application
"Finder". This is the equivalent of
saying "heads up" to the Finder. It
sends all the commands that follow to the Finder instead of to Mac OS
X itself. if the folder "Current
Work" of home exists then. This command
searches in your Home folder for a folder named Current Work. If this
folder exists, the script runs every command inside the
if statement (everything from
if to end if ).
|
In AppleScript, you need to put the names of specific items (folders,
programs, and so on) in quotation marks after the items they
refer tolike folder "Current
Work", application
"Finder", and so on.
However, some special items are represented by AppleScript keywords,
like home and current
application. These terms are placeholders for a specific
folder or application, so they don't use quotation
marks. (To represent their special status, keywords like these also
get colored blue in the Script Field.)
|
|
display dialog "The Current
Work folder is already there." This
command is placed inside the if statement, which
means it's run only if the Current Work folder
already exists. end if. This marks the end of the
if statement. Because of that, any commands that
follow are run regardless of whether the Current Work folder already
exists. end tell. This marks the end of the
tell statement. Any commands that follow are
sent to Mac OS X itself, rather than to the Finder.
Now run the script. If the script finds a Current Work folder in your
Home folder, it displays a dialog box; otherwise, the script ends
silently and you see nothing at all.
3.2.3.2 Displaying the other dialog box
At this point, your script works fine. However, it only does half the
job; if there isn't a Current
Work folder, the script just ends without letting you know. To fix
this problem, you need to insert a few
commands in your script (new lines
are shown in bold):
tell application "Finder"
if the folder "Current Work" of home exists then
display dialog "The Current Work folder already exists."
else
display dialog "The Current Work folder can't to be found."
end if
end tell
If you have a Current Work folder, the first dialog box
("The Current Work folder already
exists.") is displayed. However, if you
don't have a Current Work
folder, the first dialog box is not displayed, and the
else statementwhich shows the
second dialog boxtakes over instead.
|
That's how all if-else
statements work: if the if portion is true, the
commands that follow if are run. On the other
hand, if the if portion is false, the script
runs the commands that follow else instead.
It's the same sort of logic you'd
use when deciding what to wear outside: "If
it's raining, I will wear a raincoat. Else, I will
wear a T-shirt."
|
|
Up to Speed Understanding Dictionaries | Dictionaries are some of the most useful references in AppleScript,
since they list the meanings and usage of everyday AppleScript
keywords. Unfortunately, dictionaries can be hard to decipher, and
their strange jargon confounds many beginners. If you find yourself
tossed in the sea of programmer-speak, this sidebar can help you
regain your land legs. First of all, a
dictionary
is broken down into multiple sections, called
suites. The most commonthe Standard
Suitelists AppleScript commands that you'll
use over and over again in multiple programs. Don't
worry too much about suites, though: they're just
there for organizing large numbers of commands, and they
don't have any bearing on how commands work. Within many suites, you'll find two
sectionsclasses and commandswhen you click the flippy
triangle next to a suite's name. A
class is the AppleScript equivalent of a
nounlike disk and
folder. A command, on the
other hand, is like an AppleScript
verbdelete or
restart, for example. As with English, you make complete AppleScript statements by
connecting verbs and nouns. In the script on Section 3.2.3.2, for example, you check
whether a folder exists by using the AppleScript class (or noun)
folder, and using the command
(or verb) exists. Once you realize that
you're just building sentences, the whole concept of
"programming" should lose some of
its stigma. Finally, certain commands have results. You can
think of a result like the answer to a question; if you ask
AppleScript whether a file exists, for instance, the result will be
either true or false.
Obviously, different commands will have different resultssome
will be files, some will be numbers, and some will be lists, for
example. The great part about dictionaries, though, is that
they're nice enough to tell you what
kind of result you can expect from a particular
command: just look next to the word
"Result" at the bottom of a
command's entry. Don't get too cocky, though. Many commands in
dictionaries have optionsthe AppleScript
equivalent of prepositional phrases. Options add extra features to a
particular command, letting you make the command more targeted (at a
particular disk or folder, for instance). You can tell
something's an option because it's
surrounded in brackets in its dictionary entrylike
to location for the
duplicate command (Section 5.4.1). Keep in mind, though, that an option is just
that: optional. You never have to use an option with a particular
command if you don't want to. |
3.2.4. Displaying a Window in the Finder
So far, your script lets you know whether it finds a Current Work
folder or not. That's useful, but it would be even
better if the script could open a Finder window showing you the
contents of the Current Work folder if it exists.
To do that, you have to go back and sift through the
Finder's dictionaryand again, the Standard
Suite is the best place to start, since the commands there are both
simple and powerful. As you browse the command list,
you'll come across the open
commandand its definition, "open: Open the
specified object(s)," reveals that it will do just
what you want (Figure 3-3).
| Figure 3-3. The "specified object(s)" that the dictionary refers to can be anything you want to open. In this case, you want to open a folder, so the "specified object" will be the name of that folder. |
|
Now you can build an updated
script:
tell application "Finder"
if the folder "Current Work" of home exists then
activate
open folder "Current Work" of home
else
display dialog "The Current Work folder 'can't be found."
end if
end tell
Here's how the two new lines work:
|
To open a program without bringing it forward, use the
launch command instead of
activate.
|
|
Now that you know how to display dialog boxes, read
programs' dictionaries, and use
if statements to run code selectively,
you're well on your way to creating truly timesaving
scripts. On Section 5.3.1.1, for example,
you'll learn how to create
folders, in addition to opening them.
Before going on, though, you might want to exercise your new
AppleScript knowledge to keep it flexible. For example, you could
write a script that opens your Home folder, or write a script to
display a series of jokes. As with most things in AppleScript, the
only limit is your tolerance for geekiness.
Power Users' Clinic Scripting Additions | So far, you've looked up two commands in a
dictionary: exists and
open. You might be wondering, though, which
dictionary has the entries for commands like display
dialog. As it turns out, general AppleScript commands (like display
dialog) are stored in files called scripting
additions. You can examine scripting
additions' dictionaries the same way
you'd examine the Finder's
dictionary: by choosing
File Open Dictionary. The display dialog command is stored in the Standard
Additions dictionary included with your Maca scripting
addition that contains commands for user interaction, network jobs,
and other miscellaneous tasks. If you use Script
Editor's Library window (Sidebar 3.3), you can just double-click the item called
StandardAdditions.osax to open the Standard Additions dictionary. Not all scripting additions come with your Mac, though. If you ever
find AppleScript lacking in a certain featuresay, the ability
to use logarithmsthere's a good chance
someone else has created a scripting addition to
plug the hole. Check out www.osaxen.com for a comprehensive list of
third-party scripting additionsincluding ones that do everything from spell checking to complex math operations. Once
you've downloaded a scripting addition, just drag it
into the Library
ScriptingAdditions folder (creating the
folder if necessary) to make the new commands available to all your
scripts. (And a side note: among AppleScript geeks, another term for
"scripting addition" is
osax, which stands for
Open Scripting
Architecture Extension. For the ultimate in obscurity, memorize the
plural form: osaxen.) Unfortunately, if you use commands from third-party scripting
additions, your scripts might not work properly on other Macs. The
workaround: use script
bundlesself-contained files that let you
distribute scripting additions along with your scripts (Sidebar 2.5). Of course, if you
purchased a third-party scripting addition, make
sure it's legal to distribute before you go bundling
it with all your scripts. If after a while you find it hard to keep track of all the scripting
additions on your Mac, install the HetimaOsaxOpener plug-in (Section 2.3.5) to keep them organized in Script
Editor's File menu. That's a much
easier way of opening scripting additions than, for example, the Open
Dictionary dialog box. |
|