Close all window of all applications except window currently in focus

Is this possible? I can find several macros in the forum that attempt to hide all windows, but not close all windows.

Hi @layo, perhaps this KM action is what you need.


https://wiki.keyboardmaestro.com/action/Quit_a_Specific_Application?redirect=1

Close windows, or quit applications? These are different things.

Even so, you should be able to put something together. For quitting, see @ChrisQ's answer above. For closing, you'll need to capture the name of the active application, then use the "For Each" action, the %ApplicationName% token, and the "Use Variable" action to work through all the apps and close their windows (skipping the app you want to keep open, obviously).

Hint: Many apps have a "Close All Windows" menu item, often mapped to ⌥⌘W

You'll also need to include a method of handling any dialogs when you go to close an unsaved document. And you may or may not (it isn't clear) want to close all but the active window of the app you want to keep open.

Have a go, see how you get on, post back here with your attempted macro and any questions.

1 Like

This is my attempt. Getting name of front window is fine. But macro breaks down thereafter. In particular, %WindowName%All% does not seem to be capturing all window names even though Wiki suggests this is possible.

Note I want to close all windows, except, the frontmost, but not quit all apps.

Close all windows except current.kmmacros (3.1 KB)

Hello Ross (@layo) :wave:

Just by looking at your Screenshot I know that there is a significant mistake in your attempt.

The Variable For the frontmost window is set correctly - but not the Variable for all other Windows.

  • Change the Second Variable Name to reflect All Not all other.

To Close all other windows but the frontmost one you‘ll need two loops …

  • the first one (missing) builds up the All others Window List where you will have to ensure that if the title of the window does not match the frontmost one‘s and append this title to the List for all other windows.

  • the second one though must be set as to loop through this list and just close the window that matches the current one from the list.

Note that there is some extra work to do if any window is minimized - where you then have to un-minimze it first before you are able to close it.

I‘m not able to post a macro (because I am currently not at home) to show you how it’s done - but I hope you get it from this description though…

If not maybe one of the others here are able to follow my description and build a macro for you.

Greetings from Germany :de:

Tobias

%WindowName%All% gets all the window names of the current frontmost application rather than all the window names everywhere. Is that what's catching you out?

Also, "windows" are not the same as "tabs in windows" -- you need to mention tabs if that's important.

You don't actually need that token anyway...

I'd start with something like this:

save name of current frontmost app to 'Local_myApp'
save name of current frontmost window to 'Local_myWin'
for each open app
   if app name ≠ 'Local_myApp'
   then
      close all windows
   otherwise
      -- do nothing
end for each
-- that should leave you with all apps open but only the one you want with any windows
activate 'Local_myApp' -- the "Use Variable" action is handy here
for each window
   if window name ≠ 'Local_myWin'
   then
      close window
   otherwise
      -- do nothing
   end if
end for each

That's an off the top of my head outline, so you'll have to fill in the details. For example -- does the app have a "Close All Windows" command? If so, use it. If not, keep closing the front window until there are none left. And you'll also have to deal with any "Do you want to save...?" dialogs if they crop up.

See how far that gets you.

Nige (@Nige_S),

Of course he will need this token … he needs a list of all windows to iterate through - comparing it to the one he wants to keep opened for building the list of windows whose have to be closed …

It’s basically the same I wrote above :point_up_2:

The only difference to your outline is that I was only referring to the frontmost Application.

If he has Applications that have a Close all windows command he will not have to use this approach unless such an application is currently the frontmost …

Anyway I think he should get it working at first side using this approach and build up on this to loop through all Applications and then continue to compare each to a List of those Applications which have this command to use the command instead of this routine. If this all is done he will have a macro that handles the Windows like he wants it.

Greetings from Germany :de:

Tobias

ORLY?

image

Theoretically, that would mean you didn't have to check the window name since it's the frontmost you want to leave open. Whether you want to take that chance is another matter...

For the apps where all the windows are to be closed but don't have a "Close All Windows" option -- well, no spoilers (although the clue is in the text above), but you don't need the token for that either.

1 Like

Wow :star_struck:, thanks Nige (@Nige_S)

That usage of the Windowcount Function is incredible… didn’t expect this possibility..

Based on this it’s definitely applicable without using the All Windows Token.

Just learned something new today - thanks man , Love it :+1:

Greetings from Germany :de:

Tobias

Attached is my latest attempt. I have tried to do what Tobias suggested in his first post. But there's errors somewhere in macro. Any further pointers?

Close all windows except current.kmmacros (3.6 KB)

When you create a Collection of "The running applications" then work through "For each windowName", windowName will be something like:
/Applications/Keyboard Maestro.app
...then:
/System/Volumes/Preboot/Cryptexes/App/System/Applications/Safari.app
...then:
/Applications/Microsoft Teams.app
...then:
/Applications/Microsoft Outlook.app
...etc.

You can use those paths to activate each open app in turn -- that will then let you get the window names because the window related tokens work on the currently activated app.

But you'll need to get the window name token right. You can see in your screenshot that "Append Variable" action is appending a literal %WindowName% -- that's because KM can't make sense of the token so treats it as text.

If you look at the "Window Related Tokens" Wiki page you'll see why:

Token Parameters
The non-Front variants take a parameter indicating the window.

Your token isn't a "Front" variant and doesn't have a parameter, so isn't valid.

Have another go, but remember that you want to close all the windows in the other apps so there's no need to check their names against your saved window name.

In fact, if you work through my pseudo-code above you can almost replace each line with an appropriate action and be 90% of the way to what you want.

Yeah, I'm making you do the work :wink: But that's because you're almost there...

2 Likes

Hello Ross (@layo)

I could not explain it better than Nige …

There is only one thing I have to mention that Nige didn’t …

If you are working with Loops based on only Variables there ale always two Variables involved - the one that includes all items (a List and the Variable that represents each item at the time the loop works on one item of the List Variable) and both should be named to reflect what they are in use for and - of course they shouldn’t have the same Name.

Besides that - you should decide if you want to go with my suggested approach based on the Window Tokens, or the Window Function based approach suggested by Nige which will also work.

Greetings from Germany :de:

Tobias

1 Like

My interpretation of Nigel's post was WindowName needed changing to a front variant, and I have done this. To use paths to activate each open app in turn, I did what the Keyboard Maestro wiki appears to suggest and use the action 'Use Variable' to set the front application by name.

But there's something going wrong. Being honest, I have little clue with what this macro is trying to do now, so any further advice gratefully received.

Close all windows except current.kmmacros (3.9 KB)

You'll help yourself a lot if you use meaningful names for your variables -- if you are working through a Collection of applications, call the "For Each" variable Local_app or Application (if you want it to be a global variable). You can then simply read the action to know what it does -- "For Each Local_app in the Collection of running applications do these things".

Now my "meaningful names" may not be meaningful to you, but hopefully this macro makes sense! Read through it first to get an idea of how it works, then run it.

It will not close any windows! This is a "dry run" and will display dialogs telling you what it will do rather than doing it. If you're happy you can delete the "Display Text" actions and enable the "Type the ⌥⌘W Keystroke" and "Close..." actions.

I've used the various different methods @Nr.5-need_input and I mentioned above so you can see them all.

This has not been tested for actual window closing -- with 21 apps and 107 windows open, I'm not putting myself through that pain! -- and you'll probably have to add ways to deal with the almost inevitable "Would you like to Save this document?" dialogs. How you do that will depend on how you work, how you'd like to deal with them, the apps involved, etc.

Close All Windows Except Front One.kmmacros (19.0 KB)

Image

Ask if you've any questions.

Many thanks for patience and expertise. Learnt several things from your macro .e.g. didn't know individual actions could be disabled (just entire macros) and the if statement 'a menu item with shortcut' is new to me as well. The macro works well and I will refer back to it when trying to solve other problems.