2

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'm not personally an Apple Music user so don't have any experience with using it but I'd love to help! Just to clarify, when you say "...adds the current track that I am listening to on Apple Music to my library." do you mean that it saves the song for offline listening to your library or that it simply adds it to a playlist? – Tom Aug 1 '18 at 18:17
  • @Tom I mean that I want the song to be saved to my library. I don’t want it saved for offline. – iProgram Aug 1 '18 at 19:20
  • I've just tried out the script you pasted and it works perfectly but only with my local iTunes library. I apologise that I am unable to provide any more help as I don't personally have Apple Music so am unable to replicate the issue and attempt to find a solution as a result. Best wishes! P.S. Just a hunch but maybe the issue has something to do with Apple not wanting their media being controlled by scripts or it could just be a bug... – Tom Aug 1 '18 at 22:49
  • Take a look here hints.macworld.com/article.php?story=20090709085206245 maybe you can rework this script to fit your needs – konqui Oct 12 '18 at 6:39
4

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
2

this seems to work pretty well:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .