How to Hide All Windows of an App Other Than the Front One

Okay, I've fiddled with this quite a bit:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/04/10 06:00
# dMod: 2016/04/13 06:37
# Appl: System Events
# Task: Minimize all Windows Except the Front One in the Front App.
# Tags: @Applescript, @Script, @System_Events, @Minimize, @Windows
------------------------------------------------------------

tell application "System Events"
  tell (first process whose frontmost is true)
    set winList to windows whose subrole is "AXStandardWindow"
    
    if length of winList > 1 then
      set winList to rest of winList
      
      repeat with theWindow in winList
        try
          tell theWindow to set value of attribute "AXMinimized" to true
        end try
      end repeat
      
    end if
    
  end tell
end tell
------------------------------------------------------------

So far it's worked with every app I've tried it on.

-Chris

Edited 2016/04/13 06:38 CST to fix the flaw Alain noticed below.

2 Likes