Negative Sideffect: Unminimize Windows Action forces Window order switch after changing frontmost Application on non minimized Windows

Hey all,

I am currently using the following macro to deal with minimized windows:

This works great if there is indeed a window of an app minimized and in general if the app has only one window open.

I do see a negativ side effect, that's quite annoying: Lets assume I have two windows of one app open, non of them is minimized but they are slightly overlapping. When I come back to this app (after working in another) using CMD+tab it always brings the window up that was behind the one I last worked in, which it should not do.

Example:
App A has window 1 and window 2 open
I am working in app A on Window 2, Window 1 is behind it
I switch over to app B and then go back to app A
Window 1 pops up to the front, despite me last working in window 2

Can you think of a way to adjust the macro that it won't do that and shows me window 2 at the front instead?

Thank you
Dennis

My answers are rarely the best answers, but if the "order" of the minimized windows is "different" from what you want, then it sounds like you have to manually (ie, in KM) minimize (or, perhaps, restore) them in a different order. The order of windows on the screen is called Z-order. If you can use AppleScript to record the Z-order of all the windows on the screens, then you could minimize them (in KM, or in AppleScript) in a different order than what you are currently doing.

But from what I'm reading on ChatGPT, even AppleScript doesn't have the ability within itself to fetch the Z-order, and you will either have to use AppleScript to (a) access Swift or Objective-C, or (b) use a Third Party tool like BTT or Hammerspoon.

It sounds to me that it's easier to change the order of the windows during the minimization stage than during the restoration stage.

So this problem is two levels too deep for me (AppleScript+Swift) to be able to help. However there are much smarter people than me on this website and they might find a much better/easier solution.

Hey Airy,

appreciate your thoughts on it. In the case I am describing non of the windows would not be minimized. The script itself is ment to deal with minimized windows but apparently has a side effect on apps with multiple un-minimized windows.

So in my current situation both windows are un-minimized just one is behind the other. Does that change a possible solution?

I would solve this another way: After an app activates, check if the app has any minimized windows; if it doesn't, the macro would stop, otherwise, it would do the unminimize thing that seems to (for some reason) switch the window order.

I don't, however, know how to check if the frontmost app has minimized windows—that's an AppleScript beyond my skill level.

-rob.

You are still needing a change to the order. I'm not sure how KM could change the order without fetching the Z-order. But as I said, there are smarter people than me here, and one of them has already mentioned an alternative idea.

But I'm also wondering now if there's a way we could create a shortcut for you that would simply reverse the Z-order of the currently active app. That might be a simpler and, possibly, satisfactory solution.

Not sure if I understand why it would need to change the order of the windows.

I leave the app in a particular window and when returning to the app I want to come back to that exact window. What the macro does however is changing the order. At least if we mean the same by "order"?

As a newbie this sounds like a promising idea. As I don't have experience with it I just asked ChatGPT. Initially the suggestion does not work. Posting the suggested code here in case someone could work with that:

tell application "System Events"
-- Get the frontmost application
set frontApp to name of the first application process whose frontmost is true

-- Try to tell the application to get the windows
try
    tell application frontApp
        -- Get all the windows of the front application
        set windowList to windows
        repeat with aWindow in windowList
            -- Check if the window is minimized
            if (minimized of aWindow) is true then
                -- Bring the minimized window to the front
                set index of aWindow to 1
                -- Exit the loop once a minimized window is found and brought to the front
                return
            end if
        end repeat
    end tell
on error
    -- If the application doesn't support window management or any error occurs, simply return
    return
end try

end tell

Not ideal but I just added the following work around:

Added the keystroke that toggles between open windows. Not ideal but as I usually don't have 3 windows of the same app open, this should improve the situation.

As @griffman said, test to see if any windows are minimised.

image

The script is

tell application "System Events"
	set frontApp to name of item 1 of (every process whose frontmost is true)
end tell
using terms from application "System Events"
	tell application frontApp to return count of (get windows whose miniaturized is true)
end using terms from

There are probably quicker/cleaner ways to do this -- but this way does at least work!

1 Like

Thank you so much for the script. Unfortunately this causes the same issue like my initial script. If no window is minimized but multiple windows are open, it changes the order of the windows. Is that made a bug in Sequoia?

I think I got it to work based on some searching here and help from ChatGPT. The key is the AXMinimized window attribute, which is true for any minimized window.

Edit: @Nige_S posted his version while I was typing; his seems like an even simpler method than this one; leaving this for posterity....

This demo macro will put a message onscreen when an app with a minimized window becomes frontmost. I had a version that involved setting a variable to the frontmost app, then passing that variable to the AppleScript; ChatGPT provided the all-in-one solution to get the frontmost app in AppleScript (which I knew was possible, but didn't know how)

Download Macro(s): End if app has no minimized windows.kmmacros (5.6 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.7
  • Keyboard Maestro v11.0.3

Change the actions in the Case statement to do whatever you want to do when the app has minimized windows, and this should (in theory) do what you need it to do.

-rob.

It does the opposite here -- if no windows are minimised, nothing happens. If one or more windows are minimised it will work through all un-minimised windows (which does change the order) and then open any minimised windows.

But perhaps best to skip the KM "Manipulate..." action altogether. See if this works any better -- it only targets minimised windows:

Unminimise Windows.kmmacros (2.7 KB)

Hello @JustDennis :wave:

I’ve changed the topic name to better reflect the issue you are having.

New title: Negative Sideffect: Unminimize Windows Action forces Window order switch after changing frontmost Application on non minimized Windows

Don’t hesitate to choose a longer title for a topic if it will reflect your issues better than the shorter title because its the opener to a better understanding of what’s going on - even without reading the description of the topic.

If you want to control the Z-Order of your Application‘s Windows, you’ll have to put in the work and track everything at the moment you open the windows very accurate. If you don’t put in the work at this stage and don’t go further in tracking changes in this order - there will be no way to have a full control over it.

Greetings from Germany :de:

Tobias

Good Morning all,

Tobias thank you for changing the title and the additional background. Greetings from Munich btw :slight_smile:

What I still don't understand is the control or change of the window order. I really don't want to do that but rather want to keep it in the way it is but the script seems to change it.

However I am now more certain that an issue with macOS might be at place. The unwanted behavior only happens when I open a new app after the script has been enabled. If I open an app with two windows I see the unwanted behavior. If I turn the Macro off and on again, the behavior does stop. Quite strange isn't it?

However Griffmans version with the Apple script seems more reliable.

I will make a screen recording if it happens with the apple script again to better explain what happens

Well I did not have to wait long... Recorded my screen to show you the effect I tried to explain. I think I can not upload videos here directly, so hopefully its OK to post a link to it:

I simply used CMD+Tab to switch between KM and excel and as you can see at one point it switched between windows in excel, each time I am coming back to excel.

Good morning to Munich @JustDennis

Well, to make it clear - both Scripts are reliable…

Rob‘s (@griffman) Script just collects any window that’s minimized, while Nige‘s (@Nige_S) Script repeats to unminimize every window which Rob‘s one would just detect. So the window changes until there are no more minimized windows.

If you’re changing the AS Variable minimizedWindows to unminimziedWindows , the Value true (a Boolean value) & the KM Variable local_Minimized of Rob‘s example to local_Unminimized as well its condition to empty you’ll collect every single Window that’s not minimized.

The Fact that Nige‘s Scripted Solution still switches the window order of unminimized windows is quite a bummer …

Maybe there is any other AX Attribute that could be used in conjunction with the AXMinimized to stop switching the window order.

I don’t know… maybe I’ve missed something here I am aware of to catch up this behavior of the Script. Maybe not.

KM has everything aboard to be used with everything suggested to build a solution that will restore only the frontmost window or of course the right order of all unminimized windows if needed but that’s quite some work to do.

I’m not the healthiest person at the moment, so I’ll stop writing here for now …

Greetings from Wetzlar (Hessen)

Tobias

You're still using the version of the macro with the KM "Unminimize" action.

Think of how unminimise behaves -- it always brings the newly-unminimised window to the front, regardless of whether you do it manually by clicking the Dock icon, via the app's "Window" menu, using an AppleScript. It would appear to be part of the API result -- so when you use that KM action you're saying:

for each window in the frontmost application
   unminimize window
   bring window to the front
end for each

So you are touching each window in turn and bringing it to the front, regardless of whether or not it was minimised. That could change the window order, depending on the order that "windows with title matching ." processes the windows.

You don't show the full macro, but it looks like you've got the condition wrong. You have "is 0", so the condition is true when there are no minimised windows. You should have "is > 0" so that the condition is true when there is at least one minimised window.

My fault for only posting an image rather than a macro -- sorry about that.

1 Like

Apologies Nige, I did not realize that the Apple Script in your Macro is different.

Your script indeed is not mixing the order of the windows anymore. With some apps it works just great. Randomly for some apps it's not doing anything. If a window in those apps is minimized, it won't pop up.

But I realize I am taking already too much of all your time. I think I am a bit over my head with what I try to accomplish based on how little (or better no) knowledge I have on Apple Script and such. Might be worth revisiting at another time :frowning:

Get well soon Tobias

We can't help if you don't tell us which apps! I tend to use, and therefore test against, apps that can be scripted -- if you can say which are still giving you problems I might be able to test against those too.

There's almost certainly a way to do this, perhaps using @griffman's attribute "AXMinimized" method or maybe even collecting the names with AS then explicitly targeting those named windows with the KM action. So let's not give up yet.

Not in the slightest. This forum is a force multiplier -- time spent on this thread helps you, helps anyone else who wants to do the same, and (selfishly) helps me. So it's time well spent, IMO.

Plus it's cold and raining outside, so I'd much rather be messing around on a Mac :wink:

I really appreciate that and if its really not a bother, lets dive right back in :slight_smile:

The apps it works fine with are Safari and Firefox. Apps it does not work with are Excel, Slack, Finder, FaceTime and Quip. This is the error KM shows:

image