Download AppleScript: The Definitive Guide (Definitive Guides) PDF

TitleAppleScript: The Definitive Guide (Definitive Guides)
File Size23.7 MB
Total Pages482
Document Text Contents
Page 1



' , ..

, H;; , \


Recommended Title

The Definitive Guide

O'REILLY® Matt Seu bu,.

Page 2


. '




AppleScript: The Definitive Guide
If you want to know all about AppleScript-the how, where, and why of using it--dig

into AfJfJleScrifJt: The Definitive Guide. It doesn't make the mistake that other books do: it
isn't about scripting this or that paI1icular application, and it doesn't assume that learning

AppleScript is easy or obvious. Instead, the book teaches and documents the language

in a clear and rigorous manner, just as you'd expect with any programming or scripting language.

The book covers AppleScript 1.9.2 and the new Script Editor, released as part of the new version of

Mac OS X Panther. Experienced AppleScripters will love having the most definitive, up-to-date

AppleScript language reference available.

Kot since Danny Goodman's classic AppleScript book has a more complete tome on the language

been published. llere's what early reviewers of the book had to say:

"Having worked with AppleScript for more than 12 years, I honestly thought I knew it inside

out. Matt's book proved me wronglt does a bang-on job of telling you how to get the most

out of AppleScript."

-Mark Alldritt, President, Late Night Software Ltd., Script Debugger Developer

"Matt Neuburg knows AppleScript's ins and outs thoroughly, and is not afraid to point oul

prublems and how tu work around them, as well as its strengths. This book will be of great

interest both to programmers coming to AppleScript from other languages and to scripters

who are already adept atAppleScript but puzzled by its

,ma1 y oddities." Developer

Connection-Paul Berkowitz

Recommended Title
AppleScript.- The Definitive Guide teaches not only how the

Apple Computer, Inc. boldly combined open
AppleScript language works, but also how to use it in all sorts source technologies with its own program­
of contexts-in everyday scripts to process automation, as ming efforts to create Mac as x, one of the

most versatile and stable operating systems \\'ell as in AppleScript Studio, in Cocoa, in CGI scripts, and in
now available. In the same spirit, Apple has

combination with Perl and Ruby. Once you know the ins and
joined forces with O'Reilly & Associates,

outs of AppleScript, there's a lot you can do with it, and this Inc. to bring you an indispensable collec­
book unlocks the secrets of AppleScript. Regardless of their tion of technical publications. The ADC logo

indicates that the book has been technically
level of experience, AppleScripters everywhere will turn to

reviewed by Apple engineers and is recom­
this book again and again. mended by the Apple Developer Connection.


Visit O'Reilly on the Web at www.oreil/ US $39.95
I S B N 0 -5 9 6 - 0 05 57-1 CAN $61.95




Page 241

if condition then
- - wh at to do if con dition is true

end if

It is also permitted to supply a second block, to be executed if the condition is false .
One or the other of the two blocks will be executed.

if condition then
- - wh at to do if con dition is true

- - wh at to do if con dition is false

end if

Another syntax lets you specify multiple conditions. Apple5cript will execute the first
block whose condition is true, skipping the others . It is permitted to supply a final
block that will be executed if none of the conditions is true.

if conditionl then
- - wh at to do if con dition l is true

else if condition2 then
- - wh at to do if con dition 2 is true

- - . . . s ame for condition3, condition4, etc.
[ else ]

- - wh at to do if none of them is true
end if

So, for example :

set x to random number from 1 to 10
set guess to text returned of �

(display dialog " Pick a number from 1 to 10" default answer " " )

set guess to guess a s number
on error

end try
if guess < 1 or guess > 10 then

display dialog "I said from 1 to 10 ! "
else if guess < x then

display dialog "Too small . I was thinking of " & x
else if guess > x then

display dialog "Too big . I was thinking of " & x

display dialog " J ust right . "
end if

There's also a single-line form :

if condition then whatToDo

In the single-line form, whatToDo is any valid expression or single-line command (it
can even be another single-line if) .

Branching I 219

Page 242

The other major form of choice is looping, which involves branching back to the start
of a block repeatedly. In AppleScript, looping is performed with repeat . There are
several varieties of repeat, but repeat blocks all take same basic form :

repeat whatKindOfRepeat
- - what to do
end repeat

The big question with a repeat block is how you're going to get out of it. Obviously
you don't want to repeat the repeat block forever, since this will be an infinite loop
and will cause the computer to hang. Usually you deal with this through the nature
of the whatKindOjRepeat, which typically provides a condition to be evaluated, as a
way of deciding whether to loop again, or some other form of instruction governing
how many times the block will be repeated.

There are also some special commands for hustling things along by leaping com­
pletely out of the repeat block. They can be used with any form of repeat block. Here
they are :


This command leaves the repeat block by virtue of the fact that i t terminates exe­
cution of the handler or script.

exit repeat

This command exits the innermost repeat block in which it occurs . Execution
resumes after the end repeat line .

Repeat Forever

A repeat block with no whatKindOjRepeat clause repeats unconditionally, whence for­
ever. Obviously you don't really want it to repeat forever, so it's up to you to supply
a way out . I just told you two ways out. A third is to loop inside a try block and to
throw an error; this method is illustrated later in this chapter, in "Catch. "

display dialog " Prepare to loop forever . "
exit repeat

end repeat
display dialog " J ust kidding . "

Repeat While
A repeat block where whatKindOjRepeat is the keyword while followed by a boolean
expression tests the expression before each repetition. If the expression is true, the
block is executed. If the expression is false, the block is not executed and that's the end
of the loop; execution resumes after the end repeat line. The idea is that in the course
of looping something will eventually happen that will make the expression false.

220 I Chapter 12 : Control

Page 481

Keep in touch with O'Rei l ly
1 . Download examples from our books

To find example files for a book, go to :

seiect the book, and follow the "Examples" link.

2. Register your O'Reil ly books
Register your book at

Why register your books?
Once you've registered your O'Reilly books you can :

Win O'Reilly books, T-shirts or discount
coupons in our monthly drawing.

Get special offers available only to registered
O'Reilly customers.

Get catalogs announcing new books
(US and UK only).

Get email notification of new editions of the
O'Reilly books you own.

3. Join our email l ists
Sign up to get topic-specific email announcements
of new books and conferences, special offers, and
O'Reilly Network technology newsleners at:

It's easy to customize your free elists subscription so
you'll get exactly the O'Reilly news you want.

4. Get the latest news, tips, and tools

"Top 100 Sites on the Web"-PC Magazine

CIO Magazine's Web Business 50 Awards

Our web site contains a library of comprehensive
product information (including book excerpts and
tables of contents), downloadable software, back­
ground articles, interviews with technology leaders,
links to relevant sites, book cover art, and more.

S. Work for O'Reilly
Check out our web site for current employment
opportunities :

jobs. oreilly. com

6. Contact us
O'Reilly &: Associates, Inc.
1005 Gravenstein Hwy North
Sebastopol, CA 95472 USA

TEL: 707-827-7000 or 800-998-9938
(6am to 5pm PST)

FAX: 707-829-0104

[email protected]
For answers to problems regarding your order or our
products. To place a book order online, visit:


[email protected]
To request a copy of our latest catalog.

[email protected]
For book content technical questions or corrections.

[email protected]
for educational, library, government, and
corporate sales.

[email protected]
To submit new book proposals to our editors and
product managers.

[email protected]
For information about our international distributors
or translation queries. For a list of our distributors
outside of North America check out:

international. oreilly. coml distributors. html

[email protected]
For information about academic use of O'Reilly
books, visit :

academic. oreilly. com

Our books are available at most retail and online bookstores.

To order direct : 1-800-998-9938 • [email protected] • www.oreillycom
Online editions of most O'Reilly titles are available by subscription at safari. oreil ly. com

Similer Documents