SetTitleMatchMode 2 is placed at the beginning of my script set as the default matching mode.
However, is it possible to apply SetTitleMatchMode RegEx only to directives.
In other words, I would like to partially match the window title for directives such as #IfWinActive, but not to titles for commands such as IfWinActive.
SetTitleMatchMode RegEx applied only to directives
Re: SetTitleMatchMode RegEx applied only to directives
Hi paik1002,
The following code shows the easiest way I can think of to handle this situation for any kind of expression - create a simple function wrapper for the SetTilteMatchMode command and make it behave as the ComObjError function (specifically, by returning the setting which was in effect before the function was called):
Code: Select all
SetTitleMatchMode, 2
#If ((matchmode:=SetTitleMatchMode("RegEx")) && ((YOUREXPRESSIONWHICHEVALUATESTOTRUEORFALSE) || !SetTitleMatchMode(matchmode)))
F4::MsgBox % A_ThisHotkey
#If
return
SetTitleMatchMode(titleMatchMode) {
lastFound := A_TitleMatchMode
SetTitleMatchMode % titleMatchMode
return lastFound
}
If you intend to use only a Win(Not)Active-like checking, you probably better use something following this pattern:
Code: Select all
SetTitleMatchMode, 2
#If !WinActiveRegEx("(.*- )?YouTube(?= - Mozilla Firefox$) ahk_class MozillaWindowClass ahk_exe firefox.exe")
F4::MsgBox % A_ThisHotkey
#If
WinActiveRegEx(_wintitle) {
local _titleMatchMode := A_TitleMatchMode
SetTitleMatchMode, RegEx
_hwnd := WinActive(_wintitle)
SetTitleMatchMode % _titleMatchMode
return _hwnd
}
Spoiler
Hope this helps. cheers
Re: SetTitleMatchMode RegEx applied only to directives
Interesting 3rd example. Here is a similar one. Disclaimer: AHK v2
Code: Select all
tmm_wrapper(fn, mode, speed := "") {
return (Args*) => (
orig_mode := A_TitleMatchMode,
orig_speed := A_TitleMatchModeSpeed,
SetTitleMatchMode(mode),
retVal := fn.Call(Args*),
SetTitleMatchMode(orig_mode),
SetTitleMatchMode(orig_speed),
retVal
)
}
WinExistRegEx := tmm_wrapper(Func('WinExist'), 'RegEx')
WinWaitActiveRegEx := tmm_wrapper(Func('WinWaitActive'), 'RegEx')
WinCloseRegEx := tmm_wrapper(Func('WinClose'), 'RegEx')
winTitle := 'i)^unt..le. - not.pad$'
Run 'notepad'
WinWaitActiveRegEx.Call(winTitle)
MsgBox WinExistRegEx.Call(winTitle)
WinCloseRegEx.Call(winTitle)
Who is online
Users browsing this forum: Google [Bot], peter_ahk and 96 guests