1.1. 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 involvementyou 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 younot 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 computera 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: