Page 1 of 1

SetTitleMatchMode RegEx applied only to directives

Posted: 17 Mar 2019, 13:11
by paik1002
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.

Re: SetTitleMatchMode RegEx applied only to directives

Posted: 17 Mar 2019, 15:31
by A_AhkUser
paik1002 wrote:
17 Mar 2019, 13:11
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
}
that means that you need to subsume all your #If-expressions one by one into such a meta-expression...

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
}
An other idea using objects:
Spoiler


Hope this helps. cheers

Re: SetTitleMatchMode RegEx applied only to directives

Posted: 18 Mar 2019, 09:31
by lvalkov
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)