Build keyboard shortcut for “minimize all windows of current app except the current window”

automatorshortcut

I'm looking to bind a keyboard shortcut to the action "minimize all windows from the current application except the current window." (The app in question is always Google Chrome.)

(Sort of like ⌘ + ⌥ + h, which translates to "hide all applications except the current application".)

I've never bound a new shortcut, but I suspect I could figure it out once I have a working AppleScript for the task. I also use Better Touch Tool, in case it is easier to go through that app.

This question asks the same thing, but the answers are not bindable to a simple keyboard shortcut as far as I can tell.

This question asks the same thing (except specifically for Safari), and a user suggests the AppleScript

tell application "Safari"
    activate
    tell application "System Events"
        keystroke "h" using {command down, option down}
    end tell
    set miniaturized of (windows whose index is not 1) to true
end tell

When I replace "Safari" with "Google Chrome" in the script, and try to run it from Script Editor, I get the error

Script error: Google Chrome got an error: Can’t make miniaturized of every window whose index ≠ 1 into type specifier.

Can anyone help? Thanks

Best Answer

The following example AppleScript code will minimize all but the front window in Google Chrome:

tell application "System Events" to ¬
    set miniaturized of windows of application ¬
        "Google Chrome" whose index is not equal to 1 to true

Since you already have BetterTouchTool and assuming it can run AppleScript code or an AppleScript .scrpt file with a keyboard shortcut, that is probably the easiest way to go to trigger the code.

That said, my preferred method for running AppleScript scripts with a keyboard shortcut, is to use a third-party application named FastScripts, however you can also use Automator with a Run AppleScript action as a Service/Quick Action and assign it a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services

  • The keyboard shortcut assigned to the Service/Quick Action needs to not conflict with a default keyboard shortcut for whichever application is frontmost at the time it's pressed, otherwise there may be unwanted behavior.

  • FastScripts can be run as a free app, up to 10 keyboard shortcuts, or upgraded for $24.95 USD to unlock unlimited keyboard shortcuts. I have no affiliation with Red Sweater Software, LLC, other then as a user of FastScripts.