Examine / manipulate all windows of all apps?

Only one of my two monitors is plugged into my UPS, so when the power goes out I want to be able to immediately move any windows on the blacked-out monitor over to the one that's still on, so I can do whatever hurried housekeeping is necessary before shutting down. I have a macro to do this for all windows of the frontmost application, but I'd like one that will do it for all windows of all running applications at once, rather than me having to manually cycle through active applications.

Here's the macro I have to make the frontmost app's windows visible. (Any window whose left edge is on the left monitor gets moved and slightly tiled. It doesn't need to be pretty, it just needs to work fast.) Is there any way to wrap this in a "for every running application" loop?

-- move all windows of current app to right monitor.kmmacros (3.3 KB)

Keyboard Maestro has not support for this, so you will have to use AppleScript or some other scripting to do it.

This should do the trick.
Just put this script in an Execute AppleScript Action.
I’ll publish a macro for this tomorrow.

Please let us know if this works for you, and/or if you have an further questions or suggestions.

<img src="/uploads/default/original/2X/9/9fa034801953dfe7d1745afcb7087c7530a06e23.gif" width="70" height="17"> 2017-08-17 18:25 CT

Just published the macro, that supersedes this script:

###[MACRO:   Move ALL Windows to Primary Screen](https://forum.keyboardmaestro.com/t/move-all-windows-to-primary-screen/7691)

Just posted this macro that should do the trick. It is an update to the above script.

###MACRO: Move ALL Windows to Primary Screen

Please let us know if this works for you.

This is terrific, thank you very much! I really appreciate all the helpful people on this forum.

I’m curious why you wrote it to ignore windows without titles? BusyCal lives on my secondary monitor, and its main window doesn’t have a title and thus doesn’t get moved. I’m never going to need to do anything with that window before shutting down, so that’s okay! But I wonder if any dialog boxes and such, that might be open and that I’d definitely want to interact with before shutting down, might also not have titles and thus would be missed. I can always edit the script, but maybe there’s a reason not to do so that I’m not seeing?

Usually windows without titles either aren’t visible to the user, or are some special window for UI purposes that are usually a part of another window. For example, Google Chrome shows the dropdowns for BookMark folders in a window with no title.

Mainly, it is just a habit I have when dealing with windows.
It will probably work OK for you to remove that test for windows.


if ((winTitle ≠ "") and (winTitle ≠ missing value) and ¬
  ((winLeft > primaryScreenRight) or ¬
    (winLeft < primaryScreenLeft))) then

--- WITH THIS ---

if (((winLeft > primaryScreenRight) or ¬
  (winLeft < primaryScreenLeft))) then

Let me know if that works OK for you.

@Shoshanna, I have changed my baseline script to be the above, without the tests for winTitle. It seems to work well so far. If I don’t find any issues, then I’ll update my macro/script.

Works fine – as I’d expect! (I also stripped out the extra set of parentheses that were left behind in the condition.) Thank you again.

@Shoshanna, sounds like your problem is solved.
But, just in case, I have updated my Macro to be more general in app/window search:

Well, we had a power outage this afternoon as I was working, and I happily triggered this macro -- and nothing happened. Now that the power is back I tried running the Applescript in Script Editor, and find that it throws this error:

Can’t get origin of {{0.0, 0.0}, {2048.0, 1129.0}}.

SCRIPT: Move Windows to Main Screen Ver: 1.0
Error Number: -1728"

JMichaelTX, the script you wrote is a bit beyond my Applescript debugging skills. If you or any other helpful person feels like being helpful again, I would be very grateful!

I just ran the script from the Macro Move ALL Windows to Primary Screen in both Script Debugger 7.0.7 (7A72) and Script Editor 2.9 (191) on macOS 10.12.6, and it worked fine for me.

Have you tried running it from the Macro?

If you can't get it working, please post the versions of KM and macOS you are running.

Hey @Shoshanna,

Have you tried reinstalling a fresh copy of the macro?


Thanks, JMichaelTX and ccstone. It's still not working.

I tried it from both KM (as macro) and Script Editor (as script) last night, and got the results I reported. This morning I downloaded and installed v2.0 of the macro (I'd been running v1.1, though property ptyScriptVer is still declared in it as "1.0" so that's what the error reported). But when I ran the new version from both KM and Script Editor again, I had the same results: no windows move, and the script reports

Can’t get origin of {{0.0, 0.0}, {2048.0, 1129.0}}.

SCRIPT: Move Windows to Main Screen Ver: 2.0
Error Number: -1728

I'm running Keyboard Maestro 8.2.4 and Mac OS 10.13.6 (High Sierra).

Maybe it is some strange window that is causing the problem.
If you could run the script in Script Debugger 7, but in the DEBUG mode where you can step through the script line-by-line to identify which window is causing the problem.

SD7 now comes with a 20-day free trial, and then will gracefully downgrade to SD7 Lite (which is still much better than Script Editor).

Script Debugger tells me that the script fails at the line

set primaryScreenRight to (x of origin of primaryScreenVisibleFrame) + (width of |size| of primaryScreenVisibleFrame)

with "Paused at exception: Can't get origin of {{0.0, 0.0}, {2048.0, 1129.0}}."

Investigating the variables in the panel on the right confirms that those coordinates are the value of the variable primaryScreenVisibleFrame.

FWIW, the main screen is my iMac, which is a 2017 iMac18,2, with a 4096x2304 Retina display. I bought it six months after you gave me this macro; the macro ran perfectly on my previous machine (a Late 2009 iMac10,1, 1920x1080 display), but it's likely that I've never previously tried to run the KM macro (or its Applescript) on this one, since we hadn't had a power failure in that time. Is the hardware the problem?

Well, I fixed the problem by the time-honored strategy of avoiding the problem! Once I realized that the script included a handy section marked "ALTERNATE METHOD Using KM" that avoids calling the "origin" function -- and once I added the missing lines to that section to define all four primaryScreen corners -- I swapped out the old method for that one, and now the script runs perfectly. (And a good thing, too, since we're about to get up to a foot of snow plus strong winds around here; another power outage may well be in my future...)

Thank you again for creating this script, JMichaelTX.

1 Like