Macro to Quit/Close All Windows Across Multiple Spaces Not Working as Expected

Hi everyone,

I’m trying to create a macro in Keyboard Maestro to quit, close, or hide all open windows across multiple Spaces (desktops). Currently, I have to manually switch to each window and press ⌘W to close them, which is time-consuming, especially with 8+ Spaces.

Here’s what I’ve tried:
I tried create macro that quitting them entirely

The Problem:
When I reopen apps like Firefox, they restore the same windows I previously closed. This happens even after quitting the app. It seems like session restoration is causing the windows to come back.

What I Need:

  • A macro that can close or quit all windows across all Spaces without manually switching to each one.
  • A way to prevent apps like Firefox from restoring windows when reopened.

Current Workaround:
For now, I’m manually moving to each window and pressing ⌘W to close them, but this isn’t efficient.

Any suggestions or examples of macros that can handle this?

Thanks in advance for your help!

I can't really speak for working in a non-current Space, other than to save that Apple doesn't allow developers to do it very easily. In one of our apps, we can't see off-space windows unless the user has manually activated those spaces, and we can't update the list of windows on other spaces until users again visit those spaces.

I don't think I've ever seen an AppleScript that can execute commands on windows in other spaces, but maybe there's one out there. There are macros here that deal with windows and spaces; @_jims has a very rich version:

As I don't use spaces at all (I prefer having all my windows in one spot), I can't say how well it works, but it might show you some tips for doing what you want to do.

As for restoring open documents, you can make it so that no apps restore their last-opened documents. Go to System Settings > Desktop & Dock, and enable "Close windows when quitting an application." The subtext to that reads "When enabled, open documents and windows will not be restored when you re-open an application."

With that enabled, just quitting all the apps may be all you need to do.

-rob.

Here's what I use. It works across all spaces and all physical screens.
Hide All Apps New.kmmacros (5.3 KB)