Minimize/unminize all windows in the active application

Dec 03, '09 07:30:01AM

Contributed by: loren_ryter

Now that it seems increasingly unlikely that Unsanity will update WindowShade X for Snow Leopard, I wanted some kind of substitute. In Snow Leopard, you can minimize (actually "miniaturize") windows to the program's Dock icon (enable this in the Dock System Preferences panel), and you can double-click on a window title bar to do this. Nice and out of the way.

But getting such windows back requires excessive mouse movements. (Either activate Exposé and click on the window, or right-click on the application's Dock icon and select the window by name from a menu. Then you must mouse back up to the window. If you do this constantly, it gets very frustrating and prone to strain.

This AppleScript will either miniaturize all windows in the active (frontmost) application, or un-miniaturize them all. The difference between the two behaviors is controlled by one script property (theval). So create two versions of the script, one with theval set to true (to miniaturize), and one with theval set to false (to un-miniaturize). Here's the "miniaturize" version:

(*
Frontmost miniaturize / unminiaturize
by Wooden Brain Concepts
11-18-2009

miniaturize (to dock) or unminiaturize all windows in the frontmost app.
This works in many but not all applications.
Suggest using it with a keyboard macro program and/or a mapped mouse button, etc.

*)

-- theval = true : miniaturize ; theval = false : unminiaturize 
property theval : true
-- require windows to be resizable?  (default: false)
property requireresizable : false
-- must all windows have a title to unminiaturize only? (default: true)
property windows_must_have_titles : true
--window names to exclude when unminiaturizing only (default: {"Downloads", "Invite"})
property exclusions : {"Downloads", "Invite"}
-- gui scripting main window for these apps (by bundle id) (default: {"com.microsoft.entourage", "ws.agile.1Password"})
property guiscripting : {"com.microsoft.entourage", "ws.agile.1Password"}

tell application "System Events"
  set bi to bundle identifier of (some process whose frontmost is true)
end tell

(*
-- testing only
--set bi to "com.apple.finder"
set bi to "com.apple.itunes"
set bi to "com.apple.systempreferences"
*)

if not theval and (bi is in guiscripting) then
  (*
  this is special handling for some unscriptable apps.  this whole conditional can be eliminated if you don't use it. it requires "Enable access for assistive devices" in Universal Access Preference Pane to be on to enable GUI access scripting. have not yet figured out how to miniaturize to dock.
  *)
  set thisapp to ""
  set theindex to 1
  if bi is "com.microsoft.entourage" then
    set thisapp to "Microsoft Entourage"
  else if bi is "ws.agile.1Password" then
    set thisapp to "1Password"
  end if
  if not theval and thisapp is not "" then
    tell application thisapp to activate
    try
      tell application "System Events"
        tell process thisapp
          tell menu bar 1
            tell menu bar item "Window"
              tell menu "Window"
                if bi is "com.microsoft.entourage" then
                  set winmenuitems to name of every menu item
                  set theindex to 11
                  -- get best guess at main window
                  repeat with i from 11 to (count winmenuitems)
                    if (item i of winmenuitems contains "—") or (item i of winmenuitems begins with "Folders") then
                      set theindex to i
                      exit repeat
                    end if
                  end repeat
                  click menu item theindex
                else if bi is "ws.agile.1Password" then
                  click menu item "Main Window"
                end if
              end tell
            end tell
          end tell
        end tell
      end tell
    on error myerr
      -- log myerr
    end try
  end if
else if bi is "com.apple.itunes" then
  -- special case for itunes :  minimize (mini-player) rather than miniaturize (to dock) the main iTunes window
  tell application "iTunes"
    set minimized of browser window 1 to theval
    activate
  end tell
else
  tell application id bi
    try
      set wins to every window whose closeable is true
      repeat with awin in wins
        try
          if bi is "com.apple.finder" then
            using terms from application "Finder"
              -- finder windows use the property "collapsed" rather than "miniaturized" -- annoying
              if closeable of awin and ((resizable of awin is true) or not requireresizable) and ((theval and visible of awin) or (not theval and (get collapsed of awin) and name of awin is not in exclusions)) and (not windows_must_have_titles or (windows_must_have_titles and name of awin is not "")) then
                try
                  set collapsed of awin to theval
                end try
              end if
            end using terms from
          else
            if closeable of awin and ((resizable of awin is true) or not requireresizable) and ((theval and visible of awin) or (not theval and miniaturized of awin and name of awin is not in exclusions)) and (not windows_must_have_titles or (windows_must_have_titles and name of awin is not "")) then
              try
                set miniaturized of awin to theval
              end try
            end if
          end if
        on error myerr
          -- log if desired
          --log myerr
        end try
      end repeat
    on error myerr
      -- log if desired
      -- log myerr
    end try
    activate
  end tell
end if
Each script can then be attached to a keyboard combo (with a keyboard macro program such as QuicKeys). I have also triggered the scripts with a programmable trackball (using Kensington MouseWorks), so I can perform both actions with the trackball without moving the mouse pointer. (Similar results could be achieved with other mouse drivers, I believe.)

Certain options are definable in the script properties. This script works in many but not all applications, and some workarounds using GUI access scripting were included for MS Entourage and 1Password.

[robg adds: I tested this script in both 10.5 and 10.6, and it works as described in both versions of the OS. As noted, it will not work in all applications.]

Comments (19)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2009111905480044