【问题标题】:Conversion of iTunes applescript to use newer iTunes library framework将 iTunes applescript 转换为使用更新的 iTunes 库框架
【发布时间】:2017-05-02 12:22:14
【问题描述】:

能否将此applescript 转换为使用iTunes Library framework 而不是使用标准的iTunes applescript。

我当前的脚本读取用户的 iTunes 库并创建一个由 TrackName、TrackLocation 和 PersistentId 组成的文件,它工作可靠,但当用户拥有大型 iTunes 库时可能会很慢

tell application "iTunes"
    with timeout of 2400 seconds
        if (count of every file track of library playlist 1) is equal to 0 then
            set thePath to (POSIX file "/tmp/songkong_itunes_model.new")
            set fileref to open for access (thePath) with write permission
            set eof fileref to 0
            close access fileref
            return
        end if

        tell every file track of library playlist 1
            script performancekludge
                property tracknames : its name
                property locs : its location
                property persistids : its persistent ID
            end script
        end tell
    end timeout
end tell

set thePath to (POSIX file "/tmp/songkong_itunes_model.new")
set fileref to open for access (thePath) with write permission
set eof fileref to 0

tell performancekludge
    repeat with i from 1 to length of its tracknames
        try
            set nextline to item i of its tracknames ¬
                & "::" & POSIX path of item i of its locs ¬
                & "::" & item i of its persistids
            write nextline & linefeed as «class utf8» to fileref
        end try
    end repeat
end tell
close access fileref

最大的不同是这个新框架不需要 iTunes 实际运行,我希望它应该更快。然而,稀疏的指令只讨论了 ObjectiveC 我不清楚我将如何在 applescript 中编写一个版本(甚至在 Java 中更好)

【问题讨论】:

    标签: applescript itunes


    【解决方案1】:

    这是使用 iTunesLibrary 框架的 AppleScriptObjc 等效项。它不会逐行写入文本,而是创建一个数组,插入换行符,然后一次写入整个文件。它在写入文件时也使用了更好的错误处理。

    use AppleScript version "2.4" -- Yosemite (10.10) or later
    use scripting additions
    
    use framework "iTunesLibrary"
    
    set {library, theError} to current application's ITLibrary's libraryWithAPIVersion:"1.0" |error|:(reference)
    if theError is not missing value then
        display dialog theError's localizedDescription() as text buttons {"Cancel"} default button 1
    end if
    set tracks to library's allMediaItems()
    
    set thePath to "/tmp/songkong_itunes_model.new"
    
    set theLines to {}
    repeat with aTrack in tracks
        try
            set nextline to (aTrack's title as text) ¬
                & "::" & (aTrack's location's |path| as text) ¬
                & "::" & (aTrack's persistentID()'s intValue())
            set end of theLines to nextline
    
        end try
    end repeat
    set {TID, text item delimiters} to {text item delimiters, return}
    set theText to theLines as text
    set text item delimiters to TID
    try
        set fileref to open for access thePath with write permission
        set eof fileref to 0
        write theText as «class utf8» to fileref
        close access fileref
    on error
        try
            close access thePath
        end try
    end try
    

    【讨论】:

    • 感谢您这样做,但是当我运行它时,文件已创建但为空,旧脚本可以正常工作。
    • 我在脚本编辑器和脚本调试器中成功运行了脚本。唯一的区别是我将文本文件保存在桌面上。
    • 我读过一些关于应用程序必须经过代码签名才能使用此 api 的内容,这可能是我刚刚在脚本编辑器中运行的问题。 (尽管我调用它的主要 Java 应用程序是代码签名的)
    • Cocoa 应用程序需要代码签名,但它似乎在没有 AppleScript 环境的情况下也能工作。
    • 有什么想法吗?如果它对我不起作用,我无法将这个答案标记为正确
    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2011-03-13
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多