iTunes: Add track to beginning/top of specified playlist?

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?

Have a look at the iTunes dictionary.

For most things it uses “INDEX”

index integer The index of the item in internal application order.

and it says it’s inherited from ITEM

also check out https://dougscripts.com/itunes/index.php
super great source for info.
Look under the miscellaneous parts and examples code.

hmmm after further reading you might not be able to as it says the index is only available
via get.

What you might have to do is

  • make a new temporary playlist in applescript.
  • duplicate the track you want 1st to it
  • duplicate the rest of the other playlist to it
  • then either deal with having a new playlist

OR

  • create an array and add your new 1st track to it
  • add the tracks from the other playlist
  • remove the tracks from the other playlist
  • add the tracks from your array back into to the playlist

also maybe investigate the fixed indexing property of your playlist

also try
“front”
“before item 1 of”
“index 1 of”

That’s great, thanks a lot. Lots of things to look at and try there :slight_smile:

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

Thanks for the help in cleaning this up Yvan :slight_smile:

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!