如何用AppleScript获取Music/iTunes中待播放的后续曲目?
获取Music应用下一首播放曲目的AppleScript解决方案
你猜的没错,只靠曲目名称对比就是问题根源。播放列表里重复出现的同一首歌,每个实例都有唯一的标识——persistent ID,这才是区分它们的核心,而不是曲目名(毕竟名字可能完全一样)。
下面是可以精准定位当前曲目实例、获取下一首曲目的代码:
tell application "Music" -- 获取当前正在播放的曲目及其持久ID set currentTrack to current track set currentPersistentID to persistent ID of currentTrack -- 获取当前活跃的播放列表(正在播放的那个列表) set currentPlaylist to current playlist set allTracks to tracks of currentPlaylist -- 遍历列表定位当前曲目的位置 set trackIndex to 0 repeat with i from 1 to count of allTracks if persistent ID of item i of allTracks is currentPersistentID then set trackIndex to i exit repeat end if end repeat -- 获取并提示下一首曲目 if trackIndex > 0 and trackIndex < count of allTracks then set nextTrack to item (trackIndex + 1) of allTracks display dialog "下一首曲目:" & name of nextTrack & " - " & artist of nextTrack else if trackIndex is count of allTracks then display dialog "当前是播放列表的最后一首,没有下一首了" else display dialog "未找到当前曲目在播放列表中的位置" end if end tell
关键说明:
persistent ID:每个曲目实例在Music中的唯一标识符,哪怕是同一首歌多次添加到播放列表,这个ID也不会重复,是精准匹配的核心。current playlist:确保你操作的是当前正在播放的列表,避免拿错其他播放列表的曲目。- 遍历过程中对比
persistent ID,能准确找到当前播放的那个曲目实例,不会被同名曲目干扰。
内容的提问来源于stack exchange,提问作者user620316
相关产品推荐
相关产品推荐

