views:

263

answers:

1

I've learned that iTunes XML file is actually a plist, and instead of trying to parse the raw XML, I could use property lists.

I'm able to access the "Tracks" section, but I'm able to unable to do anything simple like extracting a track name. Admittedly, I'm stumbling around a bit, but this is the code I've gotten to so far:

tell application "System Events"
    tell property list file property_file
        tell contents
            set my_tracks to value of property list item "Tracks"
            repeat with t in my_tracks
                set theName to value of property list item "Name" of t
                display dialog theName
            end repeat
        end tell
    end tell
end tell

Any pointers as to what I'm doing wrong?

If it helps, sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.
com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Major Version</key><integer>1</integer>
        <key>Minor Version</key><integer>1</integer>
        <key>Application Version</key><string>9.0.2</string>
        ...
        <dict>
                <key>2471</key>
                <dict>
                        <key>Name</key><string>Check The Rhime</string>
                        <key>Artist</key><string>A Tribe Called Quest</string>
                        ...
                </dict>
                <key>2473</key>
                <dict>
                        <key>Name</key><string>A Short History of Nearly Everyth
ing (Unabridged), Part 1</string>
                        <key>Artist</key><string>Bill Bryson</string>
                        ...
                </dict>
+2  A: 

When you do the value of property list item, AppleScript converts the whole thing into a native AppleScript value; in this case, the value is a record. Thus, you simply need to tweak the inside a little bit:

property property_file : ¬
    (POSIX path of (path to home folder) & ¬
        "Music/iTunes/iTunes Music Library.xml")

tell application "System Events"
    tell property list file property_file
        tell contents
            set my_tracks to value of property list item "Tracks"
            repeat with t in items of my_tracks
                display dialog (|Name| of t)
            end repeat
        end tell
    end tell
end tell

Doing items of my_tracks produces a list of the values of the record; |Name| of t is just record access. Unfortunately, doing this plist processing seems to be very slow, since the XML file is so massive.

Antal S-Z
Thanks! I also noticed you added "... in items of my_tracks". Can you help me understand what that does?
Bill
`repeat with var in some_list` only works if `some_list` is a list. Since `my_tracks` is a record (the same thing as a plist dict or a dictionary in Python, only without a way, as far as I can tell, to access its keys), we have to convert it into a list. `items of my_tracks` does that, returning a list of all the values in the record. Since `my_tracks`, as you can see in the XML, maps numbers to records, `t` is then each record in turn, and we thus extract the relevant data (`Name`—the pipes are necessary to preserve case) from it.
Antal S-Z