You are previewing AppleScript: The Definitive Guide.
O'Reilly logo
AppleScript: The Definitive Guide

Book Description

If you want to know all about AppleScript--the how, where, and why of using it--dig into AppleScript: The Definitive Guide. It doesn't make the mistake that other books do: it isn't about scripting this or that particular 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. AppleScript is a dynamic, object-oriented scripting system that allows Mac users--even novices who know nothing about programming--to directly control Macintosh applications, including the Mac OS itself. You can write scripts to automate repetitive tasks, customize applications, and even control complex workflows. AppleScript has always been useful, but with Mac OS X it's even more so. Nearly every application that comes with Mac OS X is scriptable. Even non-scriptable applications can often be driven with AppleScript, thanks to the new Accessibility API and GUI Scripting technologies. And now AppleScripters can put a true Aqua interface around their scripts! There's never been a more exciting time for AppleScript users. AppleScript: The Definitive Guide explores and teaches the language from the ground up. If you're a beginner and want to learn how to write your first script or just understand what the excitement is all about, you'll be able to do so after reading this book. AppleScript: The Definitive Guide is the quintessential guide to this important Mac tool. Regardless of their level of experience, AppleScripters everywhere will turn to this book again and again.

Table of Contents

  1. AppleScript: The Definitive Guide
    1. 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. Acknowledgments
    2. I. AppleScript Overview
      1. 1. Ways to Use AppleScript
        1. The Nature and Purpose of AppleScript
        2. Is This Application Scriptable?
        3. Calculation and Repetition
        4. Reduction
        5. Customization
        6. Combining Specialties
      2. 2. Places to Use AppleScript
        1. Script Editor
        2. Scripting Environment
        3. Internally Scriptable Application
        4. Script Runner
        5. Automatic Location
        6. Application
        7. Unix
      3. 3. The AppleScript Experience
        1. The Problem
        2. A Day in the Life
          1. Caught in the Web of Words
          2. One for All and All for One
          3. Seek and Ye Shall Find
          4. Turning the Tables
          5. Refiner’s Fire
          6. Naming of Parts
          7. Practice Makes Perfect
          8. Finder’s Keepers
          9. I’ve Got a Little List
        3. Conclusions, Lessons, and Advice
      4. 4. Basic Concepts
        1. Apple Event
          1. Command, Query, and Reply
          2. Scriptability
          3. The Life of an Apple Event
          4. What an Apple Event Looks Like
          5. Go and Catch an Apple Event
          6. What All This Has to Do with AppleScript
        2. The Open Scripting Architecture
          1. Components
          2. Other Scripting Languages
          3. Talking to a Scripting Component
          4. Maintenance of State
        3. Script
          1. Script as Drive
          2. Script as Program
          3. Script as Script Object
          4. Script as Scripting Component Operand
          5. Script as File
        4. Compiling and Decompiling
          1. Compiling
          2. Decompiling
          3. External Referents Needed at Compile Time
          4. Saving Compiled Scripts
            1. References to applications
            2. Run-only scripts
        5. Script Text File
        6. Applet and Droplet
        7. Scripting Addition
        8. Dictionary
        9. Scriptable, Recordable, Attachable
          1. Scriptable
          2. Recordable
          3. Attachable
    3. II. The AppleScript Language
      1. 5. Introducing AppleScript
        1. A “Little Language”
        2. Extensibility and Its Perils
        3. The “English-likeness” Monster
        4. Object-likeness
        5. LISP-likeness
        6. The Learning Curve
      2. 6. Syntactic Ground of Being
        1. Lines
          1. Line Break Characters in Literal Strings
          2. Continuation Character
        2. Result
          1. Explicit Result
          2. Implicit Result
        3. Comments
        4. Abbreviations and Synonyms
        5. Blocks
        6. The
      3. 7. Variables
        1. Assignment and Retrieval
          1. set
          2. copy
        2. Declaration and Definition of Variables
          1. Definition
          2. Initialization
          3. Typing
          4. Explicit Declaration
        3. Variable Names
        4. Scoping of Variables
          1. How Scoping Is Meaningful
          2. Explicit Locals
          3. Global Declarations: The Downward Effect
          4. Global Declarations: The Upward Effect
          5. Undeclared Variables
            1. Undeclared variables at top level
            2. Undeclared variables not at top level
            3. Declare your variables
          6. Free Variables
          7. Redeclaration of Locals and Globals
        5. Script Properties
          1. Scoping of Properties
          2. Top-Level Properties Are Globals
          3. Delayed Declaration of Properties
          4. Redeclaration of Properties
        6. Lifetime of Variables
      4. 8. Handlers
        1. Returned Value
        2. Parameters
        3. Syntax of Defining and Calling a Handler
          1. No Parameters
          2. Unnamed Parameters
          3. Prepositional Parameters
          4. Named Parameters
        4. Pass By Reference
        5. Scoping of Handlers
          1. Handler Calls Within Script Objects
          2. Recursion
          3. The Run Handler
        6. Handlers as Values
          1. Handlers as Parameters
          2. Handlers as Handler Results
      5. 9. Script Objects
        1. Scoping of Script Objects
          1. Script Objects in Handlers
          2. Free Variables
        2. Top-Level Entities
          1. Accessing Top-Level Entities
          2. Persistence of Top-Level Entities
        3. Script Object’s Run Handler
        4. Handler Calls
        5. Script Objects as Values
          1. Set By Reference
          2. Pass By Reference
          3. Script Object as Handler Result
            1. Closures
            2. Constructors
        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. Library
          5. Data Storage
          6. Context
        7. Inheritance
          1. Polymorphism
          2. Continue
          3. The Implicit Parent Chain
      6. 10. Objects
        1. Class
        2. Target
          1. The Chain of Ofs and Tells
          2. Multiple Assignments
          3. Nesting Target Specifications
          4. Direct Object
          5. Names in Scope
        3. Get
        4. It
        5. Me
        6. Properties and Elements
        7. Element Specifiers
          1. Name
          2. Index
          3. ID
          4. Some
          5. Every
          6. Range
          7. Relative
          8. Boolean Test
        8. Properties of Multiple References
        9. Object String Specifier
      7. 11. References
        1. References as Incantations
          1. Pre-Resolution of Terminology
          2. Being Careful with References
        2. Creating a Reference
        3. Identifying References
        4. Dereferencing a Reference
        5. Creating References to Local Variables
        6. Reference as Parameter
      8. 12. Control
        1. Branching
        2. Looping
          1. Repeat Forever
          2. Repeat While
          3. Repeat Until
          4. Repeat With
          5. Repeat With...In
          6. Repeat N Times
          7. Being Careful with Loops
        3. Tell
        4. Using Terms From
        5. With
          1. Timeout
          2. Transaction
        6. Considering/Ignoring
          1. Ignoring Application Responses
          2. String Considerations
        7. Errors
          1. Throw
          2. Catch
        8. Second-Level Evaluation
      9. 13. Datatypes
        1. Boolean
        2. Integer, Real, and Number
        3. Date
          1. Date Properties
        4. String
          1. String Properties
          2. String Elements
        5. Unicode Text
        6. Styled Text
        7. File
          1. File Properties
        8. Alias
        9. Application
        10. Machine
        11. Data
        12. List
          1. List Properties
          2. List Elements
          3. Speed of List Access
        13. Record
          1. Record Properties
      10. 14. Coercions
        1. Implicit Coercion
        2. Explicit Coercion
          1. as
          2. get...as
        3. Boolean Coercions
        4. String, Number and Date Coercions
        5. File Coercions
        6. List Coercions
        7. Unit Conversions
      11. 15. Operators
        1. Arithmetic Operators
          1. +
          2. -
          3. *
          4. /
          5. div
          6. mod
          7. ^
        2. Boolean Operators
          1. and
          2. or
          3. not
        3. Comparison Operators
          1. = (is)
          2. ≠ (is not)
          3. <
          4. >
        4. Containment Operators
          1. contains, does not contain, is in, is not in
          2. begins with
          3. ends with
        5. Concatenation Operator
          1. &
        6. Parentheses
        7. Who Performs an Operation
          1. Direct Operations
          2. Boolean Test Element Specifiers
      12. 16. Global Properties
        1. Strings
          1. return
          2. tab
          3. space
          4. text item delimiters
        2. Numbers
          1. pi
          2. minutes
          3. hours
          4. days
          5. weeks
        3. Miscellaneous
          1. version
      13. 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
        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
      14. 18. Commands
        1. Application Commands
          1. launch
          2. activate
          3. reopen
          4. quit
        2. Logging Commands
          1. log
          2. stop log, start log
    4. III. AppleScript In Action
      1. 19. Dictionaries
        1. Resolution of Terminology
          1. Loading the Dictionary
          2. Translating the Terms
            1. The innermost application dictionary
            2. Hunting for each term
            3. Substituting four-letter codes
        2. Resolution Difficulties
          1. Conflict Resolution
            1. Clash caused by the programmer
            2. Clash between dictionaries
          2. Invalid Apple Events
          3. Raw Four-Letter Codes
          4. Multiple-Word Commands
        3. What’s in a Dictionary
          1. Enumerations
          2. Value Type
          3. Event
          4. Classes
            1. Plurals
            2. Class inheritance
          5. Properties and Elements
            1. How properties are listed
            2. How elements are listed
            3. The object model
          6. Pseudo-Classes
          7. Suites
        4. The ‘aeut’ Resource
        5. Inadequacies of the Dictionary
          1. Defective Object Model
          2. Defective Element Specifiers
          3. Make
          4. Idioms for Common Tasks
          5. Events and Classes
          6. Inconsistent Return Types
          7. Coercions
          8. Enumerations
          9. Borderline Syntax
          10. Bad Grammar
          11. Lying Dictionary
          12. Bad Comments
      2. 20. Scripting Additions
        1. Pros and Cons of Scripting Additions
        2. Scripting Additions and Speed
        3. Classic Scripting Additions
        4. Loading Scripting Additions
        5. Standard Scripting Addition Commands
          1. Dialogs
            1. display dialog
            2. choose from list
            3. choose file
            4. choose folder
            5. choose file name
            6. choose application
            7. choose URL
            8. choose color
          2. Noises
            1. beep
            2. set volume
            3. say
          3. File and Machine Information
            1. system attribute
            2. path to
            3. list disks
            4. list folder
            5. info for
          4. File Data
            1. open for access
            2. read
            3. write
            4. get eof
            5. set eof
            6. close access
          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. Numbers and Dates
            1. round
            2. random number
            3. current date
            4. time to GMT
          7. Miscellaneous
            1. delay
            2. mount volume
            3. scripting components
            4. open location
      3. 21. Scriptable Applications
        1. Targeting Scriptable Applications
          1. Tell and Of
          2. Reference
          3. Local Applications
          4. Remote Applications
          5. XML-RPC and SOAP
        2. Some Scriptable Applications
          1. Finder
          2. Internet Connect
          3. System Events
          4. Speech Recognition Server
          5. URL Access Scripting
          6. Keychain Scripting
          7. Image Events
      4. 22. Unscriptable Applications
        1. Getting Started with Accessibility
        2. GUI Scripting Examples
      5. 23. Unix
        1. Do Shell Script
        2. Osascript
      6. 24. Writing Applications
        1. Applets
          1. Applet Options
          2. Applet Handlers
          3. Droplets
          4. Persistence
        2. Digital Hub Scripting
        3. Folder Actions
        4. CGI Application
        5. AppleScript Studio
          1. Cocoa
          2. How AppleScript Studio Relates to Cocoa
          3. How Much Cocoa to Learn
          4. Where and What Is AppleScript Studio
            1. The Developer Tools
            2. Interface Builder
            3. Xcode
            4. AppleScript Studio documentation and examples
            5. The dictionary
          5. AppleScript Studio Example
            1. Create the project
            2. Incorporate extra bundle components
            3. Create the interface
            4. Add AppleScript names and handlers
            5. The code
            6. Final steps
            7. Scriptability
    5. IV. Appendixes
      1. A. The ‘aeut’ Resource
      2. B. Tools and Resources
        1. Scripting Software
        2. Other Software Mentioned in This Book
        3. Apple Documentation
        4. Portals, Instruction, and Repositories
        5. Mailing Lists
        6. Books
        7. Unix Scripting
    6. Index
    7. Colophon