I’m trying to create a script that will add the song that’s currently playing in iTunes to the beginning of a specified playlist.
The script below adds the currently playing song to the specified playlist, but always adds it add the bottom.
tell application "iTunes"
duplicate current track to beginning of user playlist "My Playlist"
end tell
Whether I specify ‘track to beginning of user playlist’, 'track to end of user playlist; or just ‘track to user playlist’ it always adds the track to the bottom of the playlist.
I’m guessing I’m not doing it correctly (rather than a bug with iTunes).
Does anyone know how to get this to work as I’m wanting?
I managed to create a script for this that works, so for anyone else trying to do the same things I’m adding it here.
I originally created a script that would create a new temporary playlist, add the current track to that playlist, add all the other tracks from my chosen playlist (‘My Playlist’) to the temporary playlist, delete my original playlist, then rename the temporary playlist to the name of my original playlist (‘My Playlist’).
In the end I went with a different version that adds the currently playing track to my playlist (at the bottom), then move all the tracks above that below the track.
Although 'duplicate current track to beginning of user playlist “My Playlist” ’ and 'move current track to beginning of user playlist “My Playlist” ’ compile OK they don’t work (bug?). But using ‘end’ does work, so I ended up using that as a workaround.
Here’s the script I ended up with:
tell application "Music"
set thePlaylist to user playlist "My Playlist"
(* Get currently playing track *)
set trackName to name of current track
set artistName to artist of current track
try
duplicate current track to source "Library" -- Add track from Apple Music to local library
end try
delay 4 -- Allow time for track to be registered with the local Library
(* Copy track to playlist *)
set foundTracks to (every track of library playlist 1 whose artist is artistName and name is trackName)
repeat with theTrack in foundTracks
duplicate theTrack to thePlaylist
end repeat
(* Move the track to top of playlist (move other tracks to bottom) *)
set trackCount to get count of tracks of thePlaylist
set trackCount to (trackCount - 1)
repeat trackCount times
set theTrack to track 1 of playlist thePlaylist
move theTrack to end of playlist thePlaylist
end repeat
end tell
Hello
As I can’t run Catalina I can’t test but at first look your script isn’t using consistent syntax.
Sometimes it speak to playlist thePlaylist and sometimes it speak to user playlist thePlaylist.
It seems too that there is no need to trigger the playlist by it’s name as, on entry, you get a reference to the playlist itself.
tell application "Music"
set thePlaylist to user playlist "My Playlist"
(* Get current playing track *)
set trackName to name of current track
set artistName to artist of current track
duplicate current track to source "Library" -- Add track from Apple Music to local library
(* Copy track to playlist *)
delay 4 -- Allow time for track to be registered with the local Library
set currentTrack to (some track whose name contains trackName and artist is artistName)
duplicate currentTrack to thePlaylist
(* Move the track to top of playlist (move other tracks to bottom) *)
--set thePlaylist to name of thePlaylist
set trackCount to get count of tracks of thePlaylist -- was : user playlist thePlaylist
set trackCount to (trackCount - 1)
repeat trackCount times
set theTrack to track 1 of thePlaylist -- was : playlist thePlaylist
move theTrack to end of thePlaylist -- was : playlist thePlaylist
end repeat
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 23 janvier 2020 11:03:05
I think when I was trying to get the script working I was trying different things to differentiate between ‘Apple Music playlists’ and my own ‘user playlists’.
But I’ve just tried your script and it works for me. Thanks again!