FuPeiJiang / VD.ahk

Windows Virtual Desktop, AutoHotkey, Windows 11 support, Windows Server 2022, switch desktop, move window(wintitle) to current desktop; createDesktop, PinWindow, getCount, getDesktopNumOfWindow -> mute all windows in Virtual Desktop
MIT License
319 stars 45 forks source link

Autohotkey example function to use regex, and cycle between windows or minimize #71

Open branch3 opened 2 weeks ago

branch3 commented 2 weeks ago

Hi. Since you suggested we share our functions, I'm using your VD class for a long time.

1000x of times a day I'm using shortcuts to switch to an app, like ALT+E for Edge, ALT+A for Agenda... etc

Whenever I'm using this shortcut I want to go to a specific window of the corresponding app, so I want the shortcut to either :

Here's my custom function based on your VD class :

; function based on VD._tryGetValidWindow
; modified to return the list of all matching valid windows if mutliples are found
; and to accept regex
tryGetValidWindow_list(wintitle, useRegEx:=false) {
    bak_DetectHiddenWindows:=A_DetectHiddenWindows
    bak_TitleMatchMode:=A_TitleMatchMode
    DetectHiddenWindows, on
    SetTitleMatchMode, 2
    if useRegEx {
        SetTitleMatchMode, RegEx
    }

    WinGet, outHwndList, List, % wintitle
    returnValue:=[]

    loop % outHwndList {
        theHwnd:=outHwndList%A_Index%+0
        arr_success_pView_hWnd:=VD._isValidWindow(theHwnd)
        pView:=arr_success_pView_hWnd[2]
        if (pView) {
            returnValue.push([arr_success_pView_hWnd[3], pView])
        }
    }

    SetTitleMatchMode % bak_TitleMatchMode
    DetectHiddenWindows % bak_DetectHiddenWindows
    return returnValue
}

; Function based on VD.goToDesktopOfWindow
; modified to switch between the list of active windows if many match,
; and to minimize the window if there's only 1 window and it's already active
; and to accept regex
goToDesktopOfWindow_cycleOrMinimize(wintitle, minimizeIfActive:=true, useRegEx:=false) {

    ; search for matching window
    found:=tryGetValidWindow_list(wintitle, useRegEx)
    nb_found:=found.Length()

    if (nb_found==0) {
        toolTip_custom("Not found, launching app") ; DEBUG
        return -1 ;for false
    }
    ; We found at least 1 matching window
    theHwnd:=found[1][1]
    thePView:=found[1][2]

    ; select the window to activate if multiples are found :
    activeHwnd := WinExist("A") ; An easy way to retrieve the unique ID of the active window

    ; keep the most recent (top) found window if it's not active
    ; or pick the bottom window if multiples are found and one is already active
    if (nb_found > 1 and activeHwnd == theHwnd) {
        theHwnd:=found[nb_found][1]
        thePView:=found[nb_found][2]
    }

    ; Switch desktop if needed
    desktopNum_ofWindow:=VD._desktopNum_from_pView(thePView)
    needSwitchDesktop:=(VD._desktopNum_from_pView(thePView) != VD.getCurrentDesktopNum())
    if (needSwitchDesktop) {
        VD.goToDesktopNum(desktopNum_ofWindow)
    }

    if (activeHwnd != theHwnd) {
        ; active window is not the targeted
        WinActivate, ahk_id %theHwnd%
    }
    if (activeHwnd == theHwnd and minimizeIfActive and not needSwitchDesktop) {
        WinMinimize ; don't need to specify, it uses the last found window in WinExist
    }
}

And here's how I use it :

!e::        ; === Edge === ;
    ; rename Edge workspace with "nav:" prefix in order to make those workspace windows appears in the shortcut
    found := goToDesktopOfWindow_cycleOrMinimize("(.*Microsoft​ Edge.*|nav:.*)", true, true)
    If (found == -1)
    {
        Run, microsoft-edge:
    }
    MoveMouseToCenterActiveWindow()
return

Thank you again for your work, and hope it can inspire others. Cheers