DekGenius.com
[ Team LiB ] Previous Section Next Section

24.2 Digital Hub Scripting

You probably have your computer set up so that when you insert a music CD, the application iTunes runs. 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 sending an Apple event to a designated application. You can interpose your own code in this process: instead of iTunes, when an event like this occurs, an applet of your choice is notified, and can react in any desired manner.

In the CDs & DVDs pane of System Preferences are the settings that determine how the System reacts to a disk-insertion event. You can determine what application should be notified when the disk is inserted. If this application is an applet, it should be prepared to receive an Apple event corresponding to what sort of disk was inserted. The terminology for these Apple events is defined in the dictionary of the Digital Hub Scripting scripting addition. (Therefore the parameter does not need to appear in parentheses at the start of the definition.)

Let's say we want to take charge of what happens when a music CD is inserted. We'll create an applet called musicListener. In the CDs & DVDs pane of System Preferences, we use the Open Other Application menu item to set musicListener as the target application to respond when a music CD being inserted. In musicListener, the music CD appeared handler will be called when a music CD is inserted.

To illustrate, let's offer the user a choice of playing just one track of the music 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
        set whichTrack to choose from list L
        tell application "iTunes"
                play file ({diskName, whichTrack} as string)
        end tell
end music CD appeared
    [ Team LiB ] Previous Section Next Section