AppleScript in a Nutshell
Bruce W. Perry
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/9781565928411/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
AppleScript continues to evolve on Mac OS 9 and Mac OS X as theultimate scripting tool for the Macintosh.AppleScripts power to automate the operating systemand complex applications such as graphics, desktop-publishing, anddatabase programs, as well as a friendly English language dialectthat helps ambitious scripters get up to speed quickly with their ownapplets, is not matched by any other platformsprogramming language. Yet, only a small percentage of Macintosh usersare even aware that AppleScript is installed with their operatingsystem. Those who are aware of AppleScriptspresence on their machine often do not take full advantage of thistool to automate their daily computing activities, both on theirlocal machine and over the Internet.
Who should and can use AppleScript? The following users come to mindright away: system administrators who are automating tasks withnetworks and applications; web and graphics professionals who want tocontrol the development of web sites and publications; scientists,mathematicians, and engineers who require applets to makecalculations and automate their own software tools, as well asday-to-day programmers and students who are designing and prototypingnew programs. Not to mention everday users who want to automate theirown computing tasks, such as file and folder backups.
If you are on a Macintosh, then you should be putting AppleScript towork for you.
The purpose of this book is primarily three-fold:
Describe AppleScript and its tools ()that all users can keep next to their computers as they write newscripts.
Provide detailed descriptions, examples, and reference information onhow to script the numerous system-level programs on Mac OS 9 (), such as the Finder on bothOS versions, Sherlock, and Network Setup Scripting.
Give scripters general insight on how to approach the scripting ofseveral programs that can be automated by AppleScript, such as AdobeIllustrator and Photoshop, FileMaker Pro, QuarkXPress, SoundJam MP,and OutLook Express. The mantra is, study theapplication class in theprograms AppleScript dictionary andyoull be up and running with scripting that programbefore you know it. ( discusses theapplication class in general terms; while the application classes ofall the various system components are described in detail throughoutthe book.)
Hopefully, this book will help reveal AppleScript to more Macintoshusers, thus providing them with another outlet for creativity andproductivity.
Organization of This Book
AppleScript in a Nutshell is structured in sixparts.
This section provides an overview of AppleScript and Script Editor,the free AppleScript development tool that installs with theMacintosh. Quick studies and experienced programmers will probably beable to develop their first AppleScripts (if you have never usedAppleScript before) based on a reading of this introductory sectionalone. as a helpfulreference to Script Editor as you use this Apple Computer tool todevelop your scripts.
This AppleScript overview includes a description of how AppleScriptis primarily used, an Apple-event tutorial, and a compressed languagereference for those who want to dive right into scripting. Noviceusers should start here with the book, while very experiencedAppleScripters may use this section as a review or skip over it.
This chapter describes all of Script Editorsprimary menu commands and controls. It also explains the variousoptions for saving AppleScript files.
If scripters need more information on specific language features,this is the place to look. The core-language information is presentedwith syntax examples, code examples, and text descriptions.Everything is arranged in alphabetical order to make things easy tolocate. This includes the various data types (i.e., how AppleScriptstores data in memory), operators (such as the common Math operatorsand the string-concatenation operator&), and how to setAppleScript variables and create user-defined functions, as well asadvanced features, such as creating object-oriented script objects().
This chapter describes the built-in AppleScript data types, includingstring
, integer
,real
, list
, andrecord
. Comparisons with programming languages aremade where it is appropriate (e.g., a list
is likean array, and a record
is an associative array).
Use this chapter as a reference to the built-in symbols (e.g., &,+, *, - ) that you can use in AppleScript expressions.
AppleScript provides several English-language terms to use when thescript refers to objects on your computer system, such as files,folders, disks, and applications. This chapter is an alphabeticalreference to these terms (e.g., first
,every
, id
,where
).
AppleScript, like other languages, uses variables as placeholdersthat represent data (e.g., strings or numbers). This chapterdescribes the rules for naming and creating your own variables; italso provides a reference to AppleScripts constantsand predefined variables (like pi
).
This chapter is an alphabetical reference toAppleScripts flow-control statements, such asif
, repeat
,try
, exit
, andcontinue
.
This chapter is a tutorial on creating user-defined subroutines,which are also called handlers, functions, or methods (inobject-oriented parlance). The second part of this chapter describesfive special handlers in AppleScript: idle,open, quit, reopen, andrun.
AppleScripters can create script objects, which are user-definedtypes that can have their own attributes and methods. This chapteralso describes function libraries, which are script objects that giveother external scripts the ability to load and/or call theobjects own functions.