Condition based on current Apple Music Playlist

I want to have a macro that Play/Pause if Apple Music is in a playlist (any playlist), and that if will switch to "New Daily Music" Playlist if no playlist is open (when apple music start there is no playlist open so play/pause has no effect)

It doesn't seem possible today, is it?

I was trying to add a "If - Then" actions but I don't see any condition that is related to apple music.

Is there any other way to achieve this?

You may be able to use Shortcuts, but I don't have that here. So here's an AppleScript that should do what you want (tested on iTunes rather than Music, but should work):

tell application "Music"
	try
		get name of current playlist
		playpause
	on error
		tell playlist "New Daily Music" to play
	end try
end tell

...which works because trying to get the name of the current playlist when one isn't set will throw an error.

Pop the above into a KM "Execute an AppleScript" action and you should be golden, no need for a KM "If Then Else" unless there are other things you want to do too.

At the end I used two actions

  • Action 1: Execute AppleScript
tell application "Music"
	if (exists current track) then
		log name of current track as text
		return
	end if
	
	activate
	reveal playlist "New Music Mix"
       set shuffle enabled to true
	set shuffle mode to songs
       delay 2
	play  first track of playlist "New Music Mix"
	pause
	
end tell

the applescript check if there is current track if there is it end the script right there.

If there is no "current track" we need to activate the application (Apple Music), because otherwise the play/pause does not work if Apple Music was started by this script.

We "open" the playlist called "New Music Mix", this does NOT change the current track.

So I need to play and then pause to select the track.

  • Action 2: Play/Pause Current Track

I gave this a quick test and it seems to do your test correctly...

As for getting Music to play a playlist, I haven't given that any thought yet.

I noticed that could achieve what I wanted without AppleScript

  • If - Then condition on the %MusicPlayerState% token , if "stopped" means that there is no current track otherwise the state would be "paused".
  • So if "stopped" or "not running" I use the "Play Playlistt" action.
  • if "playing" or "paused" then I execute the "Play/Pause Current Track" action

Still if I want to set the shuffle mode (songs, albums, groupings) then I'm forced to use Execute AppleScript action for just that:

1 Like