How to favorite the current song in Music with AppleScript
You can favorite the song playing in Music from a script, a shortcut or a launcher. Here is what works, what does not, and the errors we hit building Likeit
Favorite the current song
tell application "Music"
set favorited of current track to true
end tell
Use false to remove it. Read the state with get favorited of current track. This property is called favorited. Older iTunes scripts used loved, which is why scripts from before 2023 may fail (see Love or Favorite)
Radio and streaming songs
A song from radio or the catalog is a URL track. Setting favorited to true on it works: a copy of the song appears in the Favorite Songs playlist a few seconds later. Setting it back to false on the stream item changes only a local value and nothing you see. To remove the favorite, clear it on the copy in Favorite Songs:
tell application "Music"
set n to name of current track
set a to artist of current track
repeat with nm in {"Favorite Songs", "Favourite Songs"}
try
set pl to playlist (contents of nm)
set found to every track of pl whose name is n and artist is a
repeat with c in found
set favorited of c to false
end repeat
exit repeat
end try
end repeat
end tell
The playlist name depends on the language and spelling, so the script tries both. Do not name a variable copies: it is a reserved word in the Music dictionary and raised error -1731 in our tests
Things you will run into
- The first call is slow. About 1.4 seconds for the first call after launch, around 27 milliseconds after that
- The Music window updates late. The value changes at once, the star in the Music window can take 5 to 11 seconds (details)
- Error -609, "Connection is invalid". It happens once in a while if Music closes during the call. One retry fixes it
- Do not launch Music by accident. A script that talks to Music starts it. Check that it is running first
Ratings are not the same thing
Setting rating fails with a permission error (-54) for Apple Music songs and streams. It works only for files that are in your library as files. Favorites are the part of Music that scripts can reliably change
Sandboxed apps
An app from the App Store runs in the sandbox and needs the Apple Events entitlement with a scripting target for Music. Likeit uses com.apple.Music.playback for the current track and com.apple.Music.library.read-write for the favorite flag. Inside the sandbox, use current track directly. Holding it in a variable and reading its name raised a privilege error (-10004) in our tests
Likeit does all of this for you behind one star in the menu bar