Chapter 1. Why to Use AppleScript

If you've never used AppleScript before, you're probably in need of motivation as much as information. You'd like to know: "What is AppleScript?" You'd also like to know: "And why should I care, anyway?"

Those are good questions, and they are best answered by a brief explanation of what AppleScript is for. Therefore, this first chapter classifies the main uses of AppleScript, along with some examples.

By presenting AppleScript in action, in some typical real-life contexts, I hope to inspire you to imagine how you might use AppleScript in your own life. AppleScript is a big subject, and your best incentive to press ahead is a vision of some task you actually want to accomplish with it. At the same time, you'll have a far easier, more enjoyable experience of AppleScript if your aims are consonant with its nature and abilities.

Tip

In this chapter, the examples are not intended for you to run on your own computer. This is real-life code that works on my machine, but is not expected to run elsewhere. Nor are you expected to understand the code at this point. I'm just showing it to you for purposes of illustration, so glance over it and move on! When you've read more of the book and have learned some AppleScript, you'll understand how to adapt these examples to your own purposes.

The Nature and Purpose of AppleScript

Consider the many and various applications on your computer, and how you typically make them do things. With your hands, you choose menu items, click buttons, and generally wield the mouse and keyboard in the usual way. You also use applications as a source of information; you typically get this information by reading it off the screen, and you can communicate information from one application to another by copying and pasting. Mediating between your hands, your eyes, and the application is your brain: as your eyes get information from the application, your brain decides what to do next, and instructs your hands accordingly.

With AppleScript, you make applications do things programmatically. An AppleScript program has the power to give commands to the application, taking the place of your hands on the mouse and keyboard, and it has the power to ask the application questions, taking the place of your eyes reading the screen; the program itself makes the decisions about what to do next, thus taking the place of your brain. Thus, AppleScript lets you automate the sorts of things you're accustomed to making applications do manually.

Why is that a good thing? For the same reason that any automation is good. AppleScript performs the same tasks you could perform manually, but it performs them faster, more accurately, and without your direct involvement—you needn't even be sitting at the computer. Some tasks, when performed manually, are tedious or repetitive or error-prone; it's downright annoying for you to have to perform them, whereas the computer never gets bored and never makes a mistake in calculation, and (let's face it) can perform them better than you.

For example, suppose you've got a folder full of image files and you want to change their names in a systematic way to image01.jpg, image02.jpg, and so forth. It isn't as if you don't know how to do this: you select the first image file with the mouse, press Return to start editing its name, type image01.jpg, and press Return again; then you select the next image file with mouse, and do it again, and so forth. The trouble is that you don't want to do it. The trouble is with that "Do it again," which rapidly becomes tiresome and error-prone; before long, your eyes are starting to go out of focus, or you are just plain bored out of your skull, and you start to make mistakes. The whole thing is simultaneously too easy (it's an annoying waste of time and brain-power) and too hard (it's easy to make a mistake). It's just not a fit task for a human being. But it's a perfect task for a computer, which won't get bored or make a mistake no matter how many files are in that folder. AppleScript lets you assign to the computer tasks that are better suited to it than to a human being. And that example was a tiny one; AppleScript is just as useful for assembling massive workflows, driving big applications through massive tasks, feeding information from one to the other, processing and reformatting it in complex ways.

To find reasons to use AppleScript, just leave your mental annoyance meter turned on. Does something feel slow, repetitious, clumsy, boring, error-prone? Do you feel that a program isn't quite doing what you want? Does a series of steps need to be reduced to one? Has the computer got you trained, like some sort of laboratory animal, to perform a sequence of set tasks in a certain way? That's just not right. The computer should work for you—not the other way around! Maybe AppleScript can turn the tables.

I've been talking about "AppleScript," and in particular about an "AppleScript program" that's going to replace your hands and brain and make the computer do the work for you. But where does this program come from? Someone has to write it. That "someone" could be someone else: you can find lots of AppleScript programs that might be useful to you, already written and floating around on the Internet, where there's an entire community and culture of AppleScript users, sharing their work and benefiting from one another's experience. On the other hand, that "someone" could be you. That's why this book is here; it teaches you to write programs using AppleScript. That way, you'll be in charge of the automating power of your computer—a power which even now is lurking there, just waiting for you to take advantage of its vast potential. (And, as you'll see in Chapter 2, it's lurking in a lot of places.)

The rest of this chapter will illustrate the following general principles about what AppleScript is good for:

  • AppleScript is appropriate primarily when you want to automate an application.

  • AppleScript is good for expressing calculated and repetitive activity.

  • AppleScript is a means of reducing the number of steps needed to perform an action.

  • AppleScript is a way of customizing an application.

  • AppleScript lets you combine specialties: by automating more than one application, you make them work together, letting each application do what it's good at and uniting their several powers.

Get AppleScript: The Definitive Guide, 2nd Edition 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.