Applescript to Heart or Love or Favorite song

Solution 1:

This is the script I use. It toggles the loved status of the current track.

tell application "iTunes"
    if loved of current track is true then
        set loved of current track to false
    else
        set loved of current track to true
    end if
end tell

It also works for Apple Music; just replace "iTunes" with "Music".

Solution 2:

I wrote a pretty hacky script that does the job for Apple Music tracks. It right clicks on the app icon in the dock, and selects the first option in the menu.

set current_app to current application
activate application "Dock"
tell application "System Events"
    tell process "Dock"
        set frontmost to true
        activate
        tell list 1
            perform action "AXShowMenu" of UI element "iTunes"
            delay 0.01
            repeat 1 times -- move down one menu item to the 'love' option
                key code 125 -- down arrow
            end repeat
            delay 0.1 -- 0.01 was too fast
            repeat 2 times
                key code 36 -- return key
            end repeat
        end tell
    end tell
end tell
activate current_app

(Based on this answer)

Again, this is super-hacky, but it works for me when I'm listening via "For You", which is the only time I usually "love" tracks.

Solution 3:

At the moment it doesn't appear to be possible to get or set properties on music played through Apple Music.

However you can heart a track that is stored locally or played from your iCloud Music Library.

tell application "iTunes"
    set loved of current track to true
end tell