AppleScript: How to add currently-playing Apple Music song to a playlist?

Solution 1:

I've been cracking my head on this one as well and currently, there doesn't seem to be any way to add Apple Music tracks to a playlist. It's kinda unintuitive that there isn't a default way in AM to access all loved songs, but here we are. AM is getting better surely but slowely, so fingers crossed.

What I've done as a workaround in the meantime, is added a functionality that saves the song into a text file that is synced to iCloud. That way I can manually enter the songs whenever I have some time to spare and I can do it both on my iMac and Macbook.

tell application "iTunes"
    set loved of current track to true
    set songname to current track's name
    set songartist to current track's artist
    set textpath to "~/Library/Mobile\\ Documents/com~apple~CloudDocs/AppleMusicToDo.txt"

    do shell script "echo " & quoted form of songartist & " - " & quoted form of songname & " >> " & textpath

end tell

Solution 2:

It seems your code works if you use it with songs that are already in your library. Therefore if you use some AppleScript code to add the song to your library beforehand, your code should work. Here's some AppleScript I came up with to add a song to your library: Add currently playing song to Apple Music using AppleScript Editor