SetTitleMatchMode RegEx applied only to directives

Get help with using AutoHotkey and its commands and hotkeys
paik1002
Posts: 337
Joined: 28 Nov 2015, 02:45

SetTitleMatchMode RegEx applied only to directives

Yesterday, 13:11

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.
A_AhkUser
Posts: 968
Joined: 06 Mar 2017, 16:18
Location: France

Re: SetTitleMatchMode RegEx applied only to directives

Yesterday, 15:31

paik1002 wrote:
Yesterday, 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
User avatar
lvalkov
Posts: 9
Joined: 08 Mar 2019, 16:39

Re: SetTitleMatchMode RegEx applied only to directives

Today, 09:31

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)

Return to “Ask For Help”

Who is online

Users browsing this forum: alonzam99, bojo5150, esp901, Flipeador, Google [Bot], paik1002, teadrinker and 37 guests