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.