从 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 中暂停或前进曲目;也可以设置一个处理程序,当轨道发生变化时接收通知。