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.