Add currently playing song to Apple Music using AppleScript Editor

I am trying to make an AppleScript that automatically adds the current track that I am listening to on Apple Music to my library.

What I have done so far is set the option "Add songs to Library when adding to playlist" to true and then used the following AppleScript code below:

tell application "iTunes"
    duplicate current track to playlist "New Songs"
end tell

What happens is it adds the song to the playlist and then it is automatically removed from the playlist and it is not added to my library.

When I add the song to the playlist manually, the track stays in the playlist and gets added to my Library.

How am I to get ScriptEditor to save the current track to my library?


I had the same question today. I don't know AppleScript (first time using it was to make this) so this code is probably very inefficient syntax-wise, but didn't want to risk breaking it by refactoring.

It turns out the only way to add a song to the library in every scenario using AppleScript is to force the application to go into the mini player and then use the "Add to Library" button in the menu bar. You can remove the mini player part of the script if you don't need this script to work with songs that aren't in playlists (e.g a radio).

tell application "System Events"
    set frontmostApplicationName to name of 1st process whose frontmost is true
end tell

tell application "System Events"
    tell process "iTunes"
        try
            tell menu bar 1
                tell menu bar item "View"
                    tell menu "View"
                        click menu item "Exit Full Screen"
                    end tell
                end tell
            end tell
        end try
    end tell
    tell process "iTunes"
        set frontmost to true
        try
            tell menu bar 1
                tell menu bar item "Window"
                    tell menu "Window"
                        click menu item "Switch to Mini Player"
                    end tell
                end tell
            end tell
        end try
    end tell
    tell process "iTunes"
        try
            tell menu bar 1
                tell menu bar item "Song"
                    tell menu "Song"
                        click menu item "Add to Library"
                    end tell
                end tell
            end tell
        end try
    end tell
    delay 0.5
    tell process "iTunes"
        set frontmost to true
        try
            tell menu bar 1
                tell menu bar item "Window"
                    tell menu "Window"
                        click menu item "Switch from Mini Player"
                    end tell
                end tell
            end tell
        end try
    end tell
end tell

tell application "iTunes"
    next track -- if you listen to another person's playlist and add the current playing track, sometimes apple music will completely stop playback (bug), so we have to skip song to avoid this
end tell

tell application frontmostApplicationName
    activate
end tell

this seems to work pretty well:

tell application "iTunes"
    duplicate current track to source "Library"
end tell

This works for me - loves the song and adds to library (I have a smart list to show loved songs, but they only appear if the song is in the library).

tell application "Music"
    if current track is not loved then
        set loved of current track to not loved of current track
        duplicate current track to source "Library"
    end if
end tell