Chapter 26. Triggering Scripts Automatically
What causes a script to run? It might be that you deliberately run ityou 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
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
Preparing a script to
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. |
26.1. Digital Hub Scripting
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
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
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
|