How to Hide All Other Apps on the Current Display?

I'm aware of the built in Option-Command-H to hide all other windows, however when using two displays I'd like to have this only affect the display containing the active front window - or mouse pointer if that's more feasible.

Before I dive in, has anyone else tried something like this?

That's the easy bit, either SCREEN(Front, Index) or SCREEN(Mouse, Index) will do.

Trickier is finding the not-frontmost apps that have windows on that screen and so need to be hidden. The only way I can think to do that KM-natively would be to

  1. Get the "active" screen
  2. Get a list of all foreground apps
  3. For each app apart from the first (which is your starting frontmost app)
    a. Activate the app
    b. Get all the windows
    c. Loop through the windows checking if any of the left, right, top, or bottom coordinates are in the "active" screen's bounds
    d. If so, hide the app and exit the loop
  4. Activate original frontmost app

All that app activation and window checking could mean a lot of screen flicker, CPU action, and time -- especially if, like me, you run a boatload of apps each with a boatload of windows! And it might result in extra windows if you've apps that create a new window if brought to the front with no window open.

I'm sure you could do similar with AppleScript, although with quite a bit more work to set up! I really, really, hope someone else will chip in with an "I've already done this -- here you are"...

Hey Guys,

This task is not too awful using AppleScript – with the caveat that I only have one screen and can't test properly.

It should be possible to do the same thing with native KM actions as @Nige_S describes, but I didn't want to fiddle around with that.

-Chris


Hide Non-Frontmost Application on the Main Screen v1.00.kmmacros (7.2 KB)
Keyboard Maestro Export

Interesting. Testing it on my dual monitor setup, the AppleScript has the effect of hiding all windows other than the front window, regardless of their display (so the same as the built-in cmd+opt-H behaviour).

First thoughts are that there are two things going on:

windowX1Pos is being set to a negative value for windows on my left screen, and positive for windows on the right (main) screen. That's easy enough to adjust for in the innermost if statement.

But the other problem is that I'm seeing many more window position being checked than I'd expect. So I think somewhere there's a mismatch between the window which is checked and the app that becomes hidden.

Just for clarity -- you don't hide windows, you hide applications. So if eg TextEdit is frontmost and Safari has windows open on both your main and secondary display, all your Safari windows will be closed. If that isn't the behaviour you want you'll have to do something else, like minimise windows individually.

I'm even worse with ASObjC than I am with AS, so I'm not entirely sure what @ccstone's script is doing at the beginning, but I'm surprised only two integers are involved. We need the rectangle of the primary screen so I'd expect four -- eg top-left X, top-left Y, bottom-right X, bottom-left Y; or in KM terms top-left X, top-left-Y, width, and height.

That's correct -- display coordinates have the zero point at the top-left of the main display, so anything to the left of that (as you look at the display arrangement in Display Prefs/Settings) has a -X coord, anything above has a -Y.

Okay, that makes sense. I'm going to put this aside for now and might revisit after I've had a chance to try Ventura (I didn't upgrade yet because of a potential driver issue).

Depending on how Stage Manager behaves across multiple displays, it might solve my overall need of having less stuff open when I need to focus.

All that's necessary is to have the X1 and X2 coordinates of the {X1, Y1, X2, Y2} bounds of the frame – i.e. the leftmost and rightmost x-coordinates.

Unless you have screens stacked vertically.

Then you acquire the leftmost x-coordinate of the front window, and you have everything needed to discover what screen the window is on.

That is – if I've got my logics correct...

Gotcha. I've got too many vertical stackers, eg laptop below raised second display, to make a left/right assumption which is why I was looking for the full rectangle.

1 Like