Simple: close all Safari and Chrome windows

Dumb beginner question I’m sure. What’s the simplest way to write an action to close ALL Safari and Chrome windows?

I see actions to close a window, but am not sure how to close them all. And if none are open (or either application isn’t launched) I don’t need an alert. I simply want to write a macro that makes sure that all Safari and Chrome windows are closed. Any help?

Thanks!

In an Execute JavaScript for Applications action you could write:

['Google Chrome', 'Safari'].forEach(function (a) {
	Application(a).windows().forEach(function (w) {
		w.close();
	})
});
1 Like

Thanks a lot for this! I actually didn’t even realize you could execute Javascript within KM.

I know absolutely zero about JS, so while I’m happy to simply copy and paste this code to solve my problem, I’m also curious if there’s a way to do it natively/directly in KM, or via AppleScript (which I am only slightly more familiar with). And my only reason for asking this is so that I learn KM better and can make it do other things I want in the future without having to constantly ask for help. :slight_smile:

Or You could use KB actions..


Quit Chrome and Safari.kmmacros (6.3 KB)

2 Likes

Thanks! That quits Safari and Chrome, and I know how to ask KM to relaunch them, but really all I wanted to do was tell it to close all Safari and Chrome windows, not quit them.

(If you must know, the longer explanation is that I have a media player Mac set up, and want to set up a keystroke that instantly stops ANYTHING that might be playing, so my KM macro includes telling iTunes to stop, telling Spotify to stop (via AppleScript), quitting DVD Player, and then closing all Safari and Chrome windows (since either app might be playing YouTube, Netflix, or anything else). But I don’t want to quit Safari and Chrome because they get used very regularly. I just want all their windows closed in the quickest way possible.)

One thing I’m not sure quite about is whether you want to

  1. Close all windows and leave the app running
  2. Quit both apps

The JS example does the former, and Bill’s ⌘Q actions do the latter.

in AS terms, the difference between:

tell Application "Google Chrome" to close windows

and

tell Application "Google Chrome" to quit

The JS Execute Action version for a full quit, as in Bill’s example, would be:

['Google Chrome', 'Safari'].forEach(function (a) {
	Application(a).quit();
})

UPDATE:

Ah … posts crossed : - )

1 Like

Sorry if I was unclear. I just wanted #1 (close all windows but leave apps running).

ComplexPoint, I appreciate your JS, and can go ahead and use it, but was just wondering if there was an easy way to do it in KM or AppleScript since I have zero JS experience and just wanted it in a language I could understand and learn from.

Your last post mentions the AppleScript:

tell Application "Google Chrome" to close windows

which is very helpful. I didn’t know that AS command existed. I assume it will work without errors, e.g. if there are no Chrome windows, it’ll just be ignored.

Yes – if the list of windows is empty the script will simply have no work to do, and will quietly go back to sleep.

( windows is a shorthand for every window. It first constructs a list of references to any windows, and then works through that list, empty or otherwise, applying a close to any window references it finds )

Keyboard Maestro “Close all Safari and Chrome tabs” Macro

Quite easily done using the Cmd+Option+W keys which close all open tabs in both Safari and Chrome.

2 Likes

Thanks everyone who’s chimed in.

Sounds like the simple AppleScript

tell Application "Google Chrome" to close windows

is the shortest, most direct route to doing this, plus of course, I can put multiple commands into the single KM action, enabling me to close both Chrome and Safari windows in the same step.

Bonus points to Mirizzi – thanks for pointing that out – I never saw that Safari and Chrome both had that option in their menus. That should also work. (Although if no windows are open, it’s grayed out, and thus I’d want to test it to make sure it doesn’t generate an error if it’s executed when no windows are open.)

Anyway – AppleScript it is. THANKS ALL!

Hey David,

Those are basic Mac keyboard shortcuts respected by most every application.

-Chris

1 Like

Well, some of them. Many apps (even Apple apps) don’t conform to that. I use the regular close all the time (cmd-W) but I never knew that option to close multiple windows/tabs was there.

That option is grayed out or completely missing if there aren’t multiple windows/tabs open, so I hope/assume that if I disable the “Notify on Failure” option for that action in KM, it would proceed normally.

There are variations of course.

Safari respects the normal keyboard shortcut IF no tabs are open in any window, but if tabs are open then it wants Cmd-Opt-Shift-W to close all windows.

You can use the search field in the Help menu to discover these things.

Many Mac-users have no idea they can open a menu and hold down modifier-keys and find optional commands, but this sort of thing has around for decades now.

-Chris

Yes, and Apple keeps changing them too, making it near-impossible for even power users like me to stay on top of all the new hidden tricks. Witness, for example, the debacle that was auto-save (which affected traditional Save and Save As functions) and how Apple has now brought back Save As but only by using a modifier. Things like that…

See:
Mac keyboard shortcuts

1 Like

Thanks JMichaelTX, it’s easy to assume you know all these. I found two great shortcuts I didn’t know existed.

Space bar to toggle Quick Look on and off, works in Path Finder too.

Then, ⌘⇧~ to switch between open windows in front app, I had to disable the Keyboard Maestro one as it conflicts with something else I’m using.

1 Like

I agree. It was a debacle. I turned it off instantly.

Thanks to JM for the keyboard shortcuts doc — I haven't looked at it in a while.

Years ago I spent a chunk of change on KeyCue to help deal with that stuff, and the most recent version is better than ever (it now has a search function). I only run it when I need info or am trying to diagnose something.

-Chris

Oh, I HATED Autosave. And another app I use a lot (Pixelmator) implemented it early, but without much documentation. So I would open a Pixelmator document, make an edit (such as reducing it in size for a specific export), close the file, and not realize that I just altered my master file. Then I moved the Pixelmator file to Dropbox to get it from another machine – thereby losing all the Versions…argh…it was a NIGHTMARE.

Classic example of Apple taking something that wasn’t broken, tried to make it smarter, not realizing they were making it worse.

Now we’re in this weird mid-point where autosave is used for some things (FCP X, for example), not for others, and the Save As that we all know and love is there but hidden. And many third-party apps don’t support autosave at all (for good reason – some apps should not be using autosave at all).

I just turn it off in the General Prefs.

I believe it's possible to do it on an app-by-app basis as well using defaults.

-Chris

1 Like