Keyboard shortcut to 'Like' currently playing song in iTunes 12.2?

I’m using the:

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

script so that I can love tracks with a shortcut. Unfortunately, I don’t really know script. Is it possible to make the script such that it will only do this for tracks that are not already loved? Really this script seems to toggle loved/unloved, and so it goes both ways. I’d like it to not go both ways in case I love a song that I had previously loved but forgot…

tell application "iTunes"
    if player state ≠ stopped and not loved of current track then
        set loved of current track to true
    end if
end tell
1 Like

I notice that current track chokes if nothing is playing.

error "Can’t make «class pLov» of «class pTrk» of application \"iTunes\" 
into type boolean." 
number -1700 from «class pLov» of «class pTrk» to boolean

In JS for Automation you might handle that with something like:

(function () {
    'use strict';

    var it = Application('iTunes'),
    
        // track is only defined if something is playing
        track = it.playerState() !== "stopped" ? (
            it.currentTrack()
        ) : undefined;

    // Either there is no track, or there is and we ensure that its loved.
    return !track || (track.loved = true);
})();

Good point. I modified my AppleScript example to take this into account.

tell application id "com.apple.iTunes"
	if not loved of current track then set loved of current track to true
end tell

I’m back to Apple Music (had a free Spotify Premium subscription).

This works really well. I also adapted it to dislike songs when needed, it also skips to next song. Thanks!

tell application "iTunes"
	if player state ≠ stopped and not loved of current track then
		set disliked of current track to true
         next track
	end if
end tell

Just figured I'd chime in here with a version I created for the (new iTunes) Music app:

image

tell application "Music"
	if player state ≠ stopped then
		if (not loved of current track and not disliked of current track) then
			set loved of current track to true
			return "Loved"
		else if loved of current track then
			set disliked of current track to true
			return "Disliked"
		else
			set disliked of current track to false
			set loved of current track to false
			return "Cleared"
		end if
	end if
end tell

In KM I just have this on a single hot key to loop through the different settings, and it shows the status via a notification (display results briefly).

1 Like

Brilliant, thanks Jon! This works like a charm.

FYI, I edited this slightly as follows:

tell application "Music"
	if player state ≠ stopped then
		set currenttrackname to name of current track
		if (not loved of current track and not disliked of current track) then
			set loved of current track to true
		
			display notification currenttrackname & " Loved" with title "Apple Music"
		else if loved of current track then
			set disliked of current track to true
			display notification currenttrackname & " Disliked" with title "Apple Music"
		else
			set disliked of current track to false
			set loved of current track to false
			display notification currenttrackname & " Cleared" with title "Apple Music"

		end if
	end if
end tell

and, FWIW, a JavaScript for Automation variant (assuming KM 11)

Apple Music -- toggle Like - Dislike - Clear for current track.kmmacros (3.1 KB)


Expand disclosure triangle to view JS source
const
    withTitle = "Apple Music",
    options = ["favorited", "disliked", "cleared"],
    music = Application("Music");

return Object.assign(
    Application.currentApplication(), {
        includeStandardAdditions: true
    }
)
.displayNotification(
    "stopped" === music.playerState()
        ? "Music player stopped."
        : (() => {
            const
                track = music.currentTrack(),
                newOption = options[
                track.favorited()
                    ? 1
                    : track.disliked()
                        ? 2
                        : 0
                ];

            return (
                newOption !== "cleared"
                    ? track[newOption] = true
                    : track.disliked = false,
                [
                    newOption[0].toLocaleUpperCase(),
                    newOption.slice(1)
                ]
                .join("")
            );
        })(),
    {withTitle}
);
1 Like