October 13, 2020

AutoHotkey: Gui to list and select a window

For getting the current Teams window, I have looked for a GUI Utility in AutoHotkey to select a window from a list.
I present here my final solution and code for this.  

Main Code

Code is available in my ahk github-> Lib-> WinListBox.
This is based on the ListBox utility presented in a previous post with an added button to Activate the window.

The listbox will stay always on top so you can activate the windows in the background.

Strange RegExMatch issue

I have faced an issue I have given up finding out why.
In the WinListBox while extracting the WinId from the Text using a regexp the capturing subpattern is not returned properly.
As workaround I have removed manually:
WinListBoxButtonActivate: ; "Activate" button pressed
        Gui, Submit, NoHide
        RegExMatch(LB,"\{([^}]*)\}$",WinId)
        ; %WinId1% does not work?? -> workaround remove 
        WinId := StrReplace(WinId,"{","")
        WinId := StrReplace(WinId,"}","")
        WinActivate, ahk_id %WinId%
    return

Example Usage

Prefiltering the list of Windows to be displayed.
You can apply intelligent filter (example exclude one window by Id or Title matching special criteria).

From Teams_GetMeetingWindow:

Teams_GetMeetingWindow(){

WinGet, Win, List, ahk_exe Teams.exe
TeamsMainWinId := Teams_GetMainWindow()
TeamsMeetingWinId := PowerTools_RegRead("TeamsMeetingWinId")
WinCount := 0
Select := 0
Loop %Win% {
    WinId := Win%A_Index%
    If (WinId = TeamsMainWinId) ; Exclude Main Teams Window
        Continue
    WinGetTitle, Title, % "ahk_id " WinId    

    IfEqual, Title,, Continue
    Title := StrReplace(Title," | Microsoft Teams","")
    If InStr(Title,",")  ; Exclude windows with , in the title (Popped-out 1-1 chat)
        Continue
    WinList .= ( (WinList<>"") ? "|" : "" ) Title "  {" WinId "}"
    WinCount++

    If WinId = %TeamsMeetingWinId% 
        Select := WinCount 
} ; End Loop

If (WinCount = 0)
    return
If (WinCount = 1) { ; only one other window
    RegExMatch(WinList,"\{([^}]*)\}$",WinId)
    TeamsMeetingWinId := WinId1
    PowerTools_RegWrite("TeamsMeetingWinId",TeamsMeetingWinId)
    return TeamsMeetingWinId
}

LB := WinListBox("Teams Meeting Window", , WinList, Select)
RegExMatch(LB,"\{([^}]*)\}$",WinId)
TeamsMeetingWinId := WinId1
PowerTools_RegWrite("TeamsMeetingWinId",TeamsMeetingWinId)
return TeamsMeetingWinId

} ; eofun

References

No comments:

Post a Comment