DekGenius.com
[ Team LiB ] Previous Section Next Section

1.1 The Nature and Purpose of AppleScript

As you know, you've got various applications on your computer, and you typically make them do things by choosing menu items and clicking buttons and generally wielding the mouse and keyboard in the usual way; and you get information from them by reading it off the screen, or you can communicate information from one application to another by copying and pasting.

With AppleScript, you can make applications do things, not with the mouse and keyboard and screen, but programmatically—by writing and executing a little program that gives an application commands and fetches information from it. In the chain of actions that you make the application perform, the program that you write takes the place of your brain; the program's power to give commands to the application takes the place of your hands on the mouse and keyboard, and its power to ask the application questions takes the place of your eyes reading the screen. Thus, you can automate the sorts of things you're accustomed to making applications do manually. Instead of your doing something with the mouse, then reading the screen, then thinking about what this means and what you should do next, and so forth, the computer does the doing, the reading, and the thinking. This means that your hands and eyes and brain are freed from having to perform repetitive or tiresome activities better suited to the computer itself.

Suppose, for example, 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. Now you select the next image file with mouse, and do it again. The business of doing it again rapidly becomes tiresome and error-prone. You have to remember where you are ("What was the number I assigned to the previous image file I renamed?"), think what to do next ("What do I get when I add 1 to the previous number?"), and do it (click, Return, type, Return). It isn't long before you're making mistakes clicking or typing, or your eyes are starting to go out of focus, or you are just plain bored out of your skull.

How many files would there have to be before you'd regard this as a daunting or boring or error-prone task? A thousand? A hundred? To me, the prospect of manually renaming even ten files in this way seems an annoying waste of my time and brain-power. I've got better things to do than repetitively to click and type and add 1! With AppleScript, you can just write a little program that accomplishes the same thing automatically, and it doesn't matter how many files the folder contains—the program will do the job for you, and it won't make any mistakes.

And that, of course, is just a tiny example. When I was editing MacTech magazine, AppleScript was an essential part of our workflow; we had massive tasks, tying together several major applications such as Microsoft Word and QuarkXPress, with information moving from one application and being fed into another and then being formatted and prepared in all sorts of clever, complicated ways—and these tasks were automated, freeing the human user from the burdens of tedium and accuracy and casting those burdens onto the computer itself, thanks to AppleScript.

The name "AppleScript" denotes both the language in which you write the program that automates your existing applications and the underlying System-level technology that supports and executes it. AppleScript is present as part of the System. You get it for free, so you may as well take advantage of it. And you know it will be present on any Mac OS computer, so if you write an AppleScript program that might be useful to others, you can share it. Or, just the other way around, you can find lots of AppleScript programs floating around on the Internet that might be useful to you. There's an entire community and culture of AppleScript users, sharing their work and benefiting from one another's experience.

I don't want to give the impression, however, that AppleScript lets you tell every application programmatically to do everything it is capable of. That, alas, is not so. It lets you tell some applications programmatically to do some of the things they are capable of. The way AppleScript works is by sending messages to the applications you are automating; these messages are called Apple events. You cannot send just any old Apple event to any old application. (Well, you can, but it might not have any effect.) The application you're sending an Apple event to must recognize that Apple event and must have a way of responding to it. Such an application is said to be scriptable.

Based on these considerations, we can now enunciate some general principles about what AppleScript is good for:

  • AppleScript is appropriate primarily when you have a scriptable application that you want to automate.

  • AppleScript is good for expressing calculated and repetitive activity.

  • AppleScript is a good means of reduction, combining multiple steps into a single operation.

  • AppleScript is a way of customizing an application.

  • AppleScript gives you the opportunity to combine specialties (you could also think of this as divide and conquer): 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.

In general, if you're looking for ways to use AppleScript, my advice is to leave your mental annoyance meter turned on. When the computer annoys you, that's a sign that perhaps you should call upon AppleScript to help you. 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 round! Maybe AppleScript can turn the tables.

    [ Team LiB ] Previous Section Next Section