Reputation: 445
I'm trying to write AppleScript that will find which tracks Music will play after the current track, that is, the upcoming tracks.
I can easily find all tracks of the current playlist, look for the current track in that list of tracks, and grab the following tracks. But this fails when a track appears more than once in the current playlist, because I don't know which instance of that track is the one being played! Is there some way of figuring this out?
(My current code compares track names, not tracks themselves, so perhaps this is what I'm doing wrong. If a track appears multiple times in a playlist, are the instances somehow different?)
Upvotes: 0
Views: 60
Reputation: 445
Found it after some fiddling around: Yes, each instance of a song (whether in a playlist or the main library) is a separate track object. It doesn't work to compare track objects, but you can compare the IDs of the track objects. So the following code returns the name of the current track and the names of the 4 upcoming tracks, and works correctly even if a song appears multiple times in a playlist:
tell application "Music"
set collecting to false
set itemcount to 0
set upcoming to {}
repeat with atrack in (tracks of current playlist)
if id of atrack = id of current track then set collecting to true
if collecting then
copy name of atrack to end of upcoming
set itemcount to itemcount + 1
if itemcount > 4 then exit repeat
end if
end repeat
end tell
return upcoming
Upvotes: 0