What Is Scripting, Anyway?
Why is this AppleScript stuff called scripting? The answer is unclear, but it was probably a marketing ploy to keep people from thinking about programming. While real people tend to think of programming as some arcane ritual that only nerds can master, it’s really just a way to tell the computer what to do. Unfortunately, telling a computer what to do can be very, very complex. Fortunately, Apple has been at the forefront of developing technologies — including AppleScript — that make telling computers what to do easier.
Think of your Mac as your servant, a very efficient, very fast, but not too bright aide-de-camp who really wants to make your life nicer. All you have to do is tell it what to do in a very simple language that it can understand: AppleScript.
The Mac is very nifty, but it can’t yet read your mind. So you have to tell it what to do. Of course, you already do that. After all, what is clicking menu items or dragging files around with the mouse but telling the Mac what to do? AppleScript makes life nicer by letting you tell the computer once, by writing a script, rather than by pointing and clicking every time you want to do something.
That’s really all scripting is — writing a set of instructions for the Mac. Imagine sending a friend a set of directions to your house or telling someone over the phone how to do something in iPhoto. In order to write a script, you just set those same instructions down in AppleScript. After you have the instructions down so that your Mac can understand them, you can have the Mac follow them any number of times. Think of scripts as add-on smarts for your Mac that let your Mac do the tasks that you want it to, when you want it to, and how you want it to.
Apple events aren’t just parties anymore!
You may or may not have ever heard of Apple Events, but they’re critical to the success of AppleScript. The good news is that while the details of Apple Events and the Object Model are fairly complex, Apple has succeeded in hiding all of that nasty stuff from you. In fact, you can spend your whole scripting life without ever once using either term. However, having at least a vague idea of what these terms mean gives you an understanding of how AppleScript works its magic.
Apple Events are just messages sent from one Mac application to another.
Yep, that’s all they are. The bad news is that just as you need to wire your house for cable, application developers have to work to make their programs support Apple Events. If they don’t spend the time, then their applications can’t send or respond to Apple Events. Such applications aren’t controllable from AppleScript scripts.
To make life more exciting, not all scriptable applications are the same. Some support only the four basic messages — Open, Print, Quit, and Run — which is essentially the same as not being scriptable because you don’t have any real control over what the application does. Other applications are so scriptable that you can change their basic behavior through scripts.
Levels of scripting
There are four officially recognized levels of AppleScript support:
- Scriptable: This term covers any program that has some level of support for Apple Events.
- Recordable: These applications let AppleScript automatically build a script by watching you work. It performs this feat by watching the Apple Events generated by your actions — selecting a menu item, using the keyboard, and so on — and translating them into AppleScript commands by using the application’s dictionary.
- Attachable: These fine examples of advanced software engineering let you attach scripts to various items in the program. For example, BBEdit has a special script menu that contains scripts you put into a specified folder.
- Tinkerable: These applications are few and far between, but they go one step beyond attachable by letting you change the basic way the program behaves with a script. For example, if you don’t like the way a certain menu command works in a tinkerable program, you can write a script that replaces the default action of that menu item with the action you want to perform.
Fortunately you can easily tell if an application is scriptable. You can use the Script Editor to look for applications with dictionaries. From the Script Editor, choose File –> Open Dictionary. You get a window which lists all the applications that the Script Editor thinks are scriptable. It turns out this method isn’t perfect though, so if you really want to be sure an application isn’t scriptable, click the Browse button in the lower-left corner of the Open Dictionary window and use the resulting standard Open dialog to locate the application you’re interested in. If the application you’re interested in isn’t grayed out, then it’s scriptable. To see an application’s dictionary, just click the Open button in the Open Dictionary window or the Choose button in the Open dialog.
A dictionary is a description of the commands that a program responds to and the types of values — called classes — that it understands. A word processor may have a paragraph class while a drawing program probably has a line class.
Just because an application is scriptable doesn’t mean you can use it in your scripts. Some scriptable applications have so few scripting commands that they’re not really very useful.
The Object Model is a way to define information in a modular manner. You’ve probably used the Object Model many times in everyday life. That’s right, even if you’ve never used a single script, you’ve probably used something very much like the Object Model. Let me give you an example: You’ve probably said something like, “Look at the second paragraph in that article” or “The second word in that paragraph is misspelled.” Both of these statements represent information in a modular, container-oriented fashion. In the first example, an article — one type of object — contains paragraphs — another kind of object. In the second example, a paragraph contains words.
An application doesn’t have to implement the Object Model in order to be scriptable, nor is the definition of the Object Model so precise that you can define a “correct” implementation of the Object Model for a given application. In fact, you’ll find that you never really have to worry about the Object Model. What you will care about is whether an application defines enough information objects to let you script it effectively. A word processor without a word or paragraph object would be a bit tough to work with.
What can you do with a script?
Make a movie. Of course, if you don’t have $120,000,000 lying around to finance a big-screen extravaganza, you can settle for saving time by automating things you currently do manually.
Writing a script is just telling your Mac what to do with words instead of by moving and clicking the mouse. You might tell a coworker what job needs to be done verbally — that’s like using the mouse on your Mac — or you might write him or her a note — probably e-mail these days — and that’s like using AppleScript. AppleScript is just a really simple language that your Mac can understand.
AppleScript itself is not very powerful. That’s not a bug; it’s a feature! AppleScript was designed to be the glue to control and integrate the activities of various Mac applications, including the Finder.
This is one of the most forgotten Mac facts. The Finder is just another application, like Excel or Word.
Because AppleScript lets you control applications, you can do all sorts of neat automation with very little AppleScript experience. But right now, you’re just a beginner, so what can you do? Selecting, sorting, and printing records in a FileMaker Pro database only takes six lines of AppleScript. Changing your desktop picture is also really easy, it only takes these three script lines:
tell application “Finder”
set the_pic to choose file “Select a desktop picture”
set desktop picture to the_pic
Pretty easy to read, right? This lets AppleScript know which application you want to send the indented lines to. The first indented line lets you pick a file. The second indented line tells the Finder to set the desktop picture to whatever you picked.
One last simple example: Making a script that wakes up every 15 minutes and warns you to take a break so that you don’t come down with repetitive stress injuries (RSI) is easy. Now you could spend $30–$50 for a commercial product, or you can spend 15 minutes writing yourself a script.
The bottom line is that even with very little scripting experience, you can write scripts that save you time and make your life a little bit better — unless, of course, you like doing mindless and repetitive tasks on the Mac.