Auto-switching to and from the iTunes MiniPlayer

A couple of days ago, Dan Sturm had a great idea:

Ugh, all I want is for iTunes to automatically switch to the mini player when it’s not the active window, is that so hard? Jeez
  — Dan Sturm (@dansturm) Wed Aug 30 2017 2:44 PM

The solution I found wasn’t that hard, but it did take some messing around. And even though it’s not foolproof—as Dan discovered in his testing—it’s good enough for me.

First, let’s define what we want. Under normal circumstances, iTunes displays its regular window.

iTunes regular window

To save space while still displaying the current track, especially when iTunes is in the background, there’s also a MiniPlayer window,

iTunes MiniPlayer

which can be stretched vertically to show the album cover art, but I typically don’t do that. I like having the MiniPlayer down in the lower right corner of my screen, where I seldom have other windows to cover it.

You can switch between the regular window and the MiniPlayer using the Switch to/from MiniPlayer command in iTunes’s Window menu, or use the ⇧⌘M keyboard shorcut. But this is a manual operation that you have to do when iTunes is active. What Dan wants—and what I now realize I want—is for iTunes to make this switch automatically: going to the regular view when iTunes is put in the foreground and to the MiniPlayer view when it’s put in the background.

Keyboard Maestro has a way to trigger actions upon an application being activated and deactivated, and it’s pretty easy to come up with a macro that switches to the regular window upon activation.

From MiniPlayer macro

The trickier macro is the one going the other direction. You’d think the thing to do would be to just change the menu item to Switch to MiniPlayer, but all that does is set up an infinite loop between the two macros, probably because Keyboard Maestro activates iTunes to execute the menu command.

What I found worked was good old-fashioned UI scripting in AppleScript, the kind of thing I used to have to do before buying Keyboard Maestro:

To MiniPlayer macro

The script is

1:  tell application "System Events"
2:    tell process "iTunes"
3:      click menu item "Switch to MiniPlayer" of menu "Window" of menu bar 1
4:    end tell
5:  end tell

I think this works because iTunes isn’t being activated during the execution of the script.

I have both of these macros saved in a Keyboard Maestro Group that’s available in all applications, and they work fine. Mostly.

Dan found a problem almost immediately. If you switch quickly into and out of iTunes (or vice versa), you can generate an error:

Keyboard Maestro error message

iTunes apparently doesn’t get its menus set up fast enough and isn’t ready to execute the command when it’s called. Although I’d like to fix this, I don’t feel like putting in a lot of time on something that won’t be a benefit under normal use. Other than stress testing, I don’t see why anyone would want to switch into and out of iTunes that quickly. As I told Dan,

@dansturm This is my version of “you’re holding it wrong.”
  — Dr. Drang (@drdrang) Wed Aug 30 2017 11:45 PM