November 4, 2021

RegEx Pipe sympol

I have stumbled upon a regex trap that is most likely quite common in AutoHotkey.

Use Case

Check if WinTitle ends with | ApplicationName. It is quite common to have appended after a pipe in Windows the application name - especially for browser windows.

In my case it was for Microsoft Teams opened in the browser, see Teams Shortcuts support for browser/ app.

Trap

In RegEx the pipe symbol has an OR effect.

So this function

SetTitleMatchMode, RegEx
If WinActive("| Microsoft Teams$")
    return True
return False

will always return true.

Fix

You need to escape the pipe symbol with a \ backslash

This is the new Teams_IsWinActive function:

Teams_IsWinActive(){
; Check if Active window is Teams client or a Browser/App window with a Teams url opened

If WinActive("ahk_exe Teams.exe")
    return True
SetTitleMatchMode, RegEx
If WinActive("\| Microsoft Teams$")
    return True
return False
}

See also

Teams Shortcuts support for browser/ app | Thierry Dalon's Blog

No comments:

Post a Comment