【问题标题】:Get list of unique iTunes artists via AppleScript通过 AppleScript 获取唯一 iTunes 艺术家列表
【发布时间】:2017-04-09 20:30:17
【问题描述】:

我正在尝试从我的资料库中获取独特的 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 中获取我不知道的艺术家或流派列表...

【问题讨论】:

  • 你检查过 DougScripts 吗?那里有大量运行速度极快的脚本。有一个专门针对您想要的,如果您愿意,可以将其导出为 txt 文件。我现在不记得它的名字了,但它让我的 74gb 音乐快速工作。

标签: applescript itunes


【解决方案1】:

例如,如果您获得属性值列表而不是轨道对象,则可以保存许多 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

【讨论】:

  • 酷,我没想过使用objective-c 来帮助解决这个问题。也不知道您在答案的第一部分中使用的简短语法。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
相关资源
最近更新 更多