MacOS – Minimize other windows of an application

macoswindow-manager

Is it possible to do the equivalent of "Hide other Applications" for Windows of the same application?

Say i have around 4 main windows open for Chrome and each has 10 tabs. I would like to switch to Window 3 of Chrome and hide the other Chrome windows (Window 1, 2 & 4). Essentially i want to hit a CMD+M on the other "windows".

(this is different from Minimize/Hide other Applications that would allow me to switch to Safari and hide other applications like Chrome).

A keyboard shortcut would be awesome, but cursory googling tells me that i'll have to settle with an applescript?

Cheers.

Best Answer

This doesn't work with Preview or other aplications that don't support AppleScript.

tell application (path to frontmost application as text)
    try
        set miniaturized of windows where visible is true and index is not 1 and miniaturizable is true to true
    end try
    try -- at least Finder
        set collapsed of windows where visible is true and index is not 1 and closeable is true to true
    end try
end tell

I don't know how to get the focused window or hidden windows with System Events. The focused window isn't window 1 or AXMain or AXFocused. Setting AXMinimized to true didn't work with Tweetbot either.

tell application "System Events" to tell (process 1 where frontmost is true)
    windows where value of attribute "AXMinimized" is false
    --repeat with w in result
    --try
    --set value of attribute "AXMinimized" of w to true
    --end try
    --end repeat
end tell

There are many ways to assign shortcuts to scripts, but shortcuts for Automator services don't work reliably in 10.7 or 10.8.