Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
branch3 opened this issue Jun 19, 2024 · 0 comments

Comments

@branch3
Copy link

branch3 commented Jun 19, 2024

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 :

  • launch the app if not found
  • switch to the app (and virtual desktop) if found, and make it active and upfront
  • cycle between windows of this app if multiple are found (very useful for Edge, Notion...)
  • minimize the app if only 1 window exists (useful to quickly pop up and down the Agenda, Whatsapp, Slack apps...)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant