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
Get the "active" screen
Get a list of all foreground apps
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
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"...
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.