Cover image for AppleScript: The Definitive Guide, 2nd Edition

Book description

Mac users everywhere--even those who know nothing about programming--are discovering the value of the latest version of AppleScript, Apple's vastly improved scripting language for Mac OS X Tiger. And with this new edition of the top-selling AppleScript: The Definitive Guide, anyone, regardless of your level of experience, can learn to use AppleScript to make your Mac time more efficient and more enjoyable by automating repetitive tasks, customizing applications, and even controlling complex workflows.

Fully revised and updated--and with more and better examples than ever--AppleScript: The Definitive Guide, 2nd Edition explores AppleScript 1.10 from the ground up. You will learn how AppleScript works and how to use it in a variety of contexts: in everyday scripts to process automation, in CGI scripts for developing applications in Cocoa, or in combination with other scripting languages like Perl and Ruby.

AppleScript has shipped with every Mac since System 7 in 1991, and its ease of use and English-friendly dialect are highly appealing to most Mac fans. Novices, developers, and everyone in between who wants to know how, where, and why to use AppleScript will find AppleScript: The Definitive Guide, 2nd Edition to be the most complete source on the subject available. It's as perfect for beginners who want to write their first script as it is for experienced users who need a definitive reference close at hand.

AppleScript: The Definitive Guide, 2nd Edition begins with a relevant and useful AppleScript overview and then gets quickly to the language itself; when you have a good handle on that, you get to see AppleScript in action, and learn how to put it into action for you. An entirely new chapter shows developers how to make your Mac applications scriptable, and how to give them that Mac OS X look and feel with AppleScript Studio. Thorough appendixes deliver additional tools and resources you won't find anywhere else. Reviewed and approved by Apple, this indispensable guide carries the ADC (Apple Developer Connection) logo.

Table of Contents

  1. AppleScript: The Definitive Guide, 2nd Edition
  2. Preface
    1. The Scope of This Book
    2. Versions
    3. How This Book Is Organized
      1. Part I, AppleScript Overview
      2. Part II, The AppleScript Language
      3. Part III, AppleScript in Action
      4. Part IV, Appendixes
    4. Conventions Used in This Book
    5. How to Contact Us
    6. Safari® Enabled
    7. Acknowledgments (First Edition)
    8. Acknowledgments (Second Edition)
  3. I. AppleScript Overview
    1. 1. Why to Use AppleScript
      1. 1.1. The Nature and Purpose of AppleScript
      2. 1.2. Is This Application Scriptable?
      3. 1.3. Calculation and Repetition
      4. 1.4. Reduction
      5. 1.5. Customization
      6. 1.6. Combining Specialties
    2. 2. Where to Use AppleScript
      1. 2.1. Script Editor
        1. 2.1.1. Apple's Script Editor
        2. 2.1.2. Smile
        3. 2.1.3. Script Debugger
      2. 2.2. Internally Scriptable Application
      3. 2.3. Script Runner
      4. 2.4. Automatic Location
      5. 2.5. Application
        1. 2.5.1. Applet
        2. 2.5.2. AppleScript Studio
        3. 2.5.3. Cocoa
      6. 2.6. Unix
      7. 2.7. Hyperlinks
      8. 2.8. Automator
    3. 3. Basic Concepts
      1. 3.1. Apple Events
        1. 3.1.1. Reply
        2. 3.1.2. Scriptability
        3. 3.1.3. The Life of an Apple Event
        4. 3.1.4. What an Apple Event Looks Like
        5. 3.1.5. Go and Catch an Apple Event
        6. 3.1.6. What All This Has to Do with AppleScript
      2. 3.2. The Open Scripting Architecture
        1. 3.2.1. Components
        2. 3.2.2. Other Scripting Languages
        3. 3.2.3. Talking to a Scripting Component
        4. 3.2.4. Maintenance of State
      3. 3.3. Script
        1. 3.3.1. Script as Drive
        2. 3.3.2. Script as Program
        3. 3.3.3. Script as Script Object
        4. 3.3.4. Script as Scripting Component Operand
        5. 3.3.5. Script as File
      4. 3.4. Compiling and Decompiling
        1. 3.4.1. Compiling
        2. 3.4.2. Decompiling
      5. 3.5. Compiled Script Files
        1. 3.5.1. Compiled Script File Formats
        2. 3.5.2. Run-only Scripts
      6. 3.6. Script Text File
      7. 3.7. Applet and Droplet
      8. 3.8. Scripting Addition
      9. 3.9. Dictionary
        1. 3.9.1. Dictionary Formats
        2. 3.9.2. Dictionary Troubles
      10. 3.10. Missing External Referents
        1. 3.10.1. Application Missing at Compile Time
        2. 3.10.2. Application Missing at Decompile Time
        3. 3.10.3. Application Missing When a Compiled Script Runs
        4. 3.10.4. Application Missing When an Applet Launches
        5. 3.10.5. Scripting Addition Missing
      11. 3.11. Modes of Scriptability
        1. 3.11.1. Recordability
        2. 3.11.2. Attachability
  4. II. The AppleScript Language
    1. 4. Introducing the Language
      1. 4.1. A Little Language
      2. 4.2. Extensibility and Its Perils
      3. 4.3. The "English-likeness" Monster
      4. 4.4. Object-likeness
      5. 4.5. LISP-likeness
      6. 4.6. The Learning Curve
    2. 5. Syntactic Ground of Being
      1. 5.1. Lines
        1. 5.1.1. Line-Break Characters
        2. 5.1.2. Line-Break Characters in Strings
        3. 5.1.3. Continuation Character
      2. 5.2. Result
        1. 5.2.1. Explicit Result
        2. 5.2.2. Implicit Result
      3. 5.3. Comments
      4. 5.4. Abbreviations and Synonyms
      5. 5.5. Blocks
      6. 5.6. The
    3. 6. A Map of the World
      1. 6.1. Scope Blocks
      2. 6.2. Levels and Nesting
      3. 6.3. The Top Level
      4. 6.4. Code and the Run Handler
      5. 6.5. Variables
    4. 7. Variables
      1. 7.2. Assignment and Retrieval
        1. Reference Section
          1. set
        2. Reference Section
          1. copy
        3. 7.2.1. Set by Reference
        4. 7.2.2. Multiple Assignment
        5. 7.2.3. Retrieval
      2. 7.3. Declaration and Definition of Variables
        1. 7.3.1. Definition
        2. 7.3.2. Initialization
        3. 7.3.3. Typing
      3. 7.4. Variable Names
        1. 7.4.1. Case-Insensitivity of Variable Names
        2. 7.4.2. Memory of Variable Names
        3. 7.4.3. Variable Names and Vertical Bars
    5. 8. Script Objects
      1. 8.1. Script Object Definition
      2. 8.2. Run Handler
      3. 8.3. Script Properties
      4. 8.4. Script Objects as Values
      5. 8.5. Top-Level Entities
        1. 8.5.1. Accessing Top-Level Entities
        2. 8.5.2. Persistence of Top-Level Entities
          1. 8.5.2.1. What reinitializes top-level entities
          2. 8.5.2.2. File-level persistence
      6. 8.6. Compiled Script Files as Script Objects
        1. Reference Section
          1. load script
        2. Reference Section
          1. run script
        3. Reference Section
          1. store script
        4. 8.6.1. Data Storage
        5. 8.6.2. Library
      7. 8.7. Inheritance
        1. 8.7.1. Polymorphism
        2. 8.7.2. Continue
        3. 8.7.3. The Implicit Parent Chain
        4. 8.7.4. Non-Script Parent
        5. 8.7.5. Handler Calls, Commands, and Script Objects
    6. 9. Handlers
      1. 9.1. Handler Definition
      2. 9.2. Returned Value
      3. 9.3. Handlers as Values
      4. 9.4. Parameters
      5. 9.5. Pass by Reference
      6. 9.6. Syntax of Defining and Calling a Handler
        1. 9.6.1. Optional Parameters
        2. 9.6.2. No Parameters
        3. 9.6.3. Positional Parameters
        4. 9.6.4. Prepositional Parameters
        5. 9.6.5. Named Parameters
      7. 9.7. Event Handlers
      8. 9.8. The Run Handler
      9. 9.9. Recursion
      10. 9.10. Power Handler Tricks
        1. 9.10.1. Handler and Script Object as Parameter
        2. 9.10.2. Handler and Script Object as Result
    7. 10. Scope
      1. 10.1. Regions of Scope
      2. 10.2. Kinds of Variable
      3. 10.3. Scope of Top-Level Entities
      4. 10.4. Scope of Locals
      5. 10.5. Scope of Globals
        1. 10.5.1. Global Declarations: The Downward Effect
        2. 10.5.2. Global Declarations: The Upward Effect
          1. 10.5.2.1. Global as static
          2. 10.5.2.2. Global as script property
          3. 10.5.2.3. The double effect of global declarations
      6. 10.6. Scope of Undeclared Variables
      7. 10.7. Declare Your Variables
      8. 10.8. Free Variables
      9. 10.9. Redeclaration of Variables
      10. 10.10. Closures
        1. 10.10.1. Closures and Handlers
        2. 10.10.2. Closures and Stored Script Objects
    8. 11. Objects
      1. 11.1. Messages
      2. 11.2. Attributes
      3. 11.3. Class
      4. 11.4. Target
        1. 11.4.1. Direct Object
        2. 11.4.2. Tell Block
        3. 11.4.3. Of
        4. 11.4.4. The Chain of Ofs and Tells
        5. 11.4.5. Terms in Scope
        6. 11.4.6. Nested Targets
      5. 11.5. Get
      6. 11.6. It
      7. 11.7. Me
      8. 11.8. Properties and Elements
      9. 11.9. Element Specifiers
        1. 11.9.1. Name
        2. 11.9.2. Index
        3. 11.9.3. ID
        4. 11.9.4. Some
        5. 11.9.5. Every
        6. 11.9.6. Range
        7. 11.9.7. Relative
        8. 11.9.8. Boolean Test
      10. 11.10. Operations on Multiple References
      11. 11.11. Assignment of Multiple Attributes
      12. 11.12. Object String Specifier
    9. 12. References
      1. 12.1. Reference as Target
      2. 12.2. Reference as Incantation
        1. 12.2.1. Preresolution of Terminology
        2. 12.2.2. Being Careful with References
      3. 12.3. Creating a Reference
      4. 12.4. Identifying References
      5. 12.5. Dereferencing a Reference
      6. 12.6. Trouble with Contents
      7. 12.7. Creating References to Variables
      8. 12.8. Reference as Parameter
    10. 13. Datatypes
      1. 13.1. Application
      2. 13.2. Machine
      3. 13.3. Data
      4. 13.4. Boolean
      5. 13.5. Integer, Real, and Number
      6. 13.6. Date
        1. 13.6.1. Date Properties
      7. 13.7. String
        1. 13.7.1. String Properties
        2. 13.7.2. String Elements
      8. 13.8. Unicode Text
        1. 13.8.1. Forming Unicode Text
        2. 13.8.2. Other Text Classes
      9. 13.9. File and Alias
        1. 13.9.1. Alias
        2. 13.9.2. Other File Classes
        3. 13.9.3. File Properties
        4. 13.9.4. File Classes in Applications
      10. 13.10. List
        1. 13.10.1. List Recursion
        2. 13.10.2. Secondary List Classes
        3. 13.10.3. List Properties
        4. 13.10.4. List Elements
      11. 13.11. Record
        1. 13.11.1. Record Properties
    11. 14. Coercions
      1. 14.1. Implicit Coercion
      2. 14.2. Explicit Coercion
        1. Reference Section
          1. as
        2. Reference Section
          1. get... as
        3. 14.2.1. Coercion by AppleScript
        4. 14.2.2. Coercion by a Scriptable Application
      3. 14.3. Boolean Coercions
      4. 14.4. Number, String, and Date Coercions
      5. 14.5. File Coercions
      6. 14.6. List Coercions
      7. 14.7. Unit Conversions
    12. 15. Operators
      1. 15.1. Implicit Coercion
      2. 15.2. Arithmetic Operators
        1. +
        2. -
        3. *
        4. /
        5. div
        6. mod
        7. ^
      3. 15.3. Boolean Operators
        1. and
        2. or
        3. not
      4. 15.4. Comparison Operators
        1. = (is)
        2. ≠ (is not)
        3. <
        4. >
      5. 15.5. Containment Operators
        1. contains, does not contain, is in, is not in
        2. begins with
        3. ends with
      6. 15.6. Concatenation Operator
        1. &
      7. 15.7. Parentheses
      8. 15.8. Who Performs an Operation
        1. 15.8.1. Direct Operations
        2. 15.8.2. Boolean Test Element Specifiers
    13. 16. Global Properties
      1. 16.1. Strings
        1. return
        2. tab
        3. quote
        4. space
        5. text item delimiters
      2. 16.2. Numbers
        1. pi
        2. minutes
        3. hours
        4. days
        5. weeks
      3. 16.3. Miscellaneous
        1. version
    14. 17. Constants
      1. true , false
      2. yes, no, ask
      3. missing value
      4. null
      5. plain, bold, italic, outline, shadow, underline, superscript, subscript, strikethrough, small caps, all caps, all lowercase, condensed, expanded, hidden
      6. case, diacriticals, white space, hyphens, expansion, punctuation, numeric strings
      7. application responses
      8. current application
      9. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
      10. January, February, March, April, May, June, July, August, September, October, November, December
    15. 18. Commands
      1. 18.1. Application Commands
        1. launch
        2. activate
        3. reopen
        4. quit
      2. 18.2. Standard Commands
          1. count
      3. 18.3. Logging Commands
        1. log
        2. stop log, start log
    16. 19. Control
      1. 19.1. Branching
      2. 19.2. Looping
        1. 19.2.1. Repeat Forever
        2. 19.2.2. Repeat N Times
        3. 19.2.3. Repeat While
        4. 19.2.4. Repeat Until
        5. 19.2.5. Repeat With
        6. 19.2.6. Repeat With... In
      3. 19.3. Tell
      4. 19.4. Using Terms From
      5. 19.5. With
        1. 19.5.1. Timeout
        2. 19.5.2. Transaction
      6. 19.6. Considering/Ignoring
        1. 19.6.1. Ignoring Application Responses
        2. 19.6.2. String Considerations
      7. 19.7. Errors
        1. 19.7.1. Throwing an Error
        2. 19.7.2. Catching an Error
      8. 19.8. Second-Level Evaluation
  5. III. AppleScript In Action
    1. 20. Dictionaries
      1. 20.1. Resolution of Terminology
        1. 20.1.1. Loading the Dictionary
        2. 20.1.2. Translating the Terms
          1. 20.1.2.1. The innermost application dictionary
          2. 20.1.2.2. Hunting for each term
      2. 20.2. Terminology Clash
        1. 20.2.1. Compile-time Error
        2. 20.2.2. Runtime Error
        3. 20.2.3. No Error, Surprising Behavior
        4. 20.2.4. Detecting Terminology Clash
        5. 20.2.5. No Terminology Clash
        6. 20.2.6. Resolving Terminology Clash
        7. 20.2.7. Clash Between Dictionaries
      3. 20.3. Nonsensical Apple Events
      4. 20.4. Raw Four-Letter Codes
      5. 20.5. Multiple-Word Terms
      6. 20.6. What's in a Dictionary
        1. 20.6.1. Value Types
        2. 20.6.2. Enumerations
        3. 20.6.3. Classes
          1. 20.6.3.1. Plurals
          2. 20.6.3.2. Class inheritance
        4. 20.6.4. Properties and Elements
        5. 20.6.5. Records
        6. 20.6.6. Events
        7. 20.6.7. Suites
      7. 20.7. The 'aeut ' Resource
      8. 20.8. Inadequacies of the Dictionary
        1. 20.8.1. Defects in the Object Model
        2. 20.8.2. Defective Element Specifiers
        3. 20.8.3. Properties with Eponymous Classes
        4. 20.8.4. Clashes with AppleScript
        5. 20.8.5. Wrong Value Types
        6. 20.8.6. Wrong Parameter Details
        7. 20.8.7. Make
        8. 20.8.8. Selection
        9. 20.8.9. Idioms for Common Tasks
        10. 20.8.10. Events and Classes
        11. 20.8.11. Coercions
        12. 20.8.12. Bad Grammar
        13. 20.8.13. Multiple Listings
        14. 20.8.14. Busted Scriptability
        15. 20.8.15. Bad Comments
    2. 21. Scripting Additions
      1. 21.1. Pros and Cons of Scripting Additions
      2. 21.2. Classic Scripting Additions
      3. 21.3. Loading Scripting Additions
      4. 21.4. Standard Scripting Addition Commands
        1. 21.4.1. Dialogs
          1. display dialog
          2. display alert
          3. choose from list
          4. choose file
          5. choose folder
          6. choose file name
          7. choose application
          8. choose URL
          9. choose remote application
          10. choose color
        2. 21.4.2. Noises
          1. beep
          2. get volume settings
          3. set volume
          4. say
        3. 21.4.3. File and Machine Information
          1. system info
          2. system attribute
          3. path to
          4. path to
          5. path to resource
          6. list disks
          7. list folder
          8. info for
        4. 21.4.4. File Data
            1. open for access
            2. read
            3. write
            4. get eof
            5. set eof
            6. close access
        5. 21.4.5. String and Clipboard
          1. ASCII character
          2. ASCII number
          3. offset
          4. summarize
          5. set the clipboard to
          6. clipboard info
          7. the clipboard
        6. 21.4.6. Numbers and Dates
          1. round
          2. random number
          3. current date
          4. time to GMT
        7. 21.4.7. Miscellaneous
          1. delay
          2. mount volume
          3. scripting components
          4. open location
    3. 22. Speed
      1. 22.1. Tools of the Trade
      2. 22.2. Apple Events
      3. 22.3. List Access
      4. 22.4. Scripting Additions
      5. 22.5. Context
    4. 23. Scriptable Applications
      1. 23.1. Targeting Scriptable Applications
        1. 23.1.1. Local Applications
        2. 23.1.2. Remote Applications
        3. 23.1.3. XML-RPC and SOAP
      2. 23.2. Some Scriptable Applications
        1. 23.2.1. iApps and Utilities
        2. 23.2.2. Finder
        3. 23.2.3. System Events
        4. 23.2.4. SpeechRecognitionServer
        5. 23.2.5. URL Access Scripting
        6. 23.2.6. Keychain Scripting
        7. 23.2.7. Image Events
        8. 23.2.8. Database Events
    5. 24. Unscriptable Applications
      1. 24.1. Historical Perspective
      2. 24.2. Getting Started with Accessibility
      3. 24.3. GUI Scripting Examples
    6. 25. Unix
      1. 25.1. Do Shell Script
      2. 25.2. Osascript
    7. 26. Triggering Scripts Automatically
      1. 26.1. Digital Hub Scripting
      2. 26.2. Folder Actions
      3. 26.3. CGI Application
      4. 26.4. Timers, Hooks, Attachability, Observability
    8. 27. Writing Applications
      1. 27.1. Applets
        1. 27.1.1. Applet Options
        2. 27.1.2. Editing an Applet
        3. 27.1.3. Applet Event Handlers
        4. 27.1.4. Droplets
        5. 27.1.5. Persistence
        6. 27.1.6. Applet Scriptability
      2. 27.2. AppleScript Studio
        1. 27.2.1. Cocoa and AppleScript Studio
        2. 27.2.2. The Pieces of AppleScript Studio
        3. 27.2.3. AppleScript Studio Example
        4. 27.2.4. Automator Actions
      3. 27.3. Cocoa Scripting
      4. 27.4. AppleScript Studio Scriptability
  6. IV. Appendixes
    1. A. The AppleScript Experience
      1. A.1. The Problem
      2. A.2. A Day in the Life
        1. A.2.1. Caught in the Web of Words
        2. A.2.2. One for All and All for One
        3. A.2.3. Seek and Ye Shall Find
        4. A.2.4. Turning the Tables
        5. A.2.5. Refiner's Fire
        6. A.2.6. Naming of Parts
        7. A.2.7. Practice Makes Perfect
        8. A.2.8. Finder's Keepers
        9. A.2.9. I've Got a Little List
      3. A.3. Conclusions, Lessons, and Advice
    2. B. Apple Events Without AppleScript
      1. B.1. Pure Raw Apple Events
      2. B.2. JavaScript
      3. B.3. UserTalk
      4. B.4. Perl
      5. B.5. Python
    3. C. Tools and Resources
      1. C.1. Scripting Software and Tools
      2. C.2. Scriptable Software
      3. C.3. AppleScript Documentation
      4. C.4. Writing a Scripting Addition
      5. C.5. Writing a Scriptable Application
      6. C.6. Portals, Instruction, and Repositories
      7. C.7. Mailing Lists
      8. C.8. Books
      9. C.9. Unix Scripting
  7. About the Author
  8. Colophon
  9. Copyright