Introduction

AppleScript is a powerful computer language that’s been around since the days of Mac OS 7. Despite its maturity, however, AppleScript is often looked down upon by snooty Mac programmers for being too simple, too easy to learn, and too much like English.

Of course, those are precisely the traits you want in a computer language. If you’re an everyday Mac user—not some fancy-schmancy computer science Ph.D.—AppleScript is by far the easiest language to use for automating your Mac.

AppleScript has been around long enough to become a stable, powerful, and—most importantly—nearly bug-free language. It grew out of the old HyperCard project (Appendix B), whose goal was to make programming accessible to regular computer users. HyperCard was a big success, but Apple stopped making new versions after a few years.

Meanwhile, Apple took much of what it learned from HyperCard and applied it to its new techno-baby: AppleScript. Since its creation, AppleScript has been the heir to HyperCard’s dream of a simple, powerful language that non-programmers can use. Despite nine major revisions and countless minor ones, AppleScript’s approach today is much the same as it was 10 years ago—and that’s a good thing.

Uses for AppleScript

Of course, what really matters is what you can do with AppleScript. The answer? Just about anything you can do yourself.

AppleScript is a language with which you create individual scripts—little software robots that send commands to different programs on your computer. In fact, scripts do much of what you can do with a keyboard and mouse.

“But why should I learn an entirely new language just to do the things I do myself?” you ask. Luckily, AppleScript has several advantages over a human computer user:

  • AppleScript eliminates repetitive jobs. One of AppleScript’s greatest strengths is its ability to automatically perform similar jobs over and over again. With AppleScript, you can automatically rename all the files in a folder (Chapter 6), save all your open TextEdit documents at once (Chapter 5), or rate every song in your iTunes library (Chapter 8), for example. Best of all, AppleScript will never get repetitive stress disorder—or complain that you’re making it work too hard.

  • AppleScript automates complicated workflows. Another great ability of AppleScript is managing jobs that take place in multiple programs. For instance, AppleScript can take a picture from the Finder, bring the image into Photoshop, correct color tones, and shrink the file down to a suitable size for a Web site, all without breaking a sweat (Chapter 7).

  • AppleScript can do stuff in the background. Sometimes you might want to have a program that works without you asking, and AppleScript can do that too. If you have a screensaver running, for example, AppleScript can automatically set your iChat status to Away without you having to touch your keyboard or mouse (Chapter 9).

  • AppleScript works even when you’re not. If you’re on vacation, AppleScript can take over some of the jobs you’d normally do yourself. For instance, you can create a simple AppleScript that automatically puts excess junk from your Desktop into a different folder (Chapter 5).

  • AppleScript’s much faster than you are. Unless you’re some sort of computational Superman, AppleScript is always faster than you; while it might take you 10 minutes to rename all the files in your Documents folder, for example, AppleScript can do it in less than 10 seconds.

Advantages and Disadvantages

AppleScript is by no means the only scripting language for OS X. Nonetheless, AppleScript has several advantages that make the best option for many jobs:

  • It’s more like English than any other Mac language. AppleScript’s greatest advantage is that it uses grammar that closely resembles the commands you’d give to your seven-year-old son. (A typical command, for example, would be “say cheese”, which would have your computer speak the word “cheese” out loud.)

    If you’ve never learned a computer language before, you’ll be grateful that AppleScript doesn’t bother with semicolons, curly brackets, and all the other annoyances of more advanced computer languages. Even better, AppleScript is not case-sensitive—a feature you can appreciate only if you’ve ever had to search through reams of code just to find a mis-capitalized command name.

  • It works with the same programs that you do. Virtually every big-name Mac program supports AppleScript commands: Microsoft Word, Adobe Photoshop, FileMaker Pro, and so on. Lots of free programs support AppleScript, too: TextEdit, Address Book, Mac OS X Mail, iPhoto, and many more.

Note

For a list of common programs that work with AppleScript, see Appendix A.

  • It works with other computer languages, too. AppleScript isn’t just a little software island; it’s got bridges to virtually every other programming language on Mac OS X. If you’re a serious Unix hacker, for example, you can mix AppleScript commands right into your Unix programs (Chapter 13). And this cross-pollination works the other way around, too: you can mix Unix commands into your AppleScript programs just as easily (Chapter 13).

  • Lots of people use it. There are several popular gathering places for AppleScripters online, where you’ll find useful pre-made scripts for free. If you’re having a problem, chances are that someone else has had the same problem and posted a fix somewhere on the Web. And if you simply can’t work a problem out, AppleScript’s large community means that someone else can almost always help you fix it. (Appendix C has a list of these resources.)

Despite all its advantages, however, AppleScript isn’t perfect. Several trade-offs had to be made to make it more like English, for example, and the language sometimes reveals all too well that it was designed with 10-year-old computers in mind. Here are some of AppleScript’s biggest weaknesses:

  • Commands are sometimes inconsistent between programs. Because AppleScript has changed so many times over the years, some commands work differently in different programs. This isn’t a complete showstopper, though; it just means you may have to dig deeper into a program’s dictionary (Chapter 3) to find the right command.

  • Some programs don’t support AppleScript at all. Certain programs, unfortunately, don’t take advantage of all that AppleScript has to offer. Programs that show up in this category are usually old, free, and/or made by a company that doesn’t make Mac support a top priority.

    Still, all hope is not lost. If a program you use regularly doesn’t work with AppleScript out of the box, you may find that someone on the Internet has written a plug-in that adds AppleScript support. And as a last resort, you can use Apple’s own GUI Scripting feature (Chapter 12) to bend non-scriptable programs to your will.

  • It’s slower than most other languages. In exchange for all the power AppleScript grants you, the language has an unusually high overhead for running commands. In the real world, it probably won’t make much of a difference—AppleScript is still dozens of times faster than you are. But if speed is of utmost importance to you (say, you work in a mathematical or scientific field), you might want to look elsewhere for a language that better suits your needs.

  • It only works with Macs. Furthermore, if you write scripts that take advantage of new AppleScript features, your scripts will only work in Mac OS X. Of course, if you’re only going to write scripts for your computer, this isn’t a big deal. But if you need to exchange scripts with Windows or Linux users, you’ll need to find a cross-platform language.

In total, AppleScript is wonderful if you’re an everyday Mac user who wants to automate things, but it’s useless if you need your scripts to run on Windows.

About This Book

Like most software these days, AppleScript doesn’t come with a printed manual. To find your way around, you’re expected to use Apple’s Help Viewer program (in your Applications folder). But as you’ll quickly discover, the help pages are tersely written, offer very little technical depth, lack useful examples, and provide no tutorials whatsoever.

The AppleScript Language Guide (http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf ) is hardly any better. It hasn’t been updated since the days of OS 8.5, and it reads more like an encyclopedia than a help file. Of course, it’s wonderful to have a complete guide to the language for reference, but it doesn’t help much when you’re learning the language.

The worst part of both documents, however, is that they’re virtual help files: you can’t mark your place, underline important passages, or read them in the bathroom (unless, of course, you take your laptop in there with you). And there’s no more than a passing mention of powerful new features like multimedia support (Chapter 8), GUI Scripting (Chapter 12), or AppleScript Studio (Chapter 15). The purpose of this book, therefore, is to serve as the AppleScript manual that should have accompanied your computer.

What You’ll Need

This book covers the AppleScript features found in Mac OS 10.3 (“Panther”). If you have an earlier operating system, visit http://www.apple.com/macosx/upgrade/ for information on how to upgrade.

Furthermore, you may want a copy of these other programs that are used with some of the scripts in this book:

  • Adobe Photoshop CS ($650). Photoshop is the staple of any graphic pro’s software library, and its AppleScript support is nearly unbeatable. If you don’t feel like spending your retirement savings on software, just use the free 30-day trial version from http://www.adobe.com/products/tryadobe/main.jsp#product=39.

  • FileMaker Pro 7 ($300). FileMaker is a powerful database program that can organize virtually any kind of information. You can download a 30-day trial version from https://www.filemaker.com/downloads/trial_download.html, and that’ll work just as well.

  • Microsoft Word 2004 ($230). Word is the time-tested, cross-platform standard for word processing documents. It’s available as part of the Microsoft Office 2004 suite ($400), which also includes PowerPoint (for presentations), Excel (for spreadsheets), and Entourage (for emailing and creating calendars). You can download a free 30-day trial of the whole package from http://www.microsoft.com/mac/default.aspx?pid=office2004td.

Note

Many of the scripts in this book will work with an earlier version of these programs, but the scripts may behave somewhat differently. It’s a good idea, therefore, to download the free trial copies of the newest versions, so you can follow along easily with the scripts in this book.

About the Outline

This book is divided into four parts, each containing several chapters:

  • Part 1, AppleScript Overview covers the basics of using AppleScript to control your Mac. You’ll learn how to crack open the scripts that Apple gives you for free—and how to change what they do. These three chapters also explain the purpose of the built-in Script Editor program, and show you how to make the most of your time there.

  • Part 2, Everyday Scripting Tasks explains how to use AppleScript for automating typical jobs: renaming files, organizing your iPhoto library, playing music, and so on. These are also the chapters where you’ll learn about AppleScript’s different ways of storing information, such as lists, numbers, and database records.

  • Part 3, Power-User Features takes you beyond the basics into truly time-saving territory. You’ll learn how to make the Finder run scripts whenever you open a folder, and how to work around the scripting limitations in programs like System Preferences. These chapters also show you how to mix AppleScript with other powerful tools, to squeeze the most power possible from your scripts.

  • Part 4, Appendixes offers three useful references. Appendix A lists the Mac OS X programs that play best with AppleScript, so you can figure out which programs to avoid and which ones to use. Appendix B shows you how to move HyperCard stacks into Mac OS X, using AppleScript to ease the transfer. Finally, Appendix C sends you off into the great universe of advanced AppleScript books and Web sites.

Along the way, you’ll get a lot more out of this book if you have the free Missing CD, where you’ll find all the scripts from this book prewritten for you. Using that, you won’t have to retype all the long scripts by hand, and you’ll save yourself both time and typos. Page Chapter 2 has instructions for downloading the Missing CD.

About These Arrows

Throughout this book, and throughout the Missing Manual series, you’ll find sentences like this one: “Open the System folder Libraries Fonts folder.” That’s shorthand for a much longer instruction that directs you to open three nested folders in sequence, like this: “On your hard drive, you’ll find a folder called System. Open it. Inside the System window is a folder called Libraries. Open that. Inside that folder is yet another one called Fonts. Double-click to open it, too.”

Similarly, this kind of arrow shorthand helps to simplify the business of choosing commands in menus, such as

image with no caption

Dock Position on Left. That instruction is just another way of saying, “Click the Apple menu to open it, navigate to the Dock submenu, and then choose Position on Left.”

About MissingManuals.com

At http://www.missingmanuals.com, you’ll find news, articles, and updates to the books in this series. But if you click the name of this book and then click the Errata link, you’ll find a unique resource: a list of corrections and updates that have been made in successive printings of this book. You can mark important corrections right in your own copy of the book, if you like.

In fact, the same page offers an invitation for you to submit such corrections and updates yourself. In an effort to keep the book as up-to-date and accurate as possible, each time we print more copies of the book, we’ll make any confirmed corrections you’ve suggested. Thanks in advance for reporting any glitches you find!

In the meantime, we’d love to hear your suggestions for new books in the Missing Manual line. There’s a place for that on the Web site, too, as well as a place to sign up for free email notification of new titles in the series.

image with no caption

When you see a Safari® enabled icon on the cover of your favorite technology book, that means it’s available online through the O’Reilly Network Safari Bookshelf. Safari offers a solution that’s better than e-Books: it’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it free at http://safari.oreilly.com.

The Very Basics

To use this book, and indeed to use a Macintosh computer, you need to know a few basics. This book assumes that you’re familiar with these terms and concepts (if you’re not, pick up a book like Mac OS X: The Missing Manual):

  • Clicking. This book gives you three kinds of instructions that require you to use the mouse that’s attached to your Mac. To click means to point the arrow cursor at something on the screen and then—without moving the cursor at all—to press and release the clicker button on the mouse (or laptop trackpad). To double-click, of course, means to click twice in rapid succession, again without moving the cursor at all. And to drag means to move the cursor while keeping the button held down.

    When you’re told to

    image with no caption

    -click something, you click while pressing the

    image with no caption

    key (which is next to the Space bar). Such related procedures as Shift-clicking, Option-clicking, and Control-clicking work the same way—just click while pressing the corresponding key at the bottom of your keyboard.

  • Menus. The menus are the words in the lightly striped bar at the top of your screen. You can either click one of these words to open a pull-down menu of commands (and then click on a command) or click and hold the button as you drag down the menu to the desired command (and release the button to activate the command). Either method works just as well.

Note

Apple has officially changed what it calls the little menu that pops up when you Control-click something on the screen. It’s still a contextual menu, in that the menu choices depend on the context of what you click—but now it’s called a shortcut menu. That term not only matches what it’s called in Windows, but it’s more descriptive. Shortcut menu is the term you’ll find in this book.

  • Keyboard shortcuts. Every time you take your hand off the keyboard to move the mouse, you lose time and potentially disrupt your creative flow. That’s why many Mac pros use keystroke combinations instead of menu commands whenever possible.

    image with no caption

    -P opens the Print dialog box, for example, and

    image with no caption

    -M minimizes the current window to the Dock.

    When you see a shortcut like

    image with no caption

    -Q (which quits the current program), it’s telling you to hold down the

    image with no caption

    key and, while it’s down, type the letter Q, and then release both keys.

If you’ve mastered this much information, you have all the technical background you need to enjoy AppleScript: The Missing Manual.

Get AppleScript: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.