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