Minimize all windows on current/main screen only. How?

This feels like it should be easy, but I can't figure it out.
How could this be accomplished?

I simply want to trigger an action to minimize all windows on my main center screen (I have 4 monitors). All other screens must not change.

Thanks for any help!

According to this thread you are asking for something very difficult. The thread says, "there is no convenient way to determine what windows are open on a given screen."

However one possible alternate solution is to simply use the KM Find Image macro to click on the Minimize Window button for each app on a given screen. I'm having mixed success with this approach due to all the false positives, due to the fact that the minimize button can be either yellow or grey for the frontmost visible window. If I can get it to work reliably I'll post it here.

OKAY! I got it to work reliably. You will have to change the image wells. One more thing: some apps use custom colours on their title bar, (eg, Home) which means my macro will need to add a new Find Image for each new colour you have on your apps.

The reason I use the SECONDS() function is because the macOS GUI animation for "minimize window" may cause delays in being able to see the buttons. So it keeps trying until one second of not being able to find any windows passes.

EDIT: Minor change: in the above code, replace "all screens" with "main screen."

1 Like

wow, thanks for the effort. I had no idea this is so tricky. would you be ok with sharing this macro? I have experimented with 'found image' before as well, with mixed results.

If you meant, "do you have permission to use it?" then yes, use it all you want.

If you meant, "can I upload the macro for you?" then that might not work for you, because the macro contains "captured image wells" and you will have to create those images manually to get this macro to work. So you might as well write the macro yourself. You should look at my image wells and manually capture them yourself using CMD--SHIFT-4 or CMD-SHIFT-5, which are macOS shortcuts for taking images from the screen.

Also, I'm away on vacation shortly, so if you have further questions you may need to ask others on this website.

What does "on my main centre screen" mean? Would it include a window that is mainly off that screen but still has a bit on it, a window that is mainly on the center screen but has a little bit off it, or must the window be entirely and only on the centre screen?

And is the centre screen your "main" screen, i.e. it has the primary menu bar?

@Airy's macro will work for some of these cases and, as he says, there's no simple "all the windows on this screen" token. But what you can do is go through each window of each app and compare that window's frame to the rectangle of the screen.

Thank you for the explanation and permission. I have seen in this helpful forum that people often share their solutions/macros for others to adjust and I was hoping you could do this. If you are not ok with that, no problem! I was worried, I would get some details wrong in copying it.

I have 4 monitors. 3 show different information/windows permanently and I want these windows to stay where they are. My main monitor (which has the menu bar), is where I do my actual work and that's where I would like to be able to minimize all open windows with one shortcut...Hope this explains it better?

You still haven't defined when a window should be considered as being on the main screen -- but perhaps you are tidier than me and never have windows that cross 2 or more screens. I'm going to take @Airy's lead and decide "the window should be minimised if the top-left corner is on the main screen".

So the logic is

IF
   the window's left-edge coordinate is >=  the screen's left-edge coordinate
AND
   the window's left-edge coordinate is <=  the screen's right-edge coordinate
AND
   the window's top-edge coordinate is >=  the screen's top-edge coordinate
AND
   the window's top-edge coordinate is <=  the screen's bottom-edge coordinate
THEN
   the window's top-left corner is on the main screen and the window should be minimised

...and I've left them like that in the macro -- you could combine them if you wanted.

A note on the maths: Because your main screen is 0,0 at the top-left corner, the right edge is the same coordinate as the screen Width and the bottom edge the same as Height -- we don't need to calculate right and bottom edges and can just use the 3rd and 4th numbers of %Screen%Main%. You have to get clever if you are doing this for a different screen.

There's only two real tricks to the macro:

  1. The %WindowFrame%All% token returns all the window frames of the active app, one per line, in index order (front to back in the window stack)
  2. You should start at the backmost window and work forward, to make sure that you don't change the indexes of windows before you get to them. So we reverse the list from step 1 to make it back-to-front then test each set of coordinates in turn while counting down the window indexes, starting at the highest

Putting it all together gives you:

Minimise Windows on Main.kmmacros (8.2 KB)

Image

Test Carefully!

Try it with only a couple of apps open, each with a couple of windows, and see how you get on. It doesn't do anything "destructive", it's easy to recover from, but I've only stepped it through a couple of apps'-worth of windows because it'll take me ages to reset everything on my main screen!

Thank you so much for your effort. Very interesting.

To answer your question, I am indeed not running windows that span multiple screens. In your words, it's clean, indeed.

I tried it out and it right away minimized a window on another screen. But it did not do it the second time. And then it started minimizing windows but not all of them. Unfortunately, the design of your macro is a bit too advanced for me to play with it. But I wanted to at least share this first observation with you. I'm not sure what could be done to fix it...

It'll certainly be difficult to do remotely!

But you can have a go yourself. If you launch KM's Debugger (KM menu bar icon -> Start Debugging) and set it to "Pause New Macros" you can then run the "Minimise..." macro and step through it one action at a time with the "Step into button":
image
...and see the variables, test, etc as they happen. It'll also slow things down enough that you'll be able to see any pattern to the problem.

And it will show you that while the macro might look "advanced" it's actually just a few basic actions made more confusing by the loop-within-loop structure. I'm sure you will understand what it's doing -- and doing wrong -- after a couple of loops have passed.

1 Like

Thank you! Never tried it. I will give it a shot.