【问题标题】:How do I get the file path of an iTunes Selection如何获取 iTunes 选择的文件路径
【发布时间】:2010-12-24 07:20:45
【问题描述】:

我正在尝试使用 AppleScript 确定 iTunes 中所选曲目的路径。它似乎不是 track 类的属性。谁能告诉我如何获取文件路径?

【问题讨论】:

    标签: macos applescript itunes


    【解决方案1】:

    试试这个:

    --gets file path of selected song
    tell application "iTunes"
     set songLocation to get location of selection
    end tell
    return songLocation
    
    --gets file path of currently playing song
    tell application "iTunes"
     set songLocation to get location of current track
    end tell
    return songLocation
    

    【讨论】:

    • file track 类继承自 track 类,添加了 location 属性(例如,来自共享库的轨道可能没有),其中包含文件。所以你可能想先检查类型,使用这个:if class of myTrack is not file track then...。顺便说一句,如果轨道是“孤立的”——找不到文件——location = missing value,可以检查它以找到孤立的文件。
    【解决方案2】:

    如果你打算使用 Python 而不是 AppleScript 并且不想解析 plist XML 文件,你可以通过 COM API 获取文件路径。

    import win32com.client
    
    iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
    currentTrack = win32com.client.CastTo(iTunes.CurrentTrack,"IITFileOrCDTrack")
    
    print currentTrack.Location
    

    【讨论】:

      【解决方案3】:

      如果不能通过 AppleScript 获得,最好的办法是打开并解析 iTunes plist 文件。如果您只需要静态信息,那么您就是黄金。如果您需要动态信息(例如,当前正在播放的曲目的信息),您需要通过 AppleScript 获取曲目的 persistent ID,然后解析 plist 文件并查找您需要的任何信息(包括具有完整路径的 location)。

      要解析该 plist XML 文件(在 ~/Music/iTunes/iTunes Music Library.xml 中),您可以使用 ruby​​ 或 python 或任何其他您喜欢的语言。如果你喜欢 python,试试这个库:

      http://docs.python.org/library/plistlib.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-06
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        相关资源
        最近更新 更多