Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [224]

By Root 1615 0
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.

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 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

Folder Actions


A folder action involves a script being called automatically when certain events take place in a designated folder in the Finder. A script to be used as a folder action does not live in the folder with which it is associated; rather, it should live in your user library, at ~/Library/Scripts/Folder Action Scripts. Alternatively, such a script may live in the top-level /Library/Scripts/Folder Action Scripts, where you'll find some useful examples of folder action scripts, but the location in your user library is the default.

Because the script and the folder are in different places, an extra step is required in order to form the explicit association between a particular script and a particular folder. In essence, this association is the folder action. To form such an association is to attach the script to the folder; to break the association is to remove the script from the folder. The script is not actually moved; attachment and removal are conceptual, not physical. Attachment and removal are implemented through the System Events application (see "System Events" in Chapter 23). System Events associates a folder with a script through a folder action object. These folder action objects are maintained as top-level elements of the System Events application class. System Events is then responsible for watching those folders to see whether an appropriate event takes place in the Finder, and, when it does, for sending the corresponding messages to any attached scripts. System Events doesn't do this, though, unless you also enable the folder actions mechanism as a whole—you do this through its folder actions enabled property, which functions as a kind of master switch.

Tip

It

Return Main Page Previous Page Next Page

®Online Book Reader