What causes a script to run? It might be that you deliberately run it—you press the Run button in a script editor application, for example, or you choose the script from the menu of a script runner environment. However, there is a whole class of situations where your script just sits there patiently and eventually some other process comes along and runs it, with no direct intervention or action on your part. I call this sort of milieu an automatic location (see Chapter 2). In an automatic location, AppleScript takes a passive instead of an active part. Instead of you personally setting a chain of events in motion, AppleScript operates in response to other events or activities taking place on your computer: someone inserts an audio CD, someone moves a file into a certain folder, a certain web page request appears at your server, a certain time of day arrives, the computer wakes from sleep, a certain hard drive is mounted, the telephone rings.
There are a number of distinct automatic locations built into Tiger, and there are many varieties of circumstance in which third-party applications may trigger your scripts. A complete compendium of the ways in which scripts can be triggered automatically would be impossible, so the chief purpose of this chapter is to awaken you to the range of possibilities. You may be surprised by the sorts of role AppleScript can play on your computer.
Preparing a script to operate in an automatic location is different from simply writing a script and running it. The reason is that you're not in charge. Some other process is—the process that will actually call your script—and you must know and obey the strictures imposed by that process. In general, you'll need to know two things:
Some arrangement will have to be made so that the calling process knows where and when to find your script. The script may have to be in a particular folder. It may have to have a particular name. You may have to make some sort of preparatory arrangement, explicitly informing the process beforehand that when a certain thing happens, a certain script should be called.
Your script might simply be executed (its run handler will be called, just as if you executed the script from a script editor application); but this is very often not the case. Rather, a particular event or handler call may be sent to your script, and it is up to you to know what it is, and to prepare your script to receive it.
Finding out all of this is up to you, and will usually involve reading some sort of documentation. You are making a highly specific prearrangement with the triggering process, in accordance with a kind of contract; you must know the terms of that contract and abide by them, or things won't work.
On your computer, when you insert a music CD, likely as not, the application iTunes runs. But it doesn't have to be that way. This is one example of a general phenomenon called digital hub scripting: when a DVD, or a CD that doesn't consist of ordinary files, is inserted into your computer, the system can react by notifying a designated application. A little-known fact is that you can interpose your own code in this process; instead of iTunes, when an event like this occurs, a script of your choice is triggered, and can react in any desired manner.
In the CDs & DVDs pane of System Preferences are the settings that determine how the system responds to a disk-insertion event. Here you can determine what application should be notified when the disk is inserted; alternatively, there's an option to run a script. The system will send one of five events to your script, and so your script will need to contain a handler for the appropriate event (see "Event Handlers" in Chapter 9). To learn what these events are, examine the dictionary of the Digital Hub Scripting scripting addition, where their terminology is defined.
Let's say we want to take charge of what happens when an audio CD is inserted. This means that in our script there must be a music CD appeared
handler. Suppose we call our script musicListener.scpt. In the CDs & DVDs preferences, we choose from the "When you insert a music CD" popup menu; the Run Script menu item lets us set musicListener.scpt to be called when a music CD is inserted. Here, we offer the user a choice of playing just one track of the audio CD:
on music CD appeared d set diskName to d as alias as string set text item delimiters to ":" set diskName to text item 1 of diskName tell application "Finder" set L to name of every file of disk diskName end tell tell application "iTunes" activate set temp to {diskName, choose from list L} play file (temp as string) end tell end music CD appeared