Tuesday, September 29, 2020

#1 2020-01-18 05:33:01 am

Jono
Member
Registered: 2008-07-08
Posts: 120

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.

Applescript:

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.

Applescript:

   duplicate current track to source "Library"

So I added it to my script:

Applescript:

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?

Offline

 

#2 2020-01-18 06:42:56 am

technomorph
Member
Registered: 2017-12-14
Posts: 164

Re: Add current track from Apple Music to playlist

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/ … u.php#1054


Applescript:


           
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

Offline

 

#3 2020-01-18 10:54:15 am

Jono
Member
Registered: 2008-07-08
Posts: 120

Re: Add current track from Apple Music to playlist

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

Applescript:

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

Applescript:

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»

Offline

 

#4 2020-01-19 12:38:40 am

technomorph
Member
Registered: 2017-12-14
Posts: 164

Re: Add current track from Apple Music to playlist

Try some track of source “library”

Offline

 

#5 2020-01-19 08:29:09 am

Jono
Member
Registered: 2008-07-08
Posts: 120

Re: Add current track from Apple Music to playlist

I've tried a couple of versions with 'some track of source "Library"', but unfortunately that doesn't work either neutral

Applescript:

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


Applescript:

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"

Offline

 

#6 2020-01-22 04:01:50 pm

Jono
Member
Registered: 2008-07-08
Posts: 120

Re: Add current track from Apple Music to playlist

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:

Applescript:


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 smile

Last edited by Jono (2020-01-24 04:48:04 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)