3.1. Getting Started
Just as you can't surf the Web without a Web
browser, you can't create AppleScripts without a
script-editing program. The aptly named Script Editor (in your
Applications AppleScript
folder) is the simplest program for the job, but feel free to try any
of the other editors listed on Sidebar 2.6.
Before you start diving into the following sections, the first thing
you'll need to do is launch
Script Editor (Chapter 2).
With this blank scripting slate before you, it's
possible to accomplish such neat tasks as:
Displaying a dialog box using a single line of code (see below) Performing basic arithmetic (see the next page) Getting the contents of your Clipboard (the place where Mac OS X
stores information when you choose
Edit Cut or
Edit
Copy [see the
next page])
But that's only the beginning. So strap on that
beanie with the propeller on top, and get ready to write your first
lines of AppleScript.
3.1.1. Displaying a Simple Dialog Box
As you saw on Section 1.1.4, dialog boxes are the most
common way for AppleScript to interact with you.
That's because, on their own, scripts
can't display complex interface elements like pop-up
menus, checkboxes, or toolbars. As you're starting
out in AppleScript, therefore, you'll use the
display dialog command even more than you
breathe.
Here's how:
With Script Editor open, type the following
command in the Script Field: display dialog "Hello!
I'm your first AppleScript."
As
you type in the command, you'll notice it appears as
a bunch of purple text. That's Script
Editor's way of telling you that it
hasn't checked your command for AppleScript
compliance yet. Click the Compile button. That
tells Script Editor, "Make
sure I didn't make any typos in my AppleScript
commands" (Section 2.1.1.4). Click Run. Under the hood,
AppleScript sees your display dialog command and
realizes that it has to put together a dialog box with your provided
text. After a fraction of a second, you see the fruits of
AppleScript's labor (Figure 3-1).
|
If you don't see that dialog
box, see Table 14-1 for an explanation of what may
have gone wrong.
|
|
| Figure 3-1. Your first script running. It's not much, but it's a start. |
|
No matter which button you click (Cancel or OK), AppleScript
dismisses your dialog box. If you start to suffer from dialog box
withdrawal, though, simply click Run again to redisplay the dialog
box.
3.1.2. Displaying Numbers
In general, the display dialog command just
presents whatever follows it. In the previous example, you typed a
string (a series of characters surrounded by
double-quotes), so display dialog showed that.
On the other hand, you can just as easily use a
number with the display
dialog command, since AppleScript
automatically converts (or coerces) the number
to its textual representation. For example, if you opened a new
script window (File New)
and ran this command:
display dialog 4.52
you'd see a dialog box with 4.52 in it.
The fun doesn't stop there, though. If
you'd like, you can put math expressions after the
display dialog command, like this:
display dialog (1 + 3 - 9)
When you run that command, AppleScript figures out the value of the
expression (in this case, 1+3-9), and displays the result in a dialog
box: -5.
|
AppleScript supports the full range of operations
you'd perform on a pocket calculator: addition
(symbolized by a + sign), subtraction (indicated by a - sign),
multiplication (shown with an *), and division (shown with a / ).
Just like your middle school math teacher taught you, AppleScript
follows the proper order of mathematical operations: parentheses
first, then multiplication and division, and finally addition and
subtraction. That means that 4+2*3 would come
out to 10, whereas (4+2)*3 would come out to 18.
To avoid confusion, experienced scripters use parentheses even when
they're not strictly necessary. That single change
can save you hours of time trying to track down math errors.
|
|
3.1.3. More than One Command
So far, you've been writing scripts with a single
command in themknown by geeks as
"one-liners." If you were
restricted to one command per script, though, you'd
have quite a time trying to write that workflow-integration script
you've been dreaming about.
Luckily, AppleScript lets you put as many commands as you want in
your script. When you click Run, AppleScript runs the first command,
waits for it to finish, runs the second command, waits for it to
finish, and so on. Here's a simple
example:
display dialog "This script shows the text on your clipboard. Click OK to proceed."
display dialog (the clipboard) -- This command runs after you click OK
display dialog "Thank you for running Clipboard Viewer."
This script shows three dialog boxes, one at a time.
The first dialog box explains what
the script does. If you're creating a new script,
it's often a good idea to have an explanatory dialog
box at the beginning. The second dialog box displays the
contents of the Clipboard. Any text you've copied
(using, for example, the
Edit Copy command) is
shown onscreen. |
In AppleScript, all you need to do to get the contents of the
Clipboard is write the clipboard.
|
|
The text after the two hyphens is a
commentsomething that AppleScript
ignores. See the Sidebar 3.1
sidebar (Sidebar 3.1) for more about this feature. Finally, the third dialog box
displays a goodbye note. This, of course, is completely
optional in your own scripts.
|
If, at any point, you click the Cancel button in a dialog box, the
script stops running. In fact, that's just about the
only way to make a script stop running while a
dialog box is open; if you attempt to click the Stop toolbar button,
Script Editor just beeps.
|
|
Up to Speed Making Comments | When you're reading through a script, many of the
commands are probably pretty self-evident. Unfortunately, there can
also be some that seem totally foreign. In those cases, you can use a
commenta section of text in plain
Englishto remind yourself how the commands work. AppleScript supports two kinds of comments: single-line and
multi-line. No matter which kind of
comments you use, AppleScript ignores what's
insidethey're there solely for your benefit. You'd write a single-line comment like this: display dialog "Watermelon" --a dialog box
AppleScript understands that everything from the double-hyphen to the
end of the line is a comment. To AppleScript, therefore, the previous
command is identical to this one: display dialog "Watermelon"
You'll often see a single-line comment used to
clarify how a particular command works. On the other hand, a multi-line comment is useful for explaining how
a big chunk of your script works. It could look
something like this: (* Please note that this script may irrevocably erase your ENTIRE hard drive. Use
with caution, or on someone else's computer. *)
Everything between the parentheses and asterisks is understood to be
a commentno matter how many lines it spans.
You'll often see multi-line comments in the scripts
that come with your Mac, since they contain Apple's
copyright information. |
|