通过iTunes的plist(XML)文件访问iTunes中的曲目名称

时间:2010-01-02 03:25:36

标签: applescript itunes-sdk

我了解到iTunes XML文件实际上是一个plist,而不是尝试解析原始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 my_tracks
                set theName to value of property list item "Name" of t
                display dialog theName
            end repeat
        end tell
    end tell
end tell

关于我做错了什么的指示?

如果有帮助,请采样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>

1 个答案:

答案 0 :(得分:2)

执行value of property list item时,AppleScript会将整个内容转换为原生AppleScript值;在这种情况下,该值是一个记录。因此,您只需稍微调整一下内部:

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

执行items of my_tracks会生成记录值的列表; |Name| of t只是记录访问权限。不幸的是,这个plist处理似乎非常慢,因为XML文件非常庞大。