Tip: Close All Finder Windows but the Frontmost

After years of just putting up with it, I decided how to automate this process. I was so tired about having so many Finder windows open when I just needed the one in the front so I went here:

And tried out the code. It works, so all one has to do is copy and paste the AppleScript there and out it in a "execute AppleScript" KM macro.

tell application "Finder"
   
   repeat while window 2 exists
      
      close window 2
      
   end repeat
   
end tell

Then I added that macro to the very last StreamDeck button I had empty.

Now... Bliss

KM is the best.

2 Likes

Nice idea, never occured to me.

You can also do it with pure KM actions:

Close%20all%20Finder%20Windows%20but%20the%20Latest%20%3C6138%20200224T172752%3E-pty-fs8
Close all Finder Windows but the Latest <6138 200224T172752>.kmmacros (2.9 KB)

Setting of the inner action:

21-pty-fs8

An interesting difference between the AppleScript above and the KM version is that

  • the AppleScript closes windows in all Spaces
  • the KM action only closes windows in the current Space

(at least on my machine)

So, you can use either, according to what you want.

And, just to mention it, you can also use a higher index number, for example 4. Then it should spare the 3 most recently frontmost windows.

5 Likes

Prob a better solution since native KM will be faster than Apple Events potentially.

Not necessarily, and if yes, then the difference will be negligible. More important is the different behavior with windows in different Spaces. Can you confirm this on your Mac?

For convenience, here a macro that lets you select the number of windows “to spare” (1 to 3) directly with the hotkey:

Close%20all%20Finder%20Windows%20but%20the%20Latest%20n%20%3C6138%20200224T183156%3E-pty-fs8
Close all Finder Windows but the Latest n <6138 200224T183156>.kmmacros (6.4 KB)

  • ⌃⌥⇧⌘F1 --> Closes all windows, except the frontmost window
  • ⌃⌥⇧⌘F2 --> Closes all windows, except the frontmost window and the one that was frontmost before
  • ⌃⌥⇧⌘F3 --> Closes all windows, except the frontmost window and the two windows that were frontmost before

Should also work with number triggers like ⌃⌥⇧⌘3 or ⌃⌥⌘3. Just the last character of the hotkey must be a number. You can also add triggers up to 9.

If you prefer to close Finder windows in all Spaces instead of just the current one, then disable the Repeat action and enable the AppleScript action.

4 Likes

@Tom. This is simply brilliant. I had no idea that could be done in the matter you displayed here. :face_with_hand_over_mouth:

Thanks for the ongoing lessons.

KC

Downloaded. Not changing any settings, If I have 3 or 4 finder windows open. I alway end up with 1 even though the index is set to 2.

is that correct behavior ?

EDIT:
Oh. just realized I have to set up the 2,3 etc..

Only closing finder windows on the current space is GOLD.

Thank you.

I don't know about this specific KM Action, but KM does use AppleScript under the hood for a number of Actions.

1 Like

Step 1: Open Finder.

Step 2: Hold the Option key while clicking the File menu.

Step 3: Click Close All.

This will close all Finder windows in one fell swoop. Actually, the Close All command works with pretty much any app, but it’s especially useful for Finder since you can’t actually quit the Finder like you can with other apps.

If you’d prefer to avoid the menu bar, you can just use this keyboard shortcut instead: W

Hey @10bestgloves,

Welcome to the forum!  :smile:

Good tip. But keep in mind that the O.P. wanted to leave the front window open.

Here's another way to do that with AppleScript.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2020/12/05 03:16
# dMod: 2020/12/05 03:16 
# Appl: Finder
# Task: Close all but the front window in the Finder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
--------------------------------------------------------

tell application "Finder"
   if (get count of windows) ≥ 2 then
      close (get every window whose index is not 1)
   end if
end tell

--------------------------------------------------------
1 Like