Add songs to Library in Music?

I've been searching for this for a few days now and haven't been able to find an answer.

I use BetterTouchTool to customize my touchbar, and I've added an action that will love a song in Apple Music like so:

	if is_app_running "Music" then
		tell application "Music"
			try
				if current track is loved then
					return "loved"
				else
					return "love"
				end if
			on error
				return ""
			end try
		end tell		
	else
		return ""
	end if
end tell

This works, but I realized that having a Smart Playlist of Loved songs means nothing if the songs aren't also in your Library.

So, I tried figuring out how to also add songs to my Library when they're loved.

I tried this:

	try
		duplicate current track to playlist "Library"
	on error
		duplicate current track to source "Library"
	end try
end tell

And it worked sometimes. I had to have the two versions in there for playlist and source because I found one worked for the auto-generated playlists Apple Music makes (like the "New For You" weekly playlist) versus songs from albums or your radio playlist.

This is very unreliable. Sometimes it works, sometimes it doesn't. Honestly, most times it doesn't.

It's hard to find any real documentation on how to work with Apple Music with AppleScript. I've gone through the Window -> Library view in the Script Editor and can see the list of possible actions and objects, but there's no direction on how they interact. For example, I would think you could simple do add current track to library playlist but that doesn't work at all. Any time I've tried using add with a track, I get an error that the track "doesn't understand the 'add' message".

Is there a simple, consistent way to add songs to my Library through AppleScript? Maybe bonus points if it's an add instead of a duplicate? If I manually click and add a song to my Library, it's simple added – but with this AppleScript, I have to duplicate it instead of simply adding.

Any help would be appreciated!

Hello @coreymcollins,

Thank you for this question about adding a track to the library in the macOS Music app using AppleScript.

We have indeed identified an issue with the Music app's support for AppleScript in this specific area.

We will continue investigating this issue, and hope to correct it in a future version of macOS.

I hope this helps.

Best regards,

I'm also having troubles with this. I'm glad @JoeKun and the Music team are working on the issue. This is what I have found so far:

  • for tracks in AM's playlist, when the playlist is not added to library: duplicate current track to source "Library" or duplicate current track to first source works
  • for tracks in AM's playlist, when the playlist is added to library (in the sidebar): cannot add track to library, we can only add it to an user playlist with duplicate current track to user playlist "name of user playlist"; running duplicate current track to first playlist of first source succeeds but Music hangs needing a force quit (and at the next start the track is not added)
  • for tracks in AM's radio or with autoplay: cannot add track to library nor to user playlist; running duplicate current track to first playlist of first source succeeds but the track is not actually added, same if trying to add it to playlist: the command runs, but the track is immediately removed

I hope that this information will be useful and we will have a fix soon!

@JoeKun I'd like to leave more info that I've discovered in the previous days

The "Add to playlist" commands from the Shortcuts application in Monterey do not seem to work either. I've setup a simple shortcut to add the current track library but it does not work.

I've also tried the Media Player API from Catalyst with MPMediaLibrary.addItem and that works perfectly, except it's hard to get the product ID for the current track. Do you know if there's an easy way to fetch the identifier for an MPMediaItem? As a side note, MPMediaPlayer.currentItem always returns nil when playing a track not saved in the library, is it a Catalyst bug?

The ideal solution in the future would probably be to port MPMediaPlayer to non-Catalyst macOS with its full feature set, including MPMediaLibrary.

Hello @eighty_six,

Please file a new ticket on Feedback Assistant including your suggestion of increasing our support of Media Player APIs for non-Catalyst macOS.

Best regards.

Thanks,

Hi, I have come across this issue too.

Something to note: I got it working yesterday (I was playing music from "Tim Jones' radio station") and the code:

worked, then I restarted my MacBook, I got the error. So I would also like to stay in the loop regarding this. And also wondering why this works sometimes so I could hopefully create a workaround. ideally, im trying to write a code that allows me to add a song to a playlist, but first it checks if its in my library with a search function (or if it is URL Track), if it can't find it, it adds the track to my library first, then rates/adds to playlist.

Very frustrating as well

I FIGURED OUT A WORKAROUND!!! Its silly, but it works...

basically you can do this with system events and the MiniPlayer...

	tell application "System Events"
		tell its process "Music"
			try
				click menu item "Add to Library" of menu "Song" of menu bar 1
			on error
				keystroke "m" using {command down, shift down}
				delay 1
				click menu item "Add to Library" of menu "Song" of menu bar 1
				keystroke "m" using {command down, shift down}
			end try
		end tell
	end tell

So the background for this is I have a rate/add song to playlist function which, as we all know, doesn't work when you're playing from a URL track. I have hotkeys setup through BTT to rate [⌥ + 1-5] or add to a playlist [⌥ + z] which brings up a menu to add to.

Basically, my code now checks if the song is in my library via search function:

	tell application "Music"
		set searchResults to {}
		set cname to ctrack's name as text
		set cartist to ctrack's artist as text
		set calbum to ctrack's album as text
		try
			set searchResults to (every track of playlist "Library" whose name contains cname and artist contains cartist and album contains calbum)
		end try
	end tell
	return searchResults
end checkInLibrary

If it's not in my library, it adds it through the first function (which is probably very inefficient) then performs the rate/add to playlist functions.

I REALLY HOPE THIS HELPS SOMEONE because I have been going through all of the hoops to make this work...

@JoeKun any update about this? I have literally 10k users waiting for this feature (Music extension for Raycast) but seems to be very inconsistent.

I'm currently trying via

tell application "Music" to duplicate current track to source "Library"

and if this throws error I run the following as fallback

tell application "Music" to duplicate current track to library playlist "Library"

Sometimes it works, sometimes it doesn't. Why there's no documentation about this?

https://github.com/raycast/extensions/issues/4145

Apple better just makes a big button that Loves and adds the song automatically instead of hiding everything in their tiny menus.

Because I have no understanding in adding code to shortcuts, I found another solution and it works pretty well so far.

Shortcuts doesn't have any "Love" action, so I used Automator to record and run my mouse clicks. You can run it from a Macbook Touch Bar or by tapping the current application and clicking Services/General/"Name of your Quick Action" The image below shows the actions.

As for recording the "Watch Me Do", I did:

  • Tap the Music window
  • Left click the song that is currently playing
  • Tap Love
  • Left click the song that is currently playing
  • Tap Add to Library

I got a warning that I needed to allow "ServicesUlAgent" to control my computer in System Settings/Privacy & Security/Accessibility. If you don't have a Touch Bar and want to run the Quick Action from any application, you have to allow all those applications to control your computer as well. I have no idea how else to run a Quick Action or Automator Script.

Hope this is helps someone.

Add songs to Library in Music?
 
 
Q