通过AppleScript获取独特的iTunes艺术家列表

时间:2017-04-09 20:30:17

标签: applescript itunes

我正在尝试从我的图书馆获取独特的iTunes艺术家和流派的列表。某些操作AppleScript可能会很慢,在这种情况下,我不能在速度上妥协。我可以对我的代码进行进一步的重构吗?

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中我不知道的艺术家或流派列表......

1 个答案:

答案 0 :(得分:2)

如果获得属性值列表而不是轨道对象(例如

),则可以保存许多Apple事件
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