【问题标题】:Play TOP songs of artist in AppleScript在 AppleScript 中播放艺术家的 TOP 歌曲
【发布时间】:2017-01-16 14:26:36
【问题描述】:

我正在尝试制作一个AppleScript,它可以播放某些艺术家的热门歌曲。我通过ScriptEditor 看到了一个 iTunes 脚本库,但没有发现任何对我有用的东西。希望有人可以向我转发正确的方向,如果这种情况是可能的。谢谢。

【问题讨论】:

  • 你如何定义“热门歌曲”?播放次数最多的歌曲?
  • @oa- iTunes 中有专门的“热门歌曲”段落

标签: applescript itunes


【解决方案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 得到后端评分(或将后端评分除以 20 得到 GUI 星数),零星等于 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 首的歌曲,则无法保证有足够的评分 5 首歌曲达到 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 中暂停或前进曲目;也可以设置一个处理程序,当轨道发生变化时接收通知。

【讨论】:

  • 非常感谢您的回答。但是,我能否仅从 Apple Music 列表中获取不从我的资料库中获取的曲目?
  • 我怀疑不是;但是,您的第一步必须更清楚地定义“某些艺术家的热门歌曲”的含义。也就是说,要“播放”一首歌曲,必须满足哪些标准?提供一个示例以及您为达到此示例所采取的步骤可能会对您有所帮助。一旦你这样做了,使用 iTunes 或使用其他机制的解决方案可能会变得很明显。
  • iTunes 有一个特殊的段落“热门歌曲”,我想得到这个列表。但如果能得到某个艺术家的随机歌曲列表也很好。
  • 您需要做的是更准确地了解您想要做什么,以及它与您所看到的内容之间的关系。通常,对于初学者来说,具体描述另一个人必须做什么来复制你想要的东西会有所帮助。他们需要采取哪些步骤才能找到“热门歌曲”;他们需要采取哪些步骤来找到“某个艺术家”,以及他们需要采取哪些步骤来“播放”那首歌;那么,结果如何呢?在这种情况下,他们听到了什么?了解这些步骤后,您就可以开始思考如何将这些步骤转化为计算机可以执行的操作。
猜你喜欢
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多