Cover image for AppleScript: The Missing Manual

Book description

From newspapers to NASA, Mac users around the world use AppleScript to automate their daily computing routines. Famed for its similarity to English and its ease of integration with other programs, AppleScript is the perfect programming language for time-squeezed Mac fans. As beginners quickly realize, however, AppleScript has one major shortcoming: it comes without a manual. No more. You don't need a degree in computer science, a fancy system administrator title, or even a pocket protector and pair of nerdy glasses to learn the Mac's most popular scripting language; you just need the proper guide at your side. AppleScript: The Missing Manual is that guide. Brilliantly compiled by author Adam Goldstein, AppleScript: The Missing Manual is brimming with useful examples. You'll learn how to clean up your Desktop with a single click, for example, and how to automatically optimize pictures for a website. Along the way, you ll learn the overall grammar of AppleScript, so you can write your own customized scripts when you feel the need. Naturally, AppleScript: The Missing Manual isn't merely for the uninitiated scripter. While its hands-on approach certainly keeps novices from feeling intimidated, this comprehensive guide is also suited for system administrators, web and graphics professionals, musicians, scientists, mathematicians, engineers, and others who need to learn the ins and outs of AppleScript for their daily work. Thanks to AppleScript: The Missing Manual, the path from consumer to seasoned script has never been clearer. Now you, too, can automate your Macintosh in no time.

Table of Contents

  1. AppleScript: The Missing Manual
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. The Missing Credits
      1. About the Author
      2. About the Creative Team
      3. Acknowledgments
      4. The Missing Manual Series
    4. Foreword
    5. Introduction
      1. Uses for AppleScript
      2. Advantages and Disadvantages
      3. About This Book
        1. What You'll Need
        2. About the Outline
        3. About → These → Arrows
        4. About MissingManuals.com
        5. The Very Basics
    6. I. AppleScript Overview
      1. 1. Setting Up AppleScript
        1. 1.1. The Script Menu
          1. 1.1.1. Address Book Scripts
          2. 1.1.2. Basics
          3. 1.1.3. ColorSync
          4. 1.1.4. Finder Scripts
          5. 1.1.5. Folder Actions
          6. 1.1.6. FontSync Scripts
          7. 1.1.7. Info Scripts
          8. 1.1.8. Internet Scripts
          9. 1.1.9. Mail Scripts
          10. 1.1.10. Navigation Scripts
          11. 1.1.11. Printing Scripts
          12. 1.1.12. Script Editor Scripts
          13. 1.1.13. Sherlock Scripts
          14. 1.1.14. UI Element Scripts
          15. 1.1.15. URLs
          16. 1.1.16. Customizing the Script Menu
            1. 1.1.16.1. Adding new scripts
            2. 1.1.16.2. Rearranging submenus
        2. 1.2. Working with the Scripts You Have
          1. 1.2.1. Opening a Script
          2. 1.2.2. Analyzing How It Works
          3. 1.2.3. Changing What It Does
      2. 2. Using Script Editor
        1. 2.1. The Script Editor Look
          1. 2.1.1. The Toolbar
            1. 2.1.1.1. Record
            2. 2.1.1.2. Stop
            3. 2.1.1.3. Run
            4. 2.1.1.4. Compile
            5. 2.1.1.5. Customizing the toolbar
          2. 2.1.2. The Script Field
          3. 2.1.3. The Info Field
            1. 2.1.3.1. Description
            2. 2.1.3.2. Result
            3. 2.1.3.3. Event Log
        2. 2.2. Script Formats
          1. 2.2.1. Script Files
          2. 2.2.2. Applications
          3. 2.2.3. Plain Text Documents
        3. 2.3. Setting Script Editor's Preferences
          1. 2.3.1. General
          2. 2.3.2. Editing
          3. 2.3.3. Formatting
          4. 2.3.4. History
          5. 2.3.5. Plug-ins
      3. 3. Building a Script from Scratch
        1. 3.1. Getting Started
          1. 3.1.1. Displaying a Simple Dialog Box
          2. 3.1.2. Displaying Numbers
          3. 3.1.3. More than One Command
        2. 3.2. Commanding Other Programs
          1. 3.2.1. Using tell Statements
          2. 3.2.2. Opening a Dictionary
          3. 3.2.3. Looking for the Commands You Want
            1. 3.2.3.1. Checking whether the folder is already there
            2. 3.2.3.2. Displaying the other dialog box
          4. 3.2.4. Displaying a Window in the Finder
    7. II. Everyday Scripting Tasks
      1. 4. Manipulating Text
        1. 4.1. String Notation
        2. 4.2. Getting Text Back from Dialog Boxes
        3. 4.3. Linking Strings Together
        4. 4.4. Multiline Strings
        5. 4.5. Scripting TextEdit
          1. 4.5.1. Create a New TextEdit Document
          2. 4.5.2. Adding Text to a New Document
          3. 4.5.3. Adding Formatting
            1. 4.5.3.1. Inserting a new line after the date
            2. 4.5.3.2. Changing the font and size
        6. 4.6. Adding Word Count
          1. 4.6.1. Employing the Text Suite
        7. 4.7. Commanding Microsoft Word
          1. 4.7.1. Adapting a TextEdit Script
          2. 4.7.2. Shrinking Documents by a Page
          3. 4.7.3. Repeating the Shrinking
          4. 4.7.4. Shrinking a Variable Number of Times
        8. 4.8. Running Scripts from Text
          1. 4.8.1. Get Result of AppleScript
          2. 4.8.2. Make New AppleScript
          3. 4.8.3. Run as AppleScript
      2. 5. Controlling Files
        1. 5.1. File Path Boot Camp
        2. 5.2. Displaying Folders
          1. 5.2.1. Opening a Folder with AppleScript (Reprise)
          2. 5.2.2. Changing a Finder Window's View
          3. 5.2.3. More Finder Window Settings
          4. 5.2.4. Working with More than One Window
          5. 5.2.5. Subroutines
            1. 5.2.5.1. Defining a subroutine
            2. 5.2.5.2. Running a subroutine
            3. 5.2.5.3. Variables in subroutines
            4. 5.2.5.4. Writing the appropriate subroutine
        3. 5.3. Moving Files Around
          1. 5.3.1. Transferring Items from One Folder to Another
            1. 5.3.1.1. Creating the destination folder
            2. 5.3.1.2. Eliminating the "already an item with that name" error
            3. 5.3.1.3. Moving the files and folders
        4. 5.4. Backing Up Files
          1. 5.4.1. The duplicate Command
        5. 5.5. Deleting Files
          1. 5.5.1. An Example: Clearing Out Safari's Icon Cache
        6. 5.6. Picking a File from a Dialog Box
          1. 5.6.1. Showing When a File was Created
        7. 5.7. Saving Files
          1. 5.7.1. An Example: Saving in TextEdit
          2. 5.7.2. Forgoing the Dialog Box
          3. 5.7.3. Saving All Documents at Once
      3. 6. Creating Lists
        1. 6.1. Common List Commands
        2. 6.2. Displaying Lists
          1. 6.2.1. Getting the Selected Item
        3. 6.3. The Ever-Useful every Keyword
          1. 6.3.1. An Example: Displaying a List of Running Programs
        4. 6.4. List Processing
          1. 6.4.1. Batch Renaming
        5. 6.5. Joining Lists Together
          1. 6.5.1. Merging File Lists
        6. 6.6. Inputting Lists
          1. 6.6.1. Adding Items One at a Time
          2. 6.6.2. Adding Multiple Items at Once
        7. 6.7. Getting Lists from Other Programs
          1. 6.7.1. TextEdit
            1. 6.7.1.1. Listing documents
          2. 6.7.2. Microsoft Word
      4. 7. Organizing and Editing Graphics
        1. 7.1. Scripting iPhoto
          1. 7.1.1. Accessing Pictures by Certain Criteria
          2. 7.1.2. Getting Random Pictures
            1. 7.1.2.1. Getting some item
          3. 7.1.3. Showing Slideshows
        2. 7.2. Controlling Photoshop
          1. 7.2.1. Recording Actions
          2. 7.2.2. Wrapping Actions in an AppleScript
          3. 7.2.3. Other Photoshop Commands
          4. 7.2.4. Filtering Images
            1. 7.2.4.1. An example: Making an image look surreal
            2. 7.2.4.2. Mixing your two Photoshop scripts
        3. 7.3. Image Events
          1. 7.3.1. Getting Image Dimensions
          2. 7.3.2. Padding an Image
          3. 7.3.3. Creating a Droplet
            1. 7.3.3.1. Adapting your script
          4. 7.3.4. Converting Images from One Format to Another
      5. 8. Playing Sound and Video
        1. 8.1. Scripting iTunes
          1. 8.1.1. Playing Tracks
          2. 8.1.2. Rating Songs
          3. 8.1.3. Skipping Tracks
          4. 8.1.4. Converting Song Files
        2. 8.2. Speaking and Listening
          1. 8.2.1. Speaking from a Script
            1. 8.2.1.1. Linking your script to a keyboard shortcut
          2. 8.2.2. Making a File Speak
            1. 8.2.2.1. Converting Clipboard text into audio
            2. 8.2.2.2. Reading multiple emails into a file
          3. 8.2.3. Speaking to a Script
        3. 8.3. Scripting QuickTime
          1. 8.3.1. Getting Started
          2. 8.3.2. Presenting a Movie Full-Screen
          3. 8.3.3. Including a Movie with Your Script
          4. 8.3.4. Rotating a Movie
      6. 9. Internet and Network Scripting
        1. 9.1. Internet Connect
          1. 9.1.1. Dialing Up to the Internet
          2. 9.1.2. Finding AirPort Signal Strength
        2. 9.2. Safari
          1. 9.2.1. Opening Web Sites
            1. 9.2.1.1. Opening Web sites within the same window
          2. 9.2.2. Viewing a Site's Code
          3. 9.2.3. Viewing a Site Without Formatting
          4. 9.2.4. Running AppleScripts from Safari
            1. 9.2.4.1. Scripts that appear on Web pages
            2. 9.2.4.2. Mini-scripts in the Address bar
            3. 9.2.4.3. Saved scripts in the Favorites bar
        3. 9.3. Address Book
          1. 9.3.1. Finding Contacts
          2. 9.3.2. Adding Contacts to a Group
        4. 9.4. Mail
          1. 9.4.1. Checking for New Messages
          2. 9.4.2. Mailboxes, Etc.
            1. 9.4.2.1. Finding mailboxes with unread messages
        5. 9.5. iChat Control
          1. 9.5.1. Notifying iChat when the Screensaver's On
          2. 9.5.2. Displaying a Random Status Message
        6. 9.6. URL Access Scripting
          1. 9.6.1. Downloading a File Quickly
          2. 9.6.2. Uploading a File
        7. 9.7. Recalling Passwords
      7. 10. Organizing Information in Databases
        1. 10.1. Record Notation
        2. 10.2. Making a Simple AppleScript Database
          1. 10.2.1. Searching an AppleScript Database
        3. 10.3. Getting File Information
          1. 10.3.1. Useful Properties
        4. 10.4. Scripting FileMaker Pro
          1. 10.4.1. Creating a New FileMaker Database
          2. 10.4.2. Entering Information into a FileMaker Database
            1. 10.4.2.1. Enter your information manually
            2. 10.4.2.2. Use an AppleScript to enter your information
          3. 10.4.3. Sorting FileMaker Records
    8. III. Power-User Features
      1. 11. Linking Scripts to Folders with Folder Actions
        1. 11.1. Enabling Folder Actions
        2. 11.2. Built-in Actions
          1. 11.2.1. Attaching a Folder Action
          2. 11.2.2. The Included Folder Action Scripts
          3. 11.2.3. Viewing and Changing an Action's Code
            1. 11.2.3.1. An example: shortening button names in a folder action's dialog box
          4. 11.2.4. Removing an Action
            1. 11.2.4.1. Removing all actions
        3. 11.3. Running Your Own Actions
          1. 11.3.1. When a Folder Opens
          2. 11.3.2. When You Add Files to a Folder
            1. 11.3.2.1. An example: Sending files to the Trash can
            2. 11.3.2.2. Moving old desktop files
      2. 12. Scripting Programs That Don't Have Dictionaries
        1. 12.1. Enabling GUI Scripting
        2. 12.2. GUI Scripting Basics
        3. 12.3. Controlling Menus
        4. 12.4. Clicking Buttons
          1. 12.4.1. Clicking Tab Buttons
        5. 12.5. Deciphering Interface Hierarchies
        6. 12.6. Fake Typing
          1. 12.6.1. Zooming In and Out
      3. 13. Mixing AppleScript and Unix
        1. 13.1. Terminal
          1. 13.1.1. Changing System Settings
          2. 13.1.2. Unix Text Editing
        2. 13.2. Unix Without Terminal
          1. 13.2.1. Getting Program Results Back
        3. 13.3. Running Superuser Commands
        4. 13.4. Running AppleScripts from Unix
        5. 13.5. Scheduling AppleScript Commands
      4. 14. Testing and Debugging Scripts
        1. 14.1. First Line of Defense: The Compiler
        2. 14.2. Noting Important Events
          1. 14.2.1. The Event Log
          2. 14.2.2. The Event Log History
            1. 14.2.2.1. Navigating logs
            2. 14.2.2.2. The toolbar
        3. 14.3. Preventing Errors
        4. 14.4. Isolating and Handling Errors
          1. 14.4.1. Using try
            1. 14.4.1.1. Multiple commands in a try statement
          2. 14.4.2. Discovering Errors
        5. 14.5. The Xcode Debugger
          1. 14.5.1. Obtaining and Installing the Developer Tools
          2. 14.5.2. Creating a New Project
          3. 14.5.3. Changing the Script
          4. 14.5.4. Stepping Through the Code
      5. 15. AppleScript Studio
        1. 15.1. What Is AppleScript Studio?
        2. 15.2. Making a Program
          1. 15.2.1. Creating a Project
          2. 15.2.2. Designing the Interface
          3. 15.2.3. Writing the Code
          4. 15.2.4. Running the Program
    9. IV. Appendixes
      1. A. AppleScript Support in Common Programs
        1. A.1. Databases
        2. A.2. Email Programs
        3. A.3. Graphics Editors
        4. A.4. Page Layout Programs
        5. A.5. Plain Text Editors
        6. A.6. Word Processors
        7. A.7. Web Browsers
      2. B. Moving from HyperCard to AppleScript
        1. B.1. Data Types
        2. B.2. Dialog Boxes
        3. B.3. Existence
        4. B.4. Numbers
        5. B.5. Pausing
        6. B.6. Ranges
        7. B.7. Repeat Statements
        8. B.8. Subroutines
        9. B.9. Variables
      3. C. Where to Go from Here
        1. C.1. Web Sites
          1. C.1.1. AppleScript Sites
          2. C.1.2. Other Mac Programming Sites
        2. C.2. Discussion Lists
        3. C.3. Books
    10. About the Author
    11. Colophon
    12. SPECIAL OFFER: Upgrade this ebook with O’Reilly