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?
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.
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.)
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 )
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.)
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.
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…
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.
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).