在AppleScript中播放艺术家的TOP歌曲

时间:2017-01-16 14:26:36

标签: applescript itunes

我正在尝试制作一个可以播放某位艺术家的热门歌曲的AppleScript。我通过ScriptEditor看过一个iTunes脚本库,但没有找到对我有用的东西。如果可以做到这一点,希望有人能指引我朝着正确的方向前进。感谢。

1 个答案:

答案 0 :(得分:1)

从iTunes获取曲目的基本方法是使用get every track。例如:

tell application "iTunes" to set topTracks to every track of playlist "Library" whose artist is "Golden Earring" and rating is 100

iTunes GUI中的评分是1到5星(或没有星星),但后端是0到100。将后端评级的评级乘以20(或者将GUI后端评级除以20的星级计数),零星等于0。

iTunes中有一个怪癖;添加and rating is 100会使查询变得更慢,就好像它正在对两个查询进行搜索并将它们放在一起,而不是做简单的(艺术家)然后是硬的(评级)。因此,在获得特定艺术家的所有五级曲目时,这可能会更快,更快:

tell application "iTunes"
    set artistTracks to every track of playlist "Library" whose artist is "Golden Earring"
    set topTracks to {}
    repeat with possibleTrack in artistTracks
        if the rating of possibleTrack is 100 then copy possibleTrack to end of topTracks
    end repeat
end tell

现在,如果通过“热门歌曲”,你的意思是顶级的 x 歌曲,那么就无法保证有足够的五个曲目可以达到 x 。所以如果你想要一个特定的数字的曲目,你必须获得5个曲目的评分,看看你是否有足够的,如果没有,抓住4个曲目,等等。这是一个如何做到这一点的例子;还有更多。你怎么做到这一点,部分取决于你如何定义“热门歌曲”。

tell application "iTunes"
    set desiredArtist to "chi coltrane"

    --get topCount tracks
    set topCount to 5
    set fiveStars to {}
    set fourStars to {}
    set threeStars to {}
    set twoStars to {}
    set oneStar to {}
    set noStars to {}
    set allTracks to every track of playlist "Library" whose artist is desiredArtist

    --collect tracks into bins according to star rating
    --this is much faster than doing five searches with "and rating is"
    repeat with possibleTrack in allTracks
        copy (rating of possibleTrack) / 20 as integer to starRating

        if starRating is 5 then
            copy possibleTrack to end of fiveStars
        else if starRating is 4 then
            copy possibleTrack to end of fourStars
        else if starRating is 3 then
            copy possibleTrack to end of threeStars
        else if starRating is 2 then
            copy possibleTrack to end of twoStars
        else if starRating is 1 then
            copy possibleTrack to end of oneStars
        else
            copy possibleTrack to end of noStars
        end if
    end repeat
end tell

--collect the top tracks
set topTracks to {}
getTracks(fiveStars)
getTracks(fourStars)
getTracks(threeStars)
getTracks(twoStars)
getTracks(oneStar)
getTracks(noStars)

--play the tracks, if any
if (count of topTracks) > 0 then
    tell application "iTunes"
        repeat with topTrack in topTracks
            set topTrackID to the persistent ID of topTrack
            play topTrack
            --wait until this song is no longer playing, then go to the next
            repeat while the persistent ID of current track is topTrackID
                --delay a tenth of a second to avoid too much of the next random track
                delay 0.1
            end repeat
        end repeat
    end tell
end if

--add more tracks from the given trackList, until topCount is reached
--within a list, the choice of which tracks to use to reach topCount is somewhat arbitrary
on getTracks(trackList)
    global topTracks, topCount
    if (count of topTracks) ≥ topCount then return
    repeat with possibleTrack in trackList
        if (count of topTracks) ≥ topCount then
            return
        end if
        copy possibleTrack to end of topTracks
    end repeat
end getTracks

选择 topTracks 列表中何时前进到下一首曲目的其他选项可能包括延迟the duration of topTrack as number,如果您有理由确定没有人会暂停或推进GUI;也可以设置一个在轨道发生变化时接收通知的处理程序。