Left click App in dock to Minimize/Unminize (not Hide), if only one Window

Inspiration from how Windows manages clicking taskbar icons of apps which I find much more natural.

If there's only one active window of the app and you click the App in the Dock:

  1. If the current Window is the Frontmost, then Minimize the App (not Hide).
  2. If the current Window is not the Frontmost Window, then Activate the App (to bring it to front).
  3. If the current Window is Hidden, then Activate the App (to bring it to front).
  4. If the Window of the app is Minimized, then Unminimize the App.

It only works like this on Windows when there is only one Window of the app, not two or more.

I got some inspiration from yw4z's Enhanced Dock for macOS which has working "Minimize/Unminimize all Windows" as a Named Trigger, all credit to him:

(async()=>{
    AS=s=>(runAppleScript(s))
    PA=n=>{trigger_action({json:JSON.stringify({BTTPredefinedActionType:n})})}

    a=await get_string_variable("hovered_element_details")
    v=i=>(a.split('AX'+i+':  "')[1].split('"')[0])

    F=v('Subrole').slice(2,4)
    
    // Extract the application name from the URL
    let appName = decodeURIComponent(v('URL')).slice(7,-1).split('/').pop().split('.')[0]

    if(F=="Ap" && (v('IsApplicationRunning')*1)){
        // Use AppleScript to handle the logic for minimizing/unminimizing
        await AS(`
            tell application "System Events" to tell process "${appName}"
                set windowCount to count of windows
                if windowCount is 1 then
                    set theWindow to first window
                    set isMinimized to value of attribute "AXMinimized" of theWindow
                    if isMinimized then
                        -- Unminimize the window
                        set value of attribute "AXMinimized" of theWindow to false
                    else
                        -- Minimize the window
                        set value of attribute "AXMinimized" of theWindow to true
                    end if
                end if
            end tell
        `);
    } else if(F=="Sp"||F=="Tr"||F=="Se"||F==""){
        AS(`beep`)
    }

    returnToBTT(f);
})();

The only problem I have with this is that I want it to trigger on Left Click, which naturally unminimizes/Activates the Window, but then gets triggered to minimize it again with the script. So I have to run the script with a special modifier like Shift + Left Click to Unminimize.

There's unfortunately no none script way to detect if a Window is minimized or not in BTT? It would be great to have new triggers:

  • "Show / Minimize Hovered App in Dock"
  • "Minimize Currently Hovered App in Dock"
  • "Minimize App Under Cursor"
  • or "Show / Minimize Specific Application" where you could pass the name from the Dock.

I'm no where near good enough to do this script wise in javascript or applescript.

I am trying to hide instead of minimize. But anyway can you explain how are you getting the app name ?

I understand it gets the hover element data which is like xml ? Or json ? And then extracting AxTitle or something?

What would be the method of getting the app name in plain apple script ?

Thanks.

And the problem you are facing you need to use cycle through action. Then first click activate if not running or minimised. Second click runs scripts and decides wether minimise or not.

I managed to make it work after a second try: BetterTouchTool Preset Sharing Platform

Hi @Andreas_Jakobsson ,

Although I successfully imported your presets file (5 presets in total in the file: 2 under "Trackpad Gestures", 2 under "Normal Mouse Buttons" and 1 under "Automations, Named & Other Triggers"), the click-to-minimize function when clicking on the dock icon of an active app, does not work. Can you please help in any way? In case it matters, I have an M2 Pro Macbook Pro and the click-to-minimize works neither from its touchpad nor from my Logitech MX Master 3 for Mac mouse.

Here is a screenshot in case it helps:

When I click on a dock icon, I get the following pop-up in which I can choose one of the two rows but nothing happens regardless of which one I choose (below I clicked twice on the finder icon but the same happens in the other apps too):

Screenshot 2024-11-13 at 15.52.07