AppleScript: Close all Finder windows except the front one

I often end up with tons of Finder windows open. I usually realize this when I ⌘-tab to Finder and notice there are 20 windows. I generally want to keep the frontmost window but close all the others. Turns out there is a super simple AppleScript to do this (source):

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


🌟 Was this page helpful? Please let me know with this quick, 3 question survey.