【问题标题】:Applescript - Poor performance iterating all iTunes tracks in the libraryApplescript - 迭代库中的所有 iTunes 曲目时性能不佳
【发布时间】:2017-06-11 04:14:53
【问题描述】:

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

以下脚本真的慢每首曲目大约需要 2 秒(库中大约有 8000 首曲目):

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

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

end tell

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

#!/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 变得非常迟钝。

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

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

任何帮助表示赞赏。

干杯

【问题讨论】:

    标签: performance applescript itunes osx-elcapitan


    【解决方案1】:

    您可以使用关键字get 显着加快脚本速度

    repeat with l in (get location of every file track)
    

    区别在于:

    • 没有get,每次迭代都会检索列表
    • 使用get 检索一次列表

    【讨论】:

    • 准确!谢谢
    【解决方案2】:

    两个问题:

    1. 发送大量 Apple 事件的成本很高。 repeat with l in (location of every file track) 为每个轨道发送一个单独的 get 事件(get location of file track 1,get location of file track 2,...)。首先获取所有位置的列表,然后对其进行迭代。

    2. 由于糟糕的实现,获取 AppleScript 列表项所需的时间随着列表的长度线性增加;因此迭代大型列表时的性能会进入坦克(O(n*n) 而不是O(n) 效率)。您可以使用讨厌的 hack 将其归结为 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
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2016-04-22
      • 2023-03-08
      • 2010-12-28
      相关资源
      最近更新 更多