Quit app if no windows left

Hi

I used an app call swift quit that quit an app when you close the last window.

It works pretty well until sequoia…

I would like to know if it’s possible to make something similar with keyboard maestro ?

I tried with desactivate and an AppleScript but I didn’t find a way.

Any idea ?

Thanks

I can think of a couple challenges with this. The first is that your macro needs to either be running all the time, or run any time there's a window close event. You could trap on Command-W for the keyboard shortcuts, and watch for Close from the menu, but catching a click on the red close widget is going to be tougher.

But even if you catch all the events, you still have to figure out if that was the last open window in the app. You'd have to do that via an AppleScript that counted open windows in the app, and then quit if the count was zero.

So now you have an AppleScript running on every close event, which will slow things down a bit any time you close a window. Who knows, maybe that wouldn't be too bad. The hard part is making sure your macro triggers any time a window is closed via any of the various methods. And honestly, I have no idea how you'd do that.

The other way to do it would be to just poll periodically: Have your macro run every NN seconds (or whatever). It would run in the background and run an AppleScript to check the open window count in each running app. If the count is zero, it would quit the app.

I don't think this is ideal, but it's the easiest solution to implement, I think.

-rob.

The problem that I see is that some apps may START with no windows open, either temporarily or until you open a window (manually or through the app's auto-starting.) Therefore any macro that sees an app with no open windows may close the app, even if you just started it. I don't think you want that, and I'm not sure what you want to do in that case. regardless, I took a stab at writing the macro for you, and this seems to catch most cases. Maybe it's good enough for you.

Notice that this macro doesn't trigger directly on a window closing, but it should be able to detect most windows closing through two of its three triggers.

The deactivate trigger actually works on whatever app replaces the one that was just closed. But I think that's still a good situation to trigger on.

I'd go the other way and use "focused window" changes as the trigger:

Same caveats as above for apps that don't open a window on launch -- although most I've tried do, even if that window is only an "Open..." dialog.

I do question the need for such a macro these days. macOS resource handling is so good now that any reasonably-written app has limited impact when "idle", especially with no windows open (don't be fooled by eg Activity Monitor memory numbers -- memory isn't "reclaimed" from an idle app until something else needs it). For those you'll probably be "spending" more resources checking, quitting, and re-opening than you'll ever "save".

Which just leaves "unreasonable" apps and those that are doing "stuff" in the background, "stuff" that you don't want to continue happening after you close the last window. Scope your "closer" app so that it is only active when those apps are frontmost and you'll limit its impact on your system.

Hi

Thanks all for your reply.

@griffman yes I saw the same problems as you.
I didn’t want to use timer but I like your idea with ctrl + w !

@Airy Thanks for your macro. I didn't think about using WINDOWCOUNT function.
However, the main problem is that if I close a word document for example, WINDOWCOUNT() count number of window for the next app, not for the app that I closed.

@Nige_S Thanks too for your macro but same problem as @Airy.
Do you have an idea how to check if an app is "reasonable" or "unreasonable" ?

As I always use ctrl + w to close a window, I think I will use this as a trigger.
For now, I end up with :

However, the problem is to close the window when WINDOWCOUNT() > 0 and not just quit the app.

WINDOWCOUNT() returns the count of windows for the frontmost app. If Word is at the front and you close the only Word document window, WINDOWCOUNT() will be 0 and Word will Quit. Verified with macOS Sequoia and Word v16.89.1

What might be throwing you is that as soon as Word Quits a different app becomes frontmost. That means a window change, triggering the macro again. and that app's value for WINDOWCOUNT().

Word is certainly one for the "reasonable" list.

I'd ask it the other way round -- why would you want to Quit an app that you may soon be going back to? If the answer is "because I know that when I leave it open my computer runs like a dog" -- that's an unreasonable app. And if you won't be going back to the app anytime soon -- that's what the "Quit" command is for!

You need to start your macro with a "Close window" action -- you can then get the new WINDOWCOUNT() and act accordingly.

Thanks for these informations.

However, close window close all the windows of an app. I try to find a way to emulate ctrl+w like closing a browser tab, not the entier window.

Aha!! Maybe in your original posts you meant you wanted to close a tab, not a window? That's a totally different question. Windows and tabs are different things.

Yes indeed.

Currently, I use cmd + w to close tab and close app if there’s no tab anymore.

But I didn’t see a way to emulate that beyond redirect to ctrl + w at the end.

Okay, that changes everything. But now you have to clarify some things. Many apps (eg, Safari) support multiple windows AND multiple tabs simultaneously. If all the tabs are closed on one window, surely you don't want the app to close it there are still other windows open in that app. Am I right? I'm not sure what you want, but I'm guessing that you want the app to close if the LAST tab on the LAST window has been closed. If I'm right about that, then what you are really asking for is the app to close when the last WINDOW closes, not when the last TAB closes.

Yes that’s right :slight_smile:

Do you know if KM have an action to close only tabs and not the all application ?

The short answer is "No". There is next to no OS-level support for tabs, so next to no way for KM to interact with them. That there are actions for browser tabs is because the browser apps make them available through their scripting interfaces.

The longer answer is "Maybe -- it depends on the app". Having an application "window" that contains tabs and still remains open once all the tabs are closed is, quite frankly, either a shonky port from a Windows app, a deliberate "we want our cross-platform apps to look the same on Mac as on Windows", or a "we think we know better, so up yours Apple Interface Guidelines" (looking at you, Adobe devs...).

Look at the Finder to see how a tabbed window should behave -- ⌘W closes the active tab until there are no tabs, at which point it closes the window itself.

If you're lucky then app will provide some programmatic way of querying tab count. If not you may have to resort to menu scraping or "Found Image" actions.

What's the application(s) you want to do this for? Someone may have already found a way.

Yes you right.

Right now I think about adobe… and autocad.

Anything on a macOS screen that you can see with your eyes and then use the keyboard or the mouse with your fingers to interact with, can probably be simulated with Keyboard Maestro.

Sometimes there are built-in direct actions in KM to get things done, but sometimes you may have to use tricks like the OCR action, the Find Image action, or sometimes AppleScript or Javascript to get things done.

More to the point, If you can see the tabs with your eyes, you can write code to see the tabs with KM actions. And if you can click on the tabs with the mouse, you can click on the tabs with KM's Mouse action. But it might take a lot of work. Depending on the app, I can see that it might take over 100 lines of complicated code, and the code could be different for each app.

You mentioned Autocad and Adobe (although Adobe is a suite of different apps that may behave differently.) Since I don't have those apps, I probably can't help you write the code needed to detect and close windows. But if you show some screenshots of the apps with multiple tabs, I might be able to tell you what the best approach would be.

I didn't know that Keyboard Maestro can "read" the screen, it's awesome.

Here a screenshot of auto cad screen :

And a screenshot of adobe pdf screen :

Those screenshots help me a little. I think it would be possible to use Find Image and OCR to determine how many tabs are open. and to get the names on each tab.

For example, on Autocad, you could use the OCR action to retrieve the words on the tab bar. You would probably have to stop using French characters, since Apple OCR in KM does not yet read French characters. As long as your files contained no spaces, it should be fairly trivial to return a count of the number of tabs open.

If you want to try this approach, you can create a macro that uses the OCR action to read the text in the tab bar of the Autocad window. The number of words returned by that action would tell you how many tabs you have open. Is that an adequate solution for you? Apple's OCR is extremely fast and reliable.

The problem with French is that a lot of my work function are in French, so I must use French for Autocad and for file naming...

Is it a problem at the end ?

Are you asking me if French is a problem? I wouldn't know. You can test it if you want.

Ok thanks to you, I create this macro :

CleanShot 2024-10-11 at 12.42.41

It seems to work well for this two applications !

About performances and energy consumption, can I see how much power this macro use ? OCR must need a lot of power no ?

Better than that, it can "read" menus and tell if certain items are or are not available. An obvious one to try here is the "File" menu's "Close File" menu item, as that should only be available if a tab is open in the active app window. What you do need to do is apply some pauses, else the macro races ahead and checks for the "Close File" item before Photoshop has closed the file...

For testing, put this in a Macro Group that is only active when Photoshop is frontmost. You can then add other apps later, or duplicate the macro and adjust it so they have their own version in their own app-specific Group.

You said you use "ctrl+w" to close a tab -- I've assumed you meant Command-W (⌘W), the default, and have set the trigger accordingly. Remember that hot key triggers are swallowed by KM and never reach the application -- another reason for limiting the apps the macro is available in.

Close App when no Open Tabs.kmmacros (7.2 KB)

Image