You are previewing Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition.
O'Reilly logo
Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition

Book Description

AppleScript is an English-like, easy-to-understand scripting language built into every Mac. AppleScript can automate hundreds of AppleScriptable applications, performing tasks both large and small, complex and simple.

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition has been completely updated for Mac OS X Snow Leopard. It's all here, with an emphasis on practical information that will help you solve any automation problem—from the most mundane repetitive tasks to highly integrated workflows of complex systems.

  • Friendly enough for beginners, detailed enough for advanced AppleScripters

  • Includes major contributions from expert AppleScripters: Emmanuel Levy, Harald Monihart, Ian Piper, Shane Stanley, Barry Wainwright, Craig Williams, and foreword by AppleScript inventor, William Cook

Table of Contents

  1. Copyright
  2. Foreword
  3. About the Authors
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Introduction
    1. If You Have Never Scripted Before...
    2. If You Are Already an AppleScript User...
    3. If You Are an Experienced Programmer...
    4. Resources
    5. Formatting Conventions Used in This Book
    6. Errata
  7. 1. Welcome to AppleScript
    1. 1. Introducing AppleScript
      1. 1.1. Automating with AppleScript: From Small Tasks to Large
        1. 1.1.1. Automating Small Tasks
        2. 1.1.2. Automating Large Tasks
        3. 1.1.3. When to Automate (or Not)?
      2. 1.2. The Golden Triangle: Scriptable Applications, the AppleScript Language, and the Scripts You Write
        1. 1.2.1. What Is a Scriptable Application?
          1. 1.2.1.1. Introducing Objects
          2. 1.2.1.2. Introducing Commands
        2. 1.2.2. What Is the AppleScript Language?
        3. 1.2.3. What Is a Script?
      3. 1.3. Your First AppleScript: Hello World!
      4. 1.4. Summary
    2. 2. AppleScript in Principle
      1. 2.1. The Four Key Concepts of Application Scripting
        1. 2.1.1. How Applications Represent Information As Objects
        2. 2.1.2. How Applications Manipulate Objects Using Commands
        3. 2.1.3. How Applications Organize Their Objects into Object Models
        4. 2.1.4. How Applications Describe Their Objects and Commands in Dictionaries
      2. 2.2. The Four Key Concepts of the AppleScript Language
        1. 2.2.1. How AppleScript Represents Information As Objects
        2. 2.2.2. How AppleScript Manipulates Objects Using Commands, Operators, and Variables
          1. 2.2.2.1. Using Commands
          2. 2.2.2.2. Using Operators
          3. 2.2.2.3. Using Variables
        3. 2.2.3. How AppleScript Makes Decisions on What to Do and When to Do It
        4. 2.2.4. How AppleScript Organizes Code Using Handlers and Script Objects
      3. 2.3. Tying It All Together: What Makes AppleScript Special
        1. 2.3.1. The English-like Syntax
        2. 2.3.2. Built-in Application Scripting Support
        3. 2.3.3. Attaching Scripts to Applications
        4. 2.3.4. The AppleScript Community
      4. 2.4. Summary
    3. 3. AppleScript in Practice
      1. 3.1. The Project: Tidy Desktop
      2. 3.2. Planning the Script
      3. 3.3. Writing the Script
        1. 3.3.1. Creating the Folder Name
          1. 3.3.1.1. Getting Today's Date As Text
          2. 3.3.1.2. Putting the Name Together
        2. 3.3.2. Interacting with the File System
          1. 3.3.2.1. Addressing the Finder
          2. 3.3.2.2. Creating the Archive Folder
          3. 3.3.2.3. Moving the Files
      4. 3.4. Final Thoughts
        1. 3.4.1. Improving Reliability
        2. 3.4.2. Adding Features
        3. 3.4.3. Documenting Your Code
      5. 3.5. Summary
  8. 2. Understanding AppleScript
    1. 4. Writing Scripts in AppleScript Editor
      1. 4.1. Working with Script Windows
        1. 4.1.1. Compiling a Script
        2. 4.1.2. Running and Stopping a Script
        3. 4.1.3. Recording a Script
        4. 4.1.4. Viewing the Result of Running a Script
        5. 4.1.5. Viewing the Events Sent by a Running Script
        6. 4.1.6. Adding a Description to a Script
      2. 4.2. Viewing Application Dictionaries
      3. 4.3. Other Useful AppleScript Editor Features
        1. 4.3.1. Using the Library Window
        2. 4.3.2. Navigating to a Handler in a Script
        3. 4.3.3. Viewing the Event Log History
        4. 4.3.4. Scripting the AppleScript Editor
        5. 4.3.5. Using Context Menu Scripts
        6. 4.3.6. Viewing the Bundle Contents Drawer
        7. 4.3.7. Setting the Script Formatting Preferences
      4. 4.4. Saving Scripts
        1. 4.4.1. Text-Based Scripts
        2. 4.4.2. Compiled Scripts
          1. 4.4.2.1. Saving Scripts As Run Only
          2. 4.4.2.2. Saving Scripts As Bundles
        3. 4.4.3. Script-Based Applications (Applets)
          1. 4.4.3.1. Droplets
          2. 4.4.3.2. Stay-Open Applets
          3. 4.4.3.3. Saving Applets As Run Only
          4. 4.4.3.4. Customizing the Startup Screen
      5. 4.5. Other Tools for Writing and Running AppleScripts
        1. 4.5.1. The System-wide Script Menu
          1. 4.5.1.1. Where Are the Menu Scripts Stored?
          2. 4.5.1.2. Adding Your Own Scripts
        2. 4.5.2. Third-Party AppleScript Editors
          1. 4.5.2.1. Smile
          2. 4.5.2.2. Script Debugger
        3. 4.5.3. Developing GUI Applications
        4. 4.5.4. Command-Line Tools
      6. 4.6. Summary
    2. 5. Understanding How Application Scripting Works
      1. 5.1. A Quick Application Scripting Example
      2. 5.2. Understanding Objects
        1. 5.2.1. How Objects Are Categorized by Class
        2. 5.2.2. Introducing Object Properties
        3. 5.2.3. Introducing Object Elements
      3. 5.3. Understanding Commands
        1. 5.3.1. Introducing Command Names
        2. 5.3.2. Introducing Command Parameters
        3. 5.3.3. Commands Must Be Targeted at Objects
        4. 5.3.4. Commands Can Return Results or Report Errors
      4. 5.4. Understanding the Application Object Model
        1. 5.4.1. How Objects Can Contain Other Objects
        2. 5.4.2. How AppleScript Refers to Application Objects
        3. 5.4.3. Exploring a Typical Object Model
      5. 5.5. Understanding Application Dictionaries
        1. 5.5.1. Introducing AppleScript Editor's Dictionary Viewer
        2. 5.5.2. How Classes Are Documented
          1. 5.5.2.1. Viewing the Properties and Elements Defined by a Class
          2. 5.5.2.2. How Inheritance Describes Features Shared Between Classes
          3. 5.5.2.3. Class Definitions Can Be Spread Across More than One Suite!
          4. 5.5.2.4. Browsing the Containment Hierarchy
        3. 5.5.3. How Commands Are Documented
        4. 5.5.4. Application Dictionaries Don't Tell You Everything!
      6. 5.6. More on application Objects
        1. 5.6.1. Creating application Objects
          1. 5.6.1.1. Identifying Applications by Name
          2. 5.6.1.2. Identifying Applications by Path
          3. 5.6.1.3. Identifying Applications by Bundle ID
          4. 5.6.1.4. Identifying Applications by Remote URL
        2. 5.6.2. The Standard Properties of application Objects
        3. 5.6.3. How AppleScript Compiles tell application... Blocks
      7. 5.7. More on Constructing References
        1. 5.7.1. Referring to Properties
        2. 5.7.2. Referring to Elements
          1. 5.7.2.1. Identifying All Elements
          2. 5.7.2.2. Identifying an Element by Its Index, or Position
          3. 5.7.2.3. Identifying an Element by Its Name
          4. 5.7.2.4. Identifying an Element by Its Unique ID
          5. 5.7.2.5. Identifying an Element Before or After Another Element
          6. 5.7.2.6. Identifying a Range of Elements
          7. 5.7.2.7. Identifying Elements Using the whose Clause
        3. 5.7.3. Referring to Insertion Locations
      8. 5.8. Summary
    3. 6. Learning to Work with AppleScript Objects
      1. 6.1. How AppleScript Represents Information As Objects
        1. 6.1.1. What Kinds of Objects Does AppleScript Provide?
        2. 6.1.2. Creating New Objects
        3. 6.1.3. Getting Information from Objects
      2. 6.2. How AppleScript Works with Objects
        1. 6.2.1. Manipulating Objects with Commands
          1. 6.2.1.1. Introducing the Five Built-in Commands
          2. 6.2.1.2. Introducing Scripting Addition Commands
          3. 6.2.1.3. Introducing User-Defined Commands
        2. 6.2.2. Manipulating Objects with Operators
          1. 6.2.2.1. What Makes Operators Special?
          2. 6.2.2.2. What Kinds of Operators Does AppleScript Provide?
            1. 6.2.2.2.1. The Concatenation Operator
            2. 6.2.2.2.2. Math Operators
            3. 6.2.2.2.3. Comparison Operators
            4. 6.2.2.2.4. Containment Operators
            5. 6.2.2.2.5. Boolean Operators
            6. 6.2.2.2.6. The Coercion Operator
            7. 6.2.2.2.7. The a reference to Operator
        3. 6.2.3. Coercing Objects
        4. 6.2.4. Storing Objects in Variables
          1. 6.2.4.1. How Are Identifiers Written?
          2. 6.2.4.2. Where Can Variables Be Used?
      3. 6.3. Working with Boolean Objects
        1. 6.3.1. Boolean Operators
          1. 6.3.1.1. The not Operator
          2. 6.3.1.2. The and Operator
          3. 6.3.1.3. The or Operator
          4. 6.3.1.4. How Boolean Operators Are Evaluated
        2. 6.3.2. Boolean-Related Coercions
      4. 6.4. Summary
    4. 7. Working with Text
      1. 7.1. Introducing Text
        1. 7.1.1. A Brief History of Text
          1. 7.1.1.1. When Is Text Not Text?
          2. 7.1.1.2. Understanding Character Sets
          3. 7.1.1.3. The Problem with Character Sets
          4. 7.1.1.4. Unicode to the Rescue!
        2. 7.1.2. Introducing Text in AppleScript 2.0
      2. 7.2. How to Write Literal Strings in AppleScript
      3. 7.3. The Properties of Text
        1. 7.3.1. The class Property
        2. 7.3.2. The length Property
        3. 7.3.3. The quoted form Property
        4. 7.3.4. The id Property
      4. 7.4. The Elements of Text
        1. 7.4.1. Characters
        2. 7.4.2. Words
        3. 7.4.3. Paragraphs
        4. 7.4.4. Text Ranges
      5. 7.5. Operators and Commands
        1. 7.5.1. Joining Strings
        2. 7.5.2. Comparing Strings
        3. 7.5.3. Checking for Strings Within Strings
        4. 7.5.4. Considering and Ignoring Attributes
        5. 7.5.5. The count Command
        6. 7.5.6. The offset Command
        7. 7.5.7. Coercing to and from Text
      6. 7.6. Working with Text Item Delimiters
        1. 7.6.1. Splitting Strings with Text Item Delimiters
        2. 7.6.2. Combining List Items into a Single String
        3. 7.6.3. Finding and Replacing Text
      7. 7.7. How AppleScript Text Works in Tiger
        1. 7.7.1. The Many Different Classes of Text
        2. 7.7.2. Working with the Various Text Classes
        3. 7.7.3. The ASCII number and ASCII character Commands
      8. 7.8. Example Projects
        1. 7.8.1. Defining a Find-and-Replace Command
        2. 7.8.2. Transferring Meeting Arrangements from Mail to iCal
          1. 7.8.2.1. Getting the Message from Mail
          2. 7.8.2.2. Parsing the Message Text
          3. 7.8.2.3. Creating the Event in iCal
      9. 7.9. Summary
    5. 8. Working with Numbers
      1. 8.1. Introducing Integers and Reals
      2. 8.2. Operators and Commands
        1. 8.2.1. Math Operations
          1. 8.2.1.1. Adding, Subtracting, Multiplying, and Dividing Numbers
          2. 8.2.1.2. Negating Numbers
          3. 8.2.1.3. Calculating the Integral and Remainder
          4. 8.2.1.4. Using the Exponent Operator
        2. 8.2.2. Comparison Operations
          1. 8.2.2.1. Comparing Reals Can Be Trickier Than You Think
          2. 8.2.2.2. Performing Multiple Comparisons
        3. 8.2.3. Introducing Operator Precedence
        4. 8.2.4. The round Command
          1. 8.2.4.1. Rounding Up
          2. 8.2.4.2. Rounding Down
          3. 8.2.4.3. Rounding Toward Zero
          4. 8.2.4.4. Rounding to Nearest
          5. 8.2.4.5. Rounding As Taught in School
        5. 8.2.5. The random number Command
          1. 8.2.5.1. Random Number Result
          2. 8.2.5.2. Parameters
        6. 8.2.6. Coercing to and from Numbers
      3. 8.3. Example Projects
        1. 8.3.1. Defining a Round-to-Decimal-Places Command
        2. 8.3.2. Blackjack!
        3. 8.3.3. Kitchen Timer
      4. 8.4. Summary
    6. 9. Working with Dates
      1. 9.1. Introducing Dates
        1. 9.1.1. Understanding Date and Time Formats
        2. 9.1.2. Forming Dates On-the-Fly
        3. 9.1.3. Specifying a Time Relative to a Date
      2. 9.2. The Properties of Dates
        1. 9.2.1. The class Property
        2. 9.2.2. The year Property
        3. 9.2.3. The month Property
        4. 9.2.4. The day Property
        5. 9.2.5. The weekday Property
        6. 9.2.6. The hours Property
        7. 9.2.7. The minutes Property
        8. 9.2.8. The seconds Property
        9. 9.2.9. The time Property
        10. 9.2.10. The date string Property
        11. 9.2.11. The short date string Property
        12. 9.2.12. The time string Property
        13. 9.2.13. Setting Date Properties
      3. 9.3. Operators and Commands
        1. 9.3.1. Comparison Operators
        2. 9.3.2. Math Operators
          1. 9.3.2.1. Some Useful Constants
          2. 9.3.2.2. Calculating Time Differences
        3. 9.3.3. The current date Command
        4. 9.3.4. The time to GMT Command
      4. 9.4. Example Projects
        1. 9.4.1. Deleting Old Files
          1. 9.4.1.1. Creating the Script
          2. 9.4.1.2. Adding the User Interaction Portion
        2. 9.4.2. Alarm Clock
      5. 9.5. Summary
    7. 10. Working with Lists and Records
      1. 10.1. Introducing Lists and Records
        1. 10.1.1. What Is a List?
        2. 10.1.2. What Is a Record?
        3. 10.1.3. How Are Lists and Records Used in Scripts?
      2. 10.2. Working with Lists
        1. 10.2.1. The Properties of a List
          1. 10.2.1.1. The class Property
          2. 10.2.1.2. The length Property
          3. 10.2.1.3. The rest Property
          4. 10.2.1.4. The reverse Property
        2. 10.2.2. The Elements of a List
          1. 10.2.2.1. Getting Items
          2. 10.2.2.2. Setting Items
          3. 10.2.2.3. Adding Items
          4. 10.2.2.4. "Removing" Items
        3. 10.2.3. Operators and Commands
          1. 10.2.3.1. Concatenating Lists
          2. 10.2.3.2. Comparing Lists
          3. 10.2.3.3. Checking for Sublists
            1. 10.2.3.3.1. The starts with Operator
            2. 10.2.3.3.2. The ends with Operator
            3. 10.2.3.3.3. The contains Operator
            4. 10.2.3.3.4. The is in Operator
            5. 10.2.3.3.5. More Uses for Containment Operators
          4. 10.2.3.4. Counting Lists
          5. 10.2.3.5. Coercing to and from Lists
        4. 10.2.4. Processing List Items with a Repeat Loop
        5. 10.2.5. Sorting Items in a List
      3. 10.3. Working with Records
        1. 10.3.1. Getting and Setting the Properties of a Record
        2. 10.3.2. Operators and Commands
          1. 10.3.2.1. Concatenating Records
          2. 10.3.2.2. Comparing Records
          3. 10.3.2.3. Checking for Subrecords
          4. 10.3.2.4. Coercing from Records
          5. 10.3.2.5. Counting Records
      4. 10.4. Example Project: Measuring Word Frequency in TextEdit
        1. 10.4.1. Planning the Script
        2. 10.4.2. Developing the Script
        3. 10.4.3. Conclusion
      5. 10.5. Summary
    8. 11. Storing Objects in Variables
      1. 11.1. Creating Variables
        1. 11.1.1. Declaring Your Variables
        2. 11.1.2. Choosing Your Variable Names
          1. 11.1.2.1. When Keywords and Identifiers Conflict
          2. 11.1.2.2. Tips for Avoiding Problems with Variable Names
          3. 11.1.2.3. Follow Good Naming Practices
          4. 11.1.2.4. Break All the Rules!
      2. 11.2. Working with Local Variables
        1. 11.2.1. Understanding Local Variable Scope
        2. 11.2.2. Passing Values to and from User-Defined Handlers
      3. 11.3. Working with Properties and Global Variables
        1. 11.3.1. Understanding Property and Global Variable Scope
        2. 11.3.2. How Properties (and Global Variables) Can Retain Values Between Runs
        3. 11.3.3. When to Use Properties and Global Variables
          1. 11.3.3.1. Managing User Preferences
          2. 11.3.3.2. Enabling Debugging Code
          3. 11.3.3.3. Clarifying the Meaning of Important Hard-Coded Values
          4. 11.3.3.4. Importing Script Libraries
      4. 11.4. Using AppleScript's Built-in Variables
        1. 11.4.1. Variables Containing Predefined Values
          1. 11.4.1.1. The return, linefeed, space, tab, and quote Variables
          2. 11.4.1.2. The weeks, days, hours, and minutes Variables
          3. 11.4.1.3. The pi Variable
        2. 11.4.2. Variables Containing Objects of Special Interest
          1. 11.4.2.1. The it Variable
          2. 11.4.2.2. The me Variable
          3. 11.4.2.3. The result Variable
      5. 11.5. Summary
    9. 12. More on Commands
      1. 12.1. Understanding How and Where Commands Are Defined
        1. 12.1.1. Application-Defined Commands
        2. 12.1.2. Scripting Addition Commands
        3. 12.1.3. AppleScript Commands
        4. 12.1.4. User-Defined Commands
      2. 12.2. Picking the Right Target for Your Commands
      3. 12.3. The Five Standard AppleScript Commands
        1. 12.3.1. The get Command
        2. 12.3.2. The set Command
        3. 12.3.3. The copy Command
        4. 12.3.4. The count Command
        5. 12.3.5. The run Command
      4. 12.4. Commands That All Applications Understand
        1. 12.4.1. The run Command
        2. 12.4.2. The launch Command
        3. 12.4.3. The open Command
        4. 12.4.4. The activate Command
        5. 12.4.5. The quit Command
      5. 12.5. The Standard Suite of Application Commands
        1. 12.5.1. The get Command
        2. 12.5.2. The set Command
        3. 12.5.3. The make Command
        4. 12.5.4. The exists Command
        5. 12.5.5. The count Command
        6. 12.5.6. The move Command
        7. 12.5.7. The duplicate Command
        8. 12.5.8. The delete Command
        9. 12.5.9. The print Command
        10. 12.5.10. The save Command
        11. 12.5.11. The close Command
      6. 12.6. More on Working with get, set, and copy Commands
        1. 12.6.1. When Do Implicit get Commands Occur?
        2. 12.6.2. When Are Explicit get Commands Necessary?
        3. 12.6.3. Telling the Difference Between AppleScript and Application set Commands
        4. 12.6.4. Getting and Setting Multiple Values in a Single Statement
        5. 12.6.5. Using copy As an Application Command
      7. 12.7. Changing How Commands Are Handled
        1. 12.7.1. Considering and Ignoring Application Responses
        2. 12.7.2. Considering and Ignoring Other Attributes
        3. 12.7.3. Controlling the Timeout Delay for Long-Running Commands
      8. 12.8. Sending Commands to Remote Applications
        1. 12.8.1. Enabling Remote Apple Events
        2. 12.8.2. Talking to Remote Machines
        3. 12.8.3. Authenticating Access to Remote Machines
        4. 12.8.4. Compiling Scripts Offline
        5. 12.8.5. Using the choose remote application Command
        6. 12.8.6. Targeting Remote Applications by User and Process IDs
        7. 12.8.7. Launching Remote Applications
      9. 12.9. Summary
    10. 13. More on Operators and Coercions
      1. 13.1. Understanding the a reference to Operator
      2. 13.2. Introducing Unit Type Conversions
      3. 13.3. A Summary of AppleScript Coercions
        1. 13.3.1. Coercing Class and Constant Names
        2. 13.3.2. Coercing Numbers and Text
        3. 13.3.3. Coercing File Objects
        4. 13.3.4. Coercing Lists
      4. 13.4. A Summary of AppleScript Operators
        1. 13.4.1. Boolean Logic Operators
        2. 13.4.2. The Concatenation Operator
        3. 13.4.3. Math Operators
        4. 13.4.4. Comparison Operators
        5. 13.4.5. Containment Operators
        6. 13.4.6. The Coercion Operator
        7. 13.4.7. The a reference to Operator
      5. 13.5. Operator Precedence in Detail
        1. 13.5.1. Understanding Precedence
        2. 13.5.2. Understanding Forms of Association
        3. 13.5.3. Understanding when You Should Use Parentheses
      6. 13.6. Summary
    11. 14. Making Decisions Using Conditionals and Loops
      1. 14.1. Choosing Your Course with Conditional Blocks
        1. 14.1.1. Understanding the Basic Conditional Statement
        2. 14.1.2. The Different Flavors of Conditional Statements
        3. 14.1.3. Offering an Alternative Ending with the else Clause
        4. 14.1.4. Offering Multiple Choices with else if Clauses
        5. 14.1.5. Tips for Writing Conditional Statements
          1. 14.1.5.1. Using a Condition in a Single Line
          2. 14.1.5.2. Avoiding Unneeded Conditional Statements
          3. 14.1.5.3. Is It True?
          4. 14.1.5.4. Testing a Value Against Multiple Options
      2. 14.2. Running an Assembly Line with Repeat Loops
        1. 14.2.1. The Different Flavors of Repeat Loops
        2. 14.2.2. Repeating Forever
        3. 14.2.3. Repeating n Times
        4. 14.2.4. Repeating with Numbers in a Range
        5. 14.2.5. Repeating with Items in a List
        6. 14.2.6. Repeating While/Until a Condition Is Met
        7. 14.2.7. Tips for Writing Repeat Statements
          1. 14.2.7.1. Using Loops to Avoid Repetition
          2. 14.2.7.2. Naming Loop Variables
          3. 14.2.7.3. Choosing the Right Loop for the Job
          4. 14.2.7.4. Avoid Looping when Possible
      3. 14.3. Summary
    12. 15. Making Decisions When Dealing with Errors
      1. 15.1. Understanding Compilation Errors
        1. 15.1.1. Simple Typos
        2. 15.1.2. Unbalanced Statements
        3. 15.1.3. Unbalanced Parentheses and Unescaped Quotes
        4. 15.1.4. Unrecognized Keywords
        5. 15.1.5. Reserved Word Mishaps
        6. 15.1.6. Invalid Date Literals
      2. 15.2. Understanding Runtime Errors
        1. 15.2.1. How Runtime Errors Work in AppleScript
        2. 15.2.2. Understanding the Psychology of an Error
        3. 15.2.3. Understanding the Anatomy of an Error
        4. 15.2.4. Trapping Runtime Errors
          1. 15.2.4.1. Using the try Block
          2. 15.2.4.2. Using the Full try Statement
          3. 15.2.4.3. Knowing What to Do in Case of an Error
          4. 15.2.4.4. Putting Error Numbers to Use
            1. 15.2.4.4.1. Testing For Multiple Error Numbers
            2. 15.2.4.4.2. Nesting try Handlers
        5. 15.2.5. Generating Runtime Errors
        6. 15.2.6. Tips for Handling Runtime Errors
          1. 15.2.6.1. Being Careful Not to Trap Too Much
          2. 15.2.6.2. Providing More Detailed Error Messages
          3. 15.2.6.3. Using a Scriptwide try Statement
          4. 15.2.6.4. Logging Errors to a File
      3. 15.3. Understanding Common Error Numbers
        1. 15.3.1. Identifying Operating System Errors
        2. 15.3.2. Identifying Apple Event Errors
        3. 15.3.3. Identifying Application Scripting Errors
        4. 15.3.4. Identifying AppleScript Language Errors
      4. 15.4. Summary
    13. 16. Interacting with the User
      1. 16.1. Creating Basic Dialog Boxes and Gathering Text Input
        1. 16.1.1. Introducing the display dialog Command
          1. 16.1.1.1. From the Dictionary: display dialog
          2. 16.1.1.2. From the Dictionary: dialog reply
          3. 16.1.1.3. Using the Basic Form of the Command
          4. 16.1.1.4. Dealing with the Erroneous Cancel Button
          5. 16.1.1.5. Creating Custom Buttons
          6. 16.1.1.6. Specifying a Default Button
          7. 16.1.1.7. Specifying a Cancel Button
          8. 16.1.1.8. Adding a Title
          9. 16.1.1.9. Showing Icons in Dialog Boxes
          10. 16.1.1.10. Using an Icon File in a Dialog Box
          11. 16.1.1.11. Getting Text Input from the User
          12. 16.1.1.12. Creating Password Dialog Boxes
          13. 16.1.1.13. Dismissing Dialogs Automatically
          14. 16.1.1.14. Validating User-Entered Text
        2. 16.1.2. Introducing the display alert Command
          1. 16.1.2.1. From the Dictionary: display alert
          2. 16.1.2.2. Using the Basic Form of the Command
          3. 16.1.2.3. Using the message Parameter
          4. 16.1.2.4. Using the as Parameter
          5. 16.1.2.5. Setting the Button Behavior
        3. 16.1.3. Introducing the choose from list Command
          1. 16.1.3.1. From the Dictionary: choose from list
          2. 16.1.3.2. Getting the Results of the Command
          3. 16.1.3.3. Using the Basic Command
          4. 16.1.3.4. Creating a Custom Prompt
          5. 16.1.3.5. Adding a Title
          6. 16.1.3.6. Setting the Default Selection
          7. 16.1.3.7. Restricting the Selection
          8. 16.1.3.8. Customizing Buttons
      2. 16.2. Choosing Files, Folders, and Disks
        1. 16.2.1. Introducing the choose file Command
          1. 16.2.1.1. From the Dictionary: choose file
          2. 16.2.1.2. Getting the Results of the Command
          3. 16.2.1.3. Figuring Out When to Use the Command
          4. 16.2.1.4. Using the Basic Command
          5. 16.2.1.5. Creating a Custom Prompt
          6. 16.2.1.6. Restricting to Specific File Types
          7. 16.2.1.7. Setting the Default Location
          8. 16.2.1.8. Picking Invisibles
          9. 16.2.1.9. Allowing Multiple Selections
          10. 16.2.1.10. Showing Package Contents
        2. 16.2.2. Introducing the choose file name Command
          1. 16.2.2.1. From the Dictionary: choose file name
          2. 16.2.2.2. choose file name vs. choose file
          3. 16.2.2.3. Figuring Out When to Use the Command
          4. 16.2.2.4. Getting the Results of the Command
          5. 16.2.2.5. Using the Basic Command
          6. 16.2.2.6. Creating a Custom Prompt
          7. 16.2.2.7. Setting a Default Name and Location
          8. 16.2.2.8. Replacing an Existing File
          9. 16.2.2.9. Seeing an Example in Action
        3. 16.2.3. Introducing the choose folder Command
          1. 16.2.3.1. From the Dictionary: choose folder
          2. 16.2.3.2. Using the Command
          3. 16.2.3.3. Figuring Out When to Use the Command
          4. 16.2.3.4. Setting the Parameters
          5. 16.2.3.5. Choosing a Folder Once
      3. 16.3. Choosing Other Types of Items
        1. 16.3.1. Introducing the choose application Command
          1. 16.3.1.1. From the Dictionary: choose application
          2. 16.3.1.2. Using the Command
        2. 16.3.2. Introducing the choose remote application Command
          1. 16.3.2.1. From the Dictionary: choose remote application
          2. 16.3.2.2. Using the Command
        3. 16.3.3. Introducing the choose URL Command
          1. 16.3.3.1. From the Dictionary: choose URL
          2. 16.3.3.2. Using the Command
        4. 16.3.4. Introducing the choose color Command
          1. 16.3.4.1. From the Dictionary: choose color
          2. 16.3.4.2. Getting the Results of the Command
          3. 16.3.4.3. Figuring Out When to Use the Command
          4. 16.3.4.4. Using the Basic Command and Using It with Parameters
      4. 16.4. Summary
    14. 17. Working with Files
      1. 17.1. Identifying Files, Folders, and Disks
        1. 17.1.1. Using Path Strings
          1. 17.1.1.1. Let's start with an example of an HFS path: Understanding HFS Paths
          2. 17.1.1.2. Understanding POSIX Paths
        2. 17.1.2. Using File System Objects
          1. 17.1.2.1. Understanding Alias Objects
            1. 17.1.2.1.1. How Aliases Work
            2. 17.1.2.1.2. Using Aliases to Keep Track of File System Items
            3. 17.1.2.1.3. Converting to and from Alias Objects
          2. 17.1.2.2. Understanding File "Objects"
            1. 17.1.2.2.1. How Files (Really) Work
            2. 17.1.2.2.2. Converting to and from File References
          3. 17.1.2.3. Understanding POSIX File Objects
            1. 17.1.2.3.1. How POSIX Files Work
            2. 17.1.2.3.2. Converting to and from POSIX File Objects
        3. 17.1.3. File URL Strings
      2. 17.2. Reading and Writing Files
        1. 17.2.1. Commands for Reading and Writing Files
          1. 17.2.1.1. Reading Files
            1. 17.2.1.1.1. From the Dictionary
            2. 17.2.1.1.2. Using the read Command
          2. 17.2.1.2. Opening and Closing Access to Files
            1. 17.2.1.2.1. From the Dictionary
            2. 17.2.1.2.2. Opening a File for Reading or Writing
            3. 17.2.1.2.3. The open for access Result
          3. 17.2.1.3. Read Command Parameters
            1. 17.2.1.3.1. Reading Different Kinds of Data
            2. 17.2.1.3.2. Using a Delimiter to Read Text into a List
            3. 17.2.1.3.3. Reading a Specific Number of Bytes
            4. 17.2.1.3.4. The from and to Parameters
            5. 17.2.1.3.5. The before and until Parameters
            6. 17.2.1.3.6. Using the open for access and read Commands Together
          4. 17.2.1.4. Working with the End-of-File (EOF) Commands
            1. 17.2.1.4.1. From the Dictionary
            2. 17.2.1.4.2. Getting a File's EOF
            3. 17.2.1.4.3. Setting a File's EOF
          5. 17.2.1.5. Writing Files
            1. 17.2.1.5.1. From the Dictionary
            2. 17.2.1.5.2. Using the write Command
            3. 17.2.1.5.3. How Much to Write and Where to Start
            4. 17.2.1.5.4. Useful File Writing Handlers
            5. 17.2.1.5.5. Using the write Command to Create a Script Log
            6. 17.2.1.5.6. Saving and Loading AppleScript Lists and Records
        2. 17.2.2. Dealing with Text File Encodings
          1. 17.2.2.1. About ASCII, MacRoman, and Unicode Character Sets
          2. 17.2.2.2. Reading and Writing Text Files in Different Encodings
            1. 17.2.2.2.1. Reading and Writing UTF-8 Encoded Files
            2. 17.2.2.2.2. Reading and Writing UTF-16 Encoded Files
            3. 17.2.2.2.3. Reading and Writing Files in Other Encodings
          3. 17.2.2.3. What Happens if the Wrong Encoding is Used?
          4. 17.2.2.4. A Summary of Common Text File Encodings
          5. 17.2.2.5. Further Reading on Text Encodings
      3. 17.3. Summary
    15. 18. Organizing Your Code with Handlers
      1. 18.1. What Are User-Defined Handlers?
      2. 18.2. Creating a Simple Handler
      3. 18.3. Using Parameters
      4. 18.4. What's the Result?
        1. 18.4.1. Using the return Statement
        2. 18.4.2. Working with Return Values
        3. 18.4.3. Can a Handler Return More Than One Result?
      5. 18.5. Specifying Handler Parameters
        1. 18.5.1. Using Positional Parameters
          1. 18.5.1.1. Definition for Positional Parameter Handlers
          2. 18.5.1.2. Defining and Calling Positional Parameter Handlers
          3. 18.5.1.3. Adding Parameters
        2. 18.5.2. Using Labeled Parameters
          1. 18.5.2.1. Definition for Positional Parameter Handlers
          2. 18.5.2.2. Using Predefined Parameter Labels
          3. 18.5.2.3. Making Up Your Own Labels
          4. 18.5.2.4. Calling Handlers with Boolean Parameters
      6. 18.6. Introducing Recursion
      7. 18.7. Using the run Handler in Scripts
        1. 18.7.1. I've Never Seen No run Handler!
        2. 18.7.2. When Should You Make the run Handler Explicit?
      8. 18.8. Working with Handlers in Applets
        1. 18.8.1. Using the Standard Event Handlers
          1. 18.8.1.1. The run Event Handler
          2. 18.8.1.2. The open Event Handler
          3. 18.8.1.3. The reopen Event Handler
          4. 18.8.1.4. The quit Event Handler
          5. 18.8.1.5. The idle Event Handler
        2. 18.8.2. Using User-Defined Handlers from Other Scripts
      9. 18.9. Tips for Designing and Using Handlers
        1. 18.9.1. Organizing Code
        2. 18.9.2. Reusing Code
        3. 18.9.3. Thinking Ahead
        4. 18.9.4. Thinking Small
        5. 18.9.5. Reorganizing Existing Code
      10. 18.10. Example Project: A Reusable Associative List
        1. 18.10.1. Designing the Associative List
        2. 18.10.2. Writing the Essential Handlers
        3. 18.10.3. Writing the Extra Handlers
        4. 18.10.4. Revising the Design
        5. 18.10.5. Conclusion
      11. 18.11. Summary
    16. 19. Organizing Your Code with Script Objects
      1. 19.1. Introducing Script Objects
        1. 19.1.1. What Is a Script Library?
        2. 19.1.2. What Is Object-Oriented Programming?
      2. 19.2. Understanding Script Objects
        1. 19.2.1. Defining a Script Object in a Script
        2. 19.2.2. Loading and Storing Script Objects from Disk
        3. 19.2.3. How Variable Scope Works in Script Objects
      3. 19.3. Working with Script Libraries
        1. 19.3.1. What Advantages Do Script Libraries Provide?
        2. 19.3.2. Creating Your First Script Library
        3. 19.3.3. Where Should You Save Your Library?
        4. 19.3.4. What Are the Options for Loading Script Libraries?
          1. 19.3.4.1. Using the load script Command
          2. 19.3.4.2. Using AppleMods Loader
      4. 19.4. Beginning Object-Oriented Programming
        1. 19.4.1. Your First Object: A Better Associative List
          1. 19.4.1.1. Planning the Conversion
          2. 19.4.1.2. Converting the make_associative_list Handler
          3. 19.4.1.3. Repackaging the Remaining Handlers
          4. 19.4.1.4. Using the Object-Oriented Associative List
        2. 19.4.2. Extending Objects Through Inheritance
          1. 19.4.2.1. A Quick Reminder of General Inheritance Concepts
          2. 19.4.2.2. How Inheritance Works with Script Objects
          3. 19.4.2.3. Implementing Case-Insensitive and Case-Sensitive Associative Lists
          4. 19.4.2.4. Testing the New Associative List Objects
        3. 19.4.3. Creating Plug-and-Play Objects
          1. 19.4.3.1. Planning the Logging System
          2. 19.4.3.2. Designing the LogBase Object
          3. 19.4.3.3. Defining the Concrete Log Objects
          4. 19.4.3.4. Testing the Finished Logging System
          5. 19.4.3.5. Conclusion
      5. 19.5. Summary
  9. 3. Putting AppleScript to Work
    1. 20. Scripting the File System
      1. 20.1. How the Finder Represents the Mac OS X File System
        1. 20.1.1. Understanding Containers
        2. 20.1.2. Understanding Files
        3. 20.1.3. Understanding Packages
        4. 20.1.4. Understanding the computer-object Class
      2. 20.2. How System Events Represents the Mac OS X File System
        1. 20.2.1. Understanding Files
        2. 20.2.2. Understanding Folders
        3. 20.2.3. Understanding Disks
        4. 20.2.4. Understanding System Events' alias Class
        5. 20.2.5. Other Classes of Interest
          1. 20.2.5.1. The user Class
          2. 20.2.5.2. The domain Class
          3. 20.2.5.3. The login item Class
      3. 20.3. Working with Finder Items
        1. 20.3.1. Opening and Closing Items
        2. 20.3.2. Duplicating Files and Folders
        3. 20.3.3. Deleting Files and Folders
        4. 20.3.4. Moving Files and Folders
        5. 20.3.5. Checking if Items Exist
        6. 20.3.6. Making New Items
        7. 20.3.7. Sorting Lists of Finder References
        8. 20.3.8. Ejecting Disks
        9. 20.3.9. Selecting Items in a Finder Window
        10. 20.3.10. Converting Finder References to AppleScript Aliases
      4. 20.4. More on Working with Folders
        1. 20.4.1. Filtering the Content of Folders
        2. 20.4.2. Getting the Entire Contents of a Folder
      5. 20.5. Locating Important Files and Folders with Standard Additions
        1. 20.5.1. Avoiding Hardcoded Paths to the Startup Disk and Home Folder
        2. 20.5.2. Getting the Path to the Currently Running Script
        3. 20.5.3. Getting Paths to Resources in an Application Bundle
      6. 20.6. Mounting Volumes
      7. 20.7. Summary
    2. 21. Scripting Apple Applications
      1. 21.1. Scripting iTunes
        1. 21.1.1. Understanding the iTunes Object Model
        2. 21.1.2. Working with Sources
        3. 21.1.3. Working with Tracks and Playlists
        4. 21.1.4. Example Project: My Themed Playlist
          1. 21.1.4.1. Gathering the User Input
          2. 21.1.4.2. Preparing the Playlist
          3. 21.1.4.3. Adding the Tracks to the Playlist
      2. 21.2. Scripting Mail
        1. 21.2.1. Working with Mailboxes
        2. 21.2.2. Working with Messages
        3. 21.2.3. Creating Outgoing Messages
        4. 21.2.4. Working with Mail Rules
        5. 21.2.5. Example Project: Building a Monthly Message Archive
          1. 21.2.5.1. Preparing to Write the Script
          2. 21.2.5.2. Creating the Sub-Mailboxes
          3. 21.2.5.3. Putting It All Together
      3. 21.3. Scripting iCal
        1. 21.3.1. Working with Calendars
        2. 21.3.2. Working with Events
      4. 21.4. Scripting Address Book
        1. 21.4.1. Working with People
        2. 21.4.2. Working with Groups
        3. 21.4.3. Working with Rollovers
        4. 21.4.4. Example Project: Looking Up Contact Information by Name
      5. 21.5. Scripting Automator
        1. 21.5.1. Using the Run AppleScript Action
          1. 21.5.1.1. Understanding the Action's Input and Output
          2. 21.5.1.2. Understanding the Action's Parameters
          3. 21.5.1.3. Filtering Files with Run AppleScript
        2. 21.5.2. Scripting the Automator Application
      6. 21.6. Summary
    3. 22. Extending AppleScript with Scripting Additions
      1. 22.1. Introducing Scripting Additions and Scriptable Faceless Background Applications
      2. 22.2. Scripting Additions in Detail
        1. 22.2.1. Scripting Additions and the AppleScript Language
        2. 22.2.2. Installing Scripting Additions
        3. 22.2.3. Missing Additions and Garbled Scripts
        4. 22.2.4. Distributing Scripting Additions with Your Scripts
          1. 22.2.4.1. Embedding Scripting Additions Within a Script Application
          2. 22.2.4.2. Using the Mac OS X Installer to Install Your Script and Scripting Additions
        5. 22.2.5. Understanding Scripting Addition Compatibility Issues
          1. 22.2.5.1. Caution: Mac OS 9 Scripting Additions Don't Work on Mac OS X
          2. 22.2.5.2. Using PowerPC-Only Scripting Additions on Intel-Based Macs
          3. 22.2.5.3. Using 32-Bit-Only Scripting Additions on 64-Bit Macs
          4. 22.2.5.4. Working Around Scripting Addition Limitations
      3. 22.3. Understanding Scriptable Faceless Background Applications
        1. 22.3.1. Installing Scriptable FBAs
        2. 22.3.2. Distributing Scriptable FBAs with Your Scripts
      4. 22.4. Examples of AppleScript Extensions
        1. 22.4.1. Built-in Extensions
        2. 22.4.2. Third-Party Extensions
      5. 22.5. Using the Standard Additions Scripting Addition
        1. 22.5.1. Providing Audio Feedback
          1. 22.5.1.1. The beep Command
          2. 22.5.1.2. The say Command
        2. 22.5.2. Pausing Your Scripts
          1. 22.5.2.1. The delay Command
        3. 22.5.3. Getting and Setting the System Volume
          1. 22.5.3.1. The get volume settings Command
          2. 22.5.3.2. The set volume Command
        4. 22.5.4. Getting System Information
          1. 22.5.4.1. The system attribute Command
          2. 22.5.4.2. The system info Command
        5. 22.5.5. Working with URLs
          1. 22.5.5.1. The open location Command
          2. 22.5.5.2. Coercing URLs
      6. 22.6. Using the Image Events Faceless Background Application
      7. 22.7. Processing Text with the Satimage Scripting Addition
        1. 22.7.1. Downloading and Installing the Satimage Scripting Addition
        2. 22.7.2. Performing Simple Find and Replace Tasks
        3. 22.7.3. Counting Occurrences of a Substring in a String
        4. 22.7.4. Finding and Replacing Substrings in a File
        5. 22.7.5. Searching and Replacing Across Many Files
        6. 22.7.6. Finding and Replacing Text with Regular Expressions
          1. 22.7.6.1. A Brief Background on Regular Expressions
          2. 22.7.6.2. Using Satimage Regular Expression Commands
        7. 22.7.7. Batch-Changing File Names
      8. 22.8. Summary
    4. 23. AppleScript Amenities
      1. 23.1. Using the Services Menu
        1. 23.1.1. AppleScript Editor Services
        2. 23.1.2. Creating Your Own Services
      2. 23.2. Scheduling Scripts with iCal
      3. 23.3. Triggering Scripts with Folder Actions
        1. 23.3.1. Defining Event Handlers for Folder Actions
        2. 23.3.2. Where Are Folder Action Scripts Stored?
        3. 23.3.3. Managing Your Folder Actions
          1. 23.3.3.1. Using the Folder Actions Setup Utility
          2. 23.3.3.2. Using Scripts
        4. 23.3.4. Creating Your First Folder Action
          1. 23.3.4.1. Create Your Folder Action Script
          2. 23.3.4.2. Save Your Script
          3. 23.3.4.3. Create Your Hot Folder
          4. 23.3.4.4. Activate the Folder Actions Setup Utility
          5. 23.3.4.5. Attach the Folder Action Script to the Folder
          6. 23.3.4.6. Test Your Folder Action
      4. 23.4. Controlling Applications with GUI Scripting
        1. 23.4.1. Enabling GUI Scripting
        2. 23.4.2. The GUI Scripting Dictionary
        3. 23.4.3. GUI Scripting Object Structure
        4. 23.4.4. Basic GUI Scripting Examples
        5. 23.4.5. Using GUI Scripting Commands
          1. 23.4.5.1. The click Command
          2. 23.4.5.2. The keystroke Command
          3. 23.4.5.3. The key code Command
        6. 23.4.6. Example Project: Searching in Preview
          1. 23.4.6.1. Introducing PreFab UI Browser
          2. 23.4.6.2. Beginning the Script
          3. 23.4.6.3. Performing the Search
          4. 23.4.6.4. Getting Data from the Search Table
        7. 23.4.7. Understanding the Limitations of GUI Scripting
      5. 23.5. Working with the Clipboard
        1. 23.5.1. Getting Clipboard Data into AppleScript
          1. 23.5.1.1. Understanding How the Mac Clipboard Works
          2. 23.5.1.2. Finding Out What's on the Clipboard
          3. 23.5.1.3. Getting Specific Types of Data from the Clipboard
        2. 23.5.2. Setting the Clipboard Data
          1. 23.5.2.1. Using the cut, copy, and paste Commands in Applications
          2. 23.5.2.2. Using the set the clipboard to Command
          3. 23.5.2.3. Using GUI Scripting
        3. 23.5.3. Example Project: Creating PDF Cuttings from Selections in Preview
      6. 23.6. Summary
    5. 24. Scripting iWork and Office
      1. 24.1. Scripting iWork
        1. 24.1.1. Scripting Pages
          1. 24.1.1.1. Creating a New Document
          2. 24.1.1.2. Working with a Document's Paragraphs
          3. 24.1.1.3. Adding Headers and Footers to a Document
          4. 24.1.1.4. Drawing Shapes in Pages
          5. 24.1.1.5. Adding Tables and Charts
          6. 24.1.1.6. Tips for Scripting Pages
        2. 24.1.2. Scripting Keynote
          1. 24.1.2.1. Creating a Slideshow
          2. 24.1.2.2. Choosing a Theme
          3. 24.1.2.3. Filling Slides with Content
          4. 24.1.2.4. Working with Transitions
          5. 24.1.2.5. Scripting an Advanced Screen Reader
          6. 24.1.2.6. Tips for Scripting Keynote
        3. 24.1.3. Scripting Numbers
          1. 24.1.3.1. Creating Sheets and Tables
          2. 24.1.3.2. Working with Cells
      2. 24.2. Scripting Microsoft Office
        1. 24.2.1. Working with Microsoft Entourage
          1. 24.2.1.1. Disable the Security Warning
          2. 24.2.1.2. Creating Messages
          3. 24.2.1.3. Some Shortcuts
          4. 24.2.1.4. Working with Contacts
            1. 24.2.1.4.1. Creating Contacts
            2. 24.2.1.4.2. Selecting and Changing Multiple Contacts
            3. 24.2.1.4.3. Setting Categories
            4. 24.2.1.4.4. Creating Calendar Events
            5. 24.2.1.4.5. Extracting Information from a Whole Lot of Messages in a Folder
        2. 24.2.2. Working with Excel
          1. 24.2.2.1. Creating Workbooks and Worksheets
          2. 24.2.2.2. Working with Ranges
          3. 24.2.2.3. Creating Charts
        3. 24.2.3. Working with Word
          1. 24.2.3.1. Creating a Word Document
          2. 24.2.3.2. Working with Selected Text
        4. 24.2.4. Where Next?
      3. 24.3. Summary
    6. 25. Scripting Data and Databases
      1. 25.1. Automating FileMaker Pro with AppleScript
        1. 25.1.1. The FileMaker Object Model and the Commands You Can Use
          1. 25.1.1.1. The database Class
          2. 25.1.1.2. The window Class
          3. 25.1.1.3. The table Class
          4. 25.1.1.4. The layout Class
          5. 25.1.1.5. The record, field, and cell Classes
        2. 25.1.2. Finding Data Quickly with the whose Clause
          1. 25.1.2.1. Using the whose Clause to Retrieve Relational Data
          2. 25.1.2.2. Using the find Command
        3. 25.1.3. Running AppleScripts from Inside FileMaker Pro
      2. 25.2. Scripting Runtime Labs' MacSQL
        1. 25.2.1. Getting Connected
        2. 25.2.2. Using a Simple select Command
        3. 25.2.3. More on Result Sets
        4. 25.2.4. Some Useful Handlers
      3. 25.3. Database Events
        1. 25.3.1. Database Events Classes and Commands
        2. 25.3.2. Database Events Examples
      4. 25.4. Summary
    7. 26. Scripting Adobe InDesign
      1. 26.1. Learning by Example
      2. 26.2. Understanding the InDesign Object Model
        1. 26.2.1. How InDesign Organizes Its Many Properties
        2. 26.2.2. How InDesign Manages Units of Measurement
        3. 26.2.3. How to Set Properties Safely when Making New Objects
      3. 26.3. Working with Documents
        1. 26.3.1. Working with Document Layers
        2. 26.3.2. Understanding InDesign References
      4. 26.4. Working with Text
        1. 26.4.1. Working with Text Stories
        2. 26.4.2. Working with References to Text
      5. 26.5. Working with Graphics
      6. 26.6. Drawing Lines
      7. 26.7. Example Project: An InDesign Scripts Launcher
      8. 26.8. Working with Tables
        1. 26.8.1. Importing Table Text from a Spreadsheet
        2. 26.8.2. Copying Formatting Between Cells
      9. 26.9. Working with Selections
        1. 26.9.1. Example Project: Captioning a Selected Graphic
        2. 26.9.2. Example Project: Cleaning Up Selected Text
      10. 26.10. Labeling Objects for Use in Workflow Scripts
      11. 26.11. Taking Advantage of AppleScript-Specific Preferences
      12. 26.12. Example Project: Replacing Tags with Images
        1. 26.12.1. Preparing InDesign
        2. 26.12.2. Processing the Tags
        3. 26.12.3. Preparing the Image Files
        4. 26.12.4. Placing the Graphics
        5. 26.12.5. Testing the Script
      13. 26.13. Using InDesign on a Server
      14. 26.14. Summary
    8. 27. Interacting with the Unix Command Line
      1. 27.1. Why and When Are Shell Scripts Used in AppleScript?
      2. 27.2. Understanding Unix Scripting Concepts
        1. 27.2.1. The Unix Command Line and Mac Desktop Compared
        2. 27.2.2. How the Unix Command Line Works
          1. 27.2.2.1. The Shell
          2. 27.2.2.2. Commands
          3. 27.2.2.3. Pipes
          4. 27.2.2.4. Other Redirections
        3. 27.2.3. Getting Help
          1. 27.2.3.1. Built-in Help
          2. 27.2.3.2. Other Sources of Help
        4. 27.2.4. Things to Watch Out For
          1. 27.2.4.1. Data in Unix Is "Dumb"
          2. 27.2.4.2. Unix Doesn't Know About Different Character Sets
          3. 27.2.4.3. Unix Understands POSIX File Paths Only
          4. 27.2.4.4. Watch Where You Use Spaces and Other Special Characters!
          5. 27.2.4.5. Unix Assumes You Know What You're Doing
        5. 27.2.5. Some Useful Unix Commands
      3. 27.3. Running Shell Scripts from AppleScript
        1. 27.3.1. Scripting the Terminal Application
          1. 27.3.1.1. Understanding Terminal's Object Model
          2. 27.3.1.2. Using the do script Command
          3. 27.3.1.3. A Simple do script Example
          4. 27.3.1.4. Limitations of the do script Command
        2. 27.3.2. Using the do shell script Command
          1. 27.3.2.1. From the Dictionary
          2. 27.3.2.2. Understanding the do shell script Command's Parameters
            1. 27.3.2.2.1. The Direct Parameter
            2. 27.3.2.2.2. The as Parameter
            3. 27.3.2.2.3. The administrator privileges Parameter
            4. 27.3.2.2.4. The user name and password Parameters
            5. 27.3.2.2.5. The altering line endings Parameter
          3. 27.3.2.3. How do shell script Deals with Unicode
          4. 27.3.2.4. A Simple do shell script Example
          5. 27.3.2.5. Limitations of the do shell script Command
            1. 27.3.2.5.1. No User Interaction
            2. 27.3.2.5.2. Performance Overheads
            3. 27.3.2.5.3. Can't Directly Supply Data to Standard Input
        3. 27.3.3. Assembling Shell Script Strings
          1. 27.3.3.1. Unix File Paths
          2. 27.3.3.2. Quoting Arguments
          3. 27.3.3.3. Absolute Paths, Relative Paths, and the Working Directory
        4. 27.3.4. Passing Data to Standard Input
          1. 27.3.4.1. Using echo
          2. 27.3.4.2. Using Temporary Files
            1. 27.3.4.2.1. Creating the Temporary File
            2. 27.3.4.2.2. Writing the Temporary File
            3. 27.3.4.2.3. Redirecting the Shell Script
      4. 27.4. Running AppleScript from Shell Scripts
        1. 27.4.1. Using the osascript Command
        2. 27.4.2. Passing Parameters to the Script
        3. 27.4.3. Other Related Commands
      5. 27.5. Example Projects
        1. 27.5.1. Simple Text Processing Handlers
          1. 27.5.1.1. Encoding URLs
          2. 27.5.1.2. Changing Case
        2. 27.5.2. Changing File System Permissions
        3. 27.5.3. Analyzing PDF Files
      6. 27.6. Summary
    9. 28. Using Smile: The AppleScript Integrated Production Environment
      1. 28.1. Introducing Smile
        1. 28.1.1. Introducing Smile's Integrated Engine Architecture
        2. 28.1.2. About Smile's Terminal
        3. 28.1.3. Why Should You Use Smile?
      2. 28.2. Downloading Smile
      3. 28.3. Using Smile
        1. 28.3.1. Using the AppleScript Command-Line Environment
        2. 28.3.2. Using Smile's Custom Dialog Boxes
          1. 28.3.2.1. Creating the Interface
          2. 28.3.2.2. Programming the Functionality
        3. 28.3.3. Using Regular Expressions
        4. 28.3.4. Using the Graphic Engine
        5. 28.3.5. Using SmileLab
      4. 28.4. Example Project: Adding Text to a PDF Document
        1. 28.4.1. Preparing the PDF
        2. 28.4.2. Rolling Up the Scripts into a Graphical Interface
          1. 28.4.2.1. Building the Interface
          2. 28.4.2.2. Programming the Dialog Box
          3. 28.4.2.3. Handling User Clicks
        3. 28.4.3. Further Exercises
      5. 28.5. Summary
    10. 29. Tips and Techniques for Improving Your Scripts
      1. 29.1. Design, in a Nutshell
        1. 29.1.1. Taking Your Scripting Skills to the Next Level
        2. 29.1.2. How Much Design Do Your Scripts Need?
        3. 29.1.3. Starting Out Is Hard (But Gets Easier with Practice)
        4. 29.1.4. How the Design-Driven Approach Changes the Way You Think About Code
      2. 29.2. Testing and Debugging Your Code
        1. 29.2.1. General Testing Tips and Techniques
          1. 29.2.1.1. Design Your Scripts for Easy Testing
          2. 29.2.1.2. Test Your Code with Bad Data As Well As Good
          3. 29.2.1.3. Test Your Code As You Go
          4. 29.2.1.4. Why Testing Matters
        2. 29.2.2. Debugging in AppleScript Editor
          1. 29.2.2.1. Viewing Intermediate Results with return and error Statements
          2. 29.2.2.2. Monitoring a Script's Progress Using Dialog Boxes
          3. 29.2.2.3. Using the Event Log
        3. 29.2.3. Debugging in Script Debugger
          1. 29.2.3.1. Using the start log and stop log Commands
          2. 29.2.3.2. Using the Script Window
          3. 29.2.3.3. Using the Debugging Mode
          4. 29.2.3.4. Stepping Through Scripts
        4. 29.2.4. Keeping Track of Changes
          1. 29.2.4.1. Comparing Scripts with FileMerge
          2. 29.2.4.2. Managing Your Code with Source Control
      3. 29.3. Improving the Performance of Your Code
        1. 29.3.1. The Dos and Don'ts of Optimization
          1. 29.3.1.1. When Should You Optimize?
          2. 29.3.1.2. Where Should You Optimize?
          3. 29.3.1.3. Profiling Your Code with Timing Commands
        2. 29.3.2. Common AppleScript Optimizations
          1. 29.3.2.1. Building Up Large Lists
          2. 29.3.2.2. Building Up Large Strings
          3. 29.3.2.3. Manipulating Multiple Application Objects with a Single Command
          4. 29.3.2.4. Filtering Multiple Application Objects with whose Clause References
        3. 29.3.3. Advanced Optimization Techniques
          1. 29.3.3.1. Assessing Your Code's Efficiency with Big O Notation
          2. 29.3.3.2. Improving the Efficiency of AppleScript Lists
          3. 29.3.3.3. Linear Search vs. Binary Search
          4. 29.3.3.4. Bubble Sort vs. Quicksort
      4. 29.4. Summary
    11. 30. Creating Cocoa Applications with AppleScriptObjC
      1. 30.1. Example Project: HelloAppleScriptObjC
        1. 30.1.1. Creating the Project in Xcode
        2. 30.1.2. Adding the AppleScript Code in Xcode
        3. 30.1.3. Constructing the Interface in Interface Builder
          1. 30.1.3.1. Understanding Interface Builder's Interface
            1. 30.1.3.1.1. The MainMenu Window
            2. 30.1.3.1.2. The Application Window
            3. 30.1.3.1.3. The MainMenu.xib – English Window
            4. 30.1.3.1.4. The Library Palette
          2. 30.1.3.2. Adding the GUI Objects
          3. 30.1.3.3. Making Connections
        4. 30.1.4. Building and Testing the Application
      2. 30.2. Understanding How Cocoa Works
        1. 30.2.1. Understanding Outlets
        2. 30.2.2. Understanding Targets and Actions
        3. 30.2.3. How AppleScript Interacts with Cocoa Objects
      3. 30.3. Modifying HelloAppleScriptObjC to Set the Text Field
        1. 30.3.1. Updating the Code and Testing the Changes
        2. 30.3.2. What Is All That Code in the Default AppleScript File?
      4. 30.4. Example Project: PlaySound
        1. 30.4.1. Creating the Project and Adding the Code
        2. 30.4.2. Constructing the Interface
        3. 30.4.3. Building and Testing the Application
        4. 30.4.4. Understanding How It Works
      5. 30.5. Viewing the Cocoa Documentation in Xcode
        1. 30.5.1. How Cocoa Classes Are Documented
        2. 30.5.2. How Cocoa Methods Are Documented
          1. 30.5.2.1. The Method Name
          2. 30.5.2.2. Parameters
          3. 30.5.2.3. Return Value
          4. 30.5.2.4. Discussion, Availability, See Also, Related Sample Code, and Declared In
      6. 30.6. Example Project: SnapShot
        1. 30.6.1. Creating the Project and Starting the Code
        2. 30.6.2. Constructing the Interface
        3. 30.6.3. Completing the Code
        4. 30.6.4. Understanding How It Works
        5. 30.6.5. Preparing the Text Fields Using an awakeFromNib Handler
        6. 30.6.6. Building and Testing the Application
      7. 30.7. Modifying SnapShot to Use Cocoa Bindings
        1. 30.7.1. Adjusting the Code for Bindings
        2. 30.7.2. Setting Up Bindings in Interface Builder
        3. 30.7.3. Building and Testing the Application
      8. 30.8. Modifying PlaySound to Use Bundled Sound Files
        1. 30.8.1. Adding the Sound File Resources to the Project
        2. 30.8.2. Adding the Code
        3. 30.8.3. Adding the Pop-Up Button
        4. 30.8.4. Building and Testing the Application
        5. 30.8.5. Understanding How It Works
      9. 30.9. Example Project: KitchenTimer
        1. 30.9.1. Creating the Project and Starting the Code
        2. 30.9.2. Constructing the Interface
        3. 30.9.3. Completing the Code
      10. 30.10. Extra Exercises
      11. 30.11. Where Next?
      12. 30.12. Summary
      13. 30.13. Conclusion