Tip: Close all Finder windows but the front most

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.

3 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