Last active
August 18, 2024 10:28
-
-
Save OKsign/0cdee7f652a07452a7d47b2fdeafeb25 to your computer and use it in GitHub Desktop.
Close all windows of Finder but the active one on macOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Stage Manager must be turned off | |
tell application "System Events" | |
tell application "Finder" | |
activate | |
delay 0.5 | |
set activewinname to get name of front window | |
set winc to count of windows | |
end tell | |
end tell | |
if winc is 1 then | |
delay 0.3 | |
display notification "Close All But Active" with title "Done!" | |
return | |
end if | |
tell application "System Events" | |
tell process "Finder" | |
set lastm to count menu item of menu 1 of menu bar item "Window" of menu bar 1 | |
end tell | |
end tell | |
tell application "Finder" | |
repeat with i from 0 to (winc - 1) | |
set checkm to lastm - i | |
tell application "System Events" | |
tell process "Finder" | |
set myValue to (value of attribute "AXMenuItemMarkChar" of menu item checkm of menu 1 of menu bar item "Window" of menu bar 1 as string) | |
delay 0.5 | |
if myValue is "✓" then | |
set activewin_number to checkm | |
exit repeat | |
end if | |
end tell | |
end tell | |
end repeat | |
repeat with i from 0 to (winc - 1) | |
set checkm to lastm - i | |
tell application "System Events" | |
tell process "Finder" | |
set myValue to (value of attribute "AXMenuItemMarkChar" of menu item checkm of menu 1 of menu bar item "Window" of menu bar 1 as string) | |
delay 0.5 | |
if myValue is "◆" then | |
click menu item checkm of menu 1 of menu bar item "Window" of menu bar 1 | |
delay 1 | |
end if | |
end tell | |
end tell | |
end repeat | |
end tell | |
tell application "System Events" | |
tell process "Finder" | |
click menu item activewin_number of menu 1 of menu bar item "Window" of menu bar 1 | |
delay 0.7 | |
click menu item "Hide Finder" of menu 1 of menu bar item "Finder" of menu bar 1 | |
delay 0.6 | |
end tell | |
end tell | |
tell application "System Events" | |
tell process "Finder" | |
set frontmost to true | |
delay 0.6 | |
click menu bar item "Window" of menu bar 1 | |
end tell | |
end tell | |
tell application "System Events" | |
tell process "Finder" | |
click menu item "Cycle Through Windows" of menu 1 of menu bar item "Window" of menu bar 1 | |
delay 0.3 | |
end tell | |
end tell | |
set roundlist to {} | |
set actionlist to {} | |
repeat | |
tell application "Finder" | |
copy 1 to the end of roundlist | |
set countroundlist to count roundlist | |
set countactionlist to count actionlist | |
set ans to countroundlist - countactionlist | |
if ans is not 1 then | |
tell application "System Events" | |
tell process "Finder" | |
click menu item activewinname of menu 1 of menu bar item "Window" of menu bar 1 | |
delay 0.7 | |
end tell | |
end tell | |
display notification "Close All But Active" with title "Done!" | |
return | |
end if | |
set check_activewinname to name of front window | |
if check_activewinname is not activewinname then | |
copy 1 to the end of actionlist | |
tell application "Finder" | |
set wincount to count of windows | |
end tell | |
tell application "System Events" | |
key code 13 using {command down, shift down} | |
delay 0.7 | |
end tell | |
tell application "Finder" | |
set wincount_2 to count of windows | |
end tell | |
if wincount_2 is wincount then | |
tell application "System Events" | |
key code 13 using {command down} | |
delay 0.7 | |
end tell | |
end if | |
tell application "System Events" | |
key code 50 using command down | |
delay 0.4 | |
end tell | |
else | |
tell application "System Events" | |
tell process "Finder" | |
set lastm to count menu item of menu 1 of menu bar item "Window" of menu bar 1 | |
end tell | |
end tell | |
tell application "Finder" | |
set winc to count of windows | |
end tell | |
tell application "Finder" | |
repeat with i from 0 to (winc - 1) | |
set checkm to lastm - i | |
tell application "System Events" | |
tell process "Finder" | |
set myValue to (value of attribute "AXMenuItemMarkChar" of menu item checkm of menu 1 of menu bar item "Window" of menu bar 1 as string) | |
delay 0.5 | |
if myValue is "✓" then | |
copy 1 to the end of actionlist | |
tell application "Finder" | |
set wincount to count of windows | |
end tell | |
tell application "System Events" | |
key code 13 using {command down, shift down} | |
delay 0.7 | |
end tell | |
tell application "Finder" | |
set wincount_2 to count of windows | |
end tell | |
if wincount_2 is wincount then | |
tell application "System Events" | |
key code 13 using {command down} | |
delay 0.4 | |
end tell | |
end if | |
tell application "System Events" | |
key code 50 using command down | |
delay 0.4 | |
end tell | |
exit repeat | |
end if | |
end tell | |
end tell | |
end repeat | |
end tell | |
end if | |
end tell | |
end repeat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment