Previous Page
Next Page

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


Previous Page
Next Page