Open and move windows between displays automatically

When I was visiting my pal Myke Hurley in London last year, I noticed that he had a second display to the right of his main one. It was large and mounted vertically, and when he recorded a podcast, he kept all his recording information over there. I liked the idea of monitoring my recordings while they were going on, just for extra peace of mind, and a second display seemed like the right way to do it.

If only I had a second display! But I do, of course, because my main Mac is now a MacBook Pro. So I’ve taken to recording podcasts using the “Hurley Method,” with my Audio Hijack windows positioned on the laptop display, off to my left.

It’s nice, but if you know me, I quickly became frustrated by the number of clicks and drags I needed to perform every single time I record a podcast: Open up my laptop, open Audio Hijack, open the list of recording sessions, double-click on the one I’m using right now, then drag the window over to the laptop display.

So today I automated it.

None of my automations work if I haven’t opened the laptop, and while it sits next to my Mac in a nice stand from Twelve South, it’s closed most of the time. So my first step before moving windows to the laptop display is to make sure that the display is available! Fortunately, there’s a command-line command for that:

system_profiler SPDisplaysDataType

If you want, you can simplify the output by piping it through a grep command like system_profiler SPDisplaysDataType | grep -B1 "Resolution", but all I really want to do is search for the text string “Built-in”, which indicates that the laptop is open and its built-in display is running.

I automate most of my recording stuff using Keyboard Maestro macros triggered by an Elgato Stream Deck. So I built my automation into an existing macro in Keyboard Maestro, just adding a new if/then statement: If the calculation `SCREENCOUNT() > 1 returns true.

Then the macro runs the shell script, saves that to a local variable called LocalScreens, and it’s time for one more if/then statement, to ensure that this part of the macro only runs when more than one screen is attached and when the internal MacBook Pro display is active.

Unfortunately, the next step requires automating Audio Hijack, and while that app is very scriptable via JavaScript, and controllable via Shortcuts (or a plain text file?!), there’s no direct method of automating it from within Keyboard Maestro. So I created a basic Shortcut containing the JavaScript required to tell Audio Hijack to open a specific session by name, with the name passed to the Shortcut as input:

Then it’s a one-step item in Keyboard Maestro to run that shortcut with the name of my Audio Hijack session as the input:

(I wish this was cleaner and I didn’t need to run a shortcut. The alternative is to literally write out a text file containing the JavaScript and run it via a shell script. Is that cleaner? Doesn’t feel like it, but it’s more self-contained and doesn’t rely on the good graces of Shortcuts.)

Now that I know that my MacBook Pro display is open and my Audio Hijack session window is open, it’s a one-step command in Keyboard Maestro to make the move:

This one’s a little tricky. I have to use the Move and Resize Window command, but instead of standard numerical values, I need to use custom “smart” values that Keyboard Maestro supports. In this case, horizontally it’s SCREENVISIBLE(Internal,MidX)-600 —essentially, 600 pixels to the left of the midpoint of the internal display—and vertically it’s SCREENVISIBLE(Internal,MidY)-250—250 pixels up from the midpoint of the display. You can play around with the dimensions until the window ends up in the right spot, and of course, at the right size.

And that was it. Now, when I press my recording button on my Stream Deck, if my laptop display is open, the session window will automatically pop open on my laptop display. I sure do like it when things happen on my computer automatically.