iTunes AppleScript:查询包含特定曲目的所有播放列表

时间:2014-12-11 02:14:37

标签: macos applescript itunes

使用复杂的AppleScript for iTunes。一个任务是累积包含给定轨道的所有播放列表的列表。我有来自其他地方的轨道对象(选择或其他)。

目前,我有一段这样的代码:

on containingPlaylists(theTrack)
    tell application "iTunes"
        set librarySource to the source named "Library"
        set candidateLists to every user playlist in librarySource
        set candidateId to (get id of theTrack)
        set matchLists to {}
        repeat with candidateList in candidateLists
                set matchTracks to (file tracks in candidateList whose id = candidateId)
                if (count of matchTracks) > 0 then
                        copy candidateList to end of matchLists
                end if
        end repeat
        return matchLists
    end tell
end containingPlaylists

这可行但在循环中每个播放列表需要一个Apple事件,这是昂贵的(性能)并丢弃中间结果。我所做的就是一次性查询:

set matchLists to every playlist in librarySource whose file tracks contain theTrack 

但这当然不起作用(特定错误是" Handler只处理单个对象。"但不确定这是否具有洞察力)。我真的不确定语言/应用是否支持这样的查询。

任何人都可以确认/否认/提供任何见解吗?谢谢!

2 个答案:

答案 0 :(得分:3)

您可以使用此功能(适用于 iTunes 11和12):

tell application "iTunes"
    set theTrack to item 1 of (get selection)
    return user playlists of theTrack
end tell

更新 -

AppleScript 词典中:

艺术品n [inh。 item]:轨道中的一件艺术品 元素:由曲目包含。所以artworks of thisTrack有效

追踪n [inh。 item]:可播放的音频源| 要素:包含艺术品;由播放列表包含。因此playlists of thisTrack有效,您可以使用user playlists of thisTrack

iTunes.h (ObjC脚本桥)中:

@interface iTunesTrack:iTunesItem - (SBElementArray *)艺术品; 这是不可能的,因为播放列表不在SBElementArray的列表中。

但我不知道为什么AppleScript字典和iTunes.h文件之间存在差异。

答案 1 :(得分:1)

我也希望可以使用这样的条款。可惜。其他人可能会提出更好的计划,但我很确定这就是我如何找到包含所选曲目的播放列表(它可能是最有效的):

set persisID to persistent ID of selection
set pp to playlists
set playListsWithIt to {}
repeat with p in pp
    set tt to (tracks of p whose persistent ID is persisID)
    if tt ≠ {} then set playListsWithIt to (playListsWithIt & (id of p))
end repeat

然后我可以将这些ID用于下一步。当然,这包括“最近添加”等播放列表,这些播放列表可能是您想要的,也可能不是您想要的;你必须在那里再插一步来“过滤”这样的结果。

相关问题