Add current track from Apple Music to playlist

I have a simple script that adds the track currently playing in Apple Music to one of my playlists.

tell application "Music"
	set loved of current track to true
	
	duplicate current track to user playlist "My Playlist"
end tell

But if I’m playing a track in Apple Music that I haven’t added to my own playlists previously I get the following error:

error “Can only duplicate subscription tracks to library source.” number -1708

I read somewhere else that you have to add the track to your library first.

	duplicate current track to source "Library"

So I added it to my script:

tell application "Music"
	set loved of current track to true
	
	duplicate current track to source "Library"
	duplicate current track to user playlist "My Playlist"
end tell

But even after adding this to my script I still get the same error. Does anyone know how to make this work properly and add a track from Apple Music to one of my playlists?

You may need to then get the track from the library now.
As you are still trying to add “current track”.
What you may want to do is get the persisentent ID of the track.
The after you add it to the Library.
Create a newTrackRef and set it to something like :
library track with Persistent ID.

Here’s an example from Dougscripts
https://dougscripts.com/itunes/scripts/missingmenu.php#1054


			
set aTrack to current track

set curTrack to (some track of library playlist 1 whose persistent ID is (get persistent ID of aTrack))
				
duplicate curTrack to thePlaylist

Thanks for the help, I’ve tried a couple of versions.

tell application "Music"
	set thePlaylist to user playlist "Test Playlist 1"
	set aTrack to current track
	
	duplicate current track to source "Library"
	
	set curTrack to (some track of library playlist 1 whose persistent ID is (get persistent ID of aTrack))
	
	duplicate curTrack to thePlaylist
end tell
tell application "Music"
	duplicate current track to source "Library"
	
	set thePlaylist to user playlist "Test Playlist 1"
	set aTrack to current track
	set trackPersistentID to (get persistent ID of current track)
	
	set curTrack to (some track of library playlist whose persistent ID is trackPersistentID)

	duplicate curTrack to thePlaylist	
end tell

But with these I get the following error:

error “Can’t get some track of library playlist.” number -1728 from some «class cTrk» of «class cLiP»

Try some track of source “library”

I’ve tried a couple of versions with ‘some track of source “Library”’, but unfortunately that doesn’t work either :expressionless:

tell application "Music"
	set thePlaylist to user playlist "Test Playlist 1"
	set aTrack to current track
	
	duplicate current track to source "Library"
	
	set curTrack to (some track of source "Library" whose persistent ID is (get persistent ID of aTrack))
	
	duplicate curTrack to thePlaylist
end tell

For the above I get the following error:

error “Music got an error: Can’t get persistent ID of URL track id 70477 of playlist id 70470 of source id 70.” number -1728 from persistent ID of URL track id 70477 of playlist id 70470 of source id 70

tell application "Music"
	duplicate current track to source "Library"
	
	set thePlaylist to user playlist "Test Playlist 1"
	set aTrack to current track
	set trackPersistentID to (get persistent ID of current track)
	
	set curTrack to (some track of source "Library" whose persistent ID is trackPersistentID)
	
	duplicate curTrack to thePlaylist
end tell

And for this one above I get the following error:

error “Music got an error: Can’t get some track of source "Library" whose persistent ID = "763A68CB227D148B".” number -1728 from some track of source “Library” whose persistent ID = “763A68CB227D148B”

After trying quite an few different things I managed to get this working. So for anyone else who wants to do this, here the script:


tell application "Music"
	set thePlaylist to user playlist "My Playlist"
	
	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
end tell

But if anyone can see a way of improving the script please let me know :slight_smile: