2017-04-09 98 views
0

我试图从我的图书馆获取独特的iTunes艺术家和流派的列表。在某些操作中,AppleScript可能会很慢,在这种情况下,我无法在速度上做出妥协。有没有进一步的重构,我可以做我的代码?通过AppleScript获取独特的iTunes艺术家列表

tell application "iTunes" 
    -- Get all tracks 
    set all_tracks to shared tracks 

    -- Get all artists 
    set all_artists to {} 
    repeat with i from 1 to count items in all_tracks 
     set current_track to item i of all_tracks 
     set current_artist to genre of current_track 
     if current_artist is not equal to "" and current_artist is not in all_artists then 
      set end of all_artists to current_artist 
     end if 
    end repeat 
    log all_artists 
end tell 

我觉得应该有一个更简单的方式来获得从我只是不知道iTunes的艺术家或流派的列表...

+0

你检查过DougScripts吗?那里有很多脚本快速运行。有一个专门用于你想要的东西,如果你愿意,可以将它导出到一个txt文件。我现在不记得它的名字,但它使我的74GB音乐的快速工作。 – Chilly

回答

1

您可以保存许多苹果事件,如果你得到例如属性值列表而不是跟踪对象

tell application "iTunes" 
    -- Get all tracks 
    tell shared tracks to set {all_genres, all_artists} to {genre, artist} 
end tell 

解析字符串列表根本不消耗Apple事件。

-- Get all artists 
set uniqueArtists to {} 
repeat with i from 1 to count items in all_artists 
    set currentArtist to item i of all_artists 
    if currentArtist is not equal to "" and currentArtist is not in uniqueArtists then 
     set end of uniqueArtists to currentArtist 
    end if 
end repeat 
log uniqueArtists 

在Cocoa(AppleScriptObjC)的帮助下,它可能要快得多。 NSSet是一个包含唯一对象的集合类型。当从一个数组创建一个集合时,所有的重复项都会被隐式删除。方法allObjects()将设置转换回数组。

use framework "Foundation" 

tell application "iTunes" to set all_artists to artist of shared tracks 
set uniqueArtists to (current application's NSSet's setWithArray:all_artists)'s allObjects() as list 
+0

很酷,我没有想过使用objective-c来解决这个问题。不知道你在答案的第一部分中使用的简短语法。谢谢! –

相关问题