Performance Applescript-在库中迭代所有iTunes曲目时性能不佳

Performance Applescript-在库中迭代所有iTunes曲目时性能不佳,performance,applescript,itunes,osx-elcapitan,Performance,Applescript,Itunes,Osx Elcapitan,我正在尝试检查哪些曲目位于我的iTunes库目录中,哪些没有使用AppleScript 下面的脚本非常慢,每首曲目大约需要2秒(库中大约有8000首曲目): 还尝试了以下操作,但性能相同: #!/usr/bin/osascript tell application "iTunes" repeat with l in (location of every file track) POSIX path of l does not start with

我正在尝试检查哪些曲目位于我的iTunes库目录中,哪些没有使用AppleScript

下面的脚本非常慢,每首曲目大约需要2秒(库中大约有8000首曲目):

还尝试了以下操作,但性能相同:

#!/usr/bin/osascript
tell application "iTunes"

        repeat with l in (location of every file track)
                POSIX path of l does not start with "/Users/user/Music/iTunes/iTunes Media/"
        end repeat

end tell
与此同时,iTunes变得毫无反应

一定是在做傻事,但不知道是什么

这是在OS X El Capitan的2015 27'iMac上

谢谢你的帮助


干杯

通过使用关键字
get

repeat with l in (get location of every file track)
区别在于:

  • 如果不使用
    get
    ,则会在每次迭代中检索列表
  • 使用
    get
    检索列表一次
两个问题:

  • 发送大量苹果活动是昂贵的<代码>用l in重复(每个文件磁道的位置)为每个磁道发送一个单独的
    获取
    事件(
    获取文件磁道1的位置
    获取文件磁道2的位置
    ,…)。首先获取所有位置的列表,然后对其进行迭代

  • 由于糟糕的实现,获取AppleScript列表项所需的时间随着列表的长度线性增加;因此,当迭代大型列表时,性能将在存储库中运行(
    O(n*n)
    而不是
    O(n)
    效率)。你可以通过一个讨厌的黑客把它降到
    O(n)
    ,通过引用引用列表项(例如,将列表粘贴到脚本对象属性中,然后引用该属性)

  • 例如:

    set iTunesFolder to POSIX path of (path to music folder) & "iTunes/iTunes Media/"
    
    tell application "iTunes"
        script
            property fileLocations : location of every file track
        end script
    end tell
    repeat with l in fileLocations of result
        set fileName to (POSIX path of l)
        if fileName does not start with iTunesFolder then log fileName
    end repeat
    

    完全正确谢谢
    set iTunesFolder to POSIX path of (path to music folder) & "iTunes/iTunes Media/"
    
    tell application "iTunes"
        script
            property fileLocations : location of every file track
        end script
    end tell
    repeat with l in fileLocations of result
        set fileName to (POSIX path of l)
        if fileName does not start with iTunesFolder then log fileName
    end repeat