How to add a track to Library in Apple Music using Apple Script

I searched through a few posts online and most of them say to use this:

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

…but I get the following error:

"Music got an error: Can’t set source \"Library\" to current track." number -10006 from source "Library"

Did Apple change/remove adding a track to the Library? I can’t find any update on this and Apple’s docs on this are nonexistent.


The source is the source. Nobody don’t add songs there (except of application itself, which add current track automatically to “Library” source). From there we take. It’s another matter if you want to add the current track to the user playlist (other is the library playlist, managed by, as I sad):

property FavoritePlaylist : "Playlist 2"

tell application "Music"
	if player state is stopped then return
	set dbid to database ID of current track
	if not (exists playlist FavoritePlaylist) then make new user playlist with properties {name:FavoritePlaylist}
	if exists (some track of playlist FavoritePlaylist whose database ID is dbid) then return
		duplicate current track to playlist FavoritePlaylist
	on error
		display dialog "Could not add the track to playlist \"" & FavoritePlaylist & "\"." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
	end try
end tell

Note: other source, managed by is “iTines Store” source. Also, the following script is quite legitimate, but meaningless since the track you are duplicating already exists at the destination:

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

Duplicate is for adding an existing track in your library to a playlist.

Sounds like your looking for Add Track
Which will add a file to your library from a
Path or URL

What do you mean by “add track to Library”? Do you want to import files to Music or add them to Music’s “Library” “playlist”? If the latter then any currently playing track is in “Library” by default. “Library” is contained by the source with the same name since it’s one of the main default playlists. Those playlists contain user playlists you can create, add tracks to, duplicate, modify, delete etc.
If you want to import files from Finder then you simply pass files as the list of alias references to add command as a direct parameter like so:

tell application "Music" to add alias_references_list

If you want to move those tracks to a specific playlist on import then the same command will be slightly different by adding the optional parameter to:

tell application "Music" to add alias_references_list to playlist "Best from Cannibal Corpse"

I’m having the exact same problem. Can someone help me? In iTunes, there is a critical feature to “Add to library.” This has nothing to do with importing files from your machine. This has to do with adding something that you are listening to on Apple Music, a subscription music service that is built into the iTunes application. If one does not add a track to the library, they can’t access it for later listening, even if they “love” it. Hence, a script to “Add track to library” would be exceptionally helpful. No one on the web seems to have been able to find a suitable solution. Or, the solution that has been proposed is no longer compatible with the latest version of iTunes.

See picture here:

As I sad already, importing songs to Library playlist (source) is controlled fully by interface. It isn’t scriptable at least for now. Adding songs to Library playlist (source) requires GUI scripting of “Import…” menu or GUI scripting of “Add to library” menu when iTunes Store is open.

The add and duplicate commands of Music dictionary is only for adding existing in the Library songs to user playlists. So, to solve your problem, forget for them entirely and go to GUI scripting of indicated Music controls.

NOTE: to add song from hard disk to Library playlist, we can avoid GUI scripting, because as I sad already above, adds automatically every currently played song from hard disk:

set theAudio to choose file of type ""

tell application "Music"
	open theAudio
end tell

As for the iTunes Store, I can’t test and play with it, because it provides payed things, and not some test things for free at all.

set f to alias "Musca:New Music:Marisa Monte - Portas (2021)_flac:16. Pra Melhorar.m4a"

tell application "iTunes"
	open f
end tell

Voila! Imported!