Use SetTitleMatchMode only in part of script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ufunk
Posts: 54
Joined: 17 Jan 2020, 13:45

Use SetTitleMatchMode only in part of script

Post by ufunk » 01 Jul 2022, 15:04

Hi,
I would like to use SetTitleMatchMode, RegEx only on selected hotkeys of a script. Is this possible, if yes, how?
In the following example, I would like that SetTitleMatchMode, RegEx applies only to the first hotkey (now it applies to all hotkeys).
I might have somthing in mind to switch off SetTitleMatchMode, RegEx after the first hotkey?

Code: Select all

SetTitleMatchMode, RegEx

#IfWinActive, ahk_exe notepad.*.exe
^n::
Send, test
Return

^!a::
IfWinExist folder
Run, C:\drive\folder
Return

^!b::
IfWinExist folder_b
Run, C:\drive\folder_b
Return
Sorry if this question might have been asked before, I didn't find it.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Use SetTitleMatchMode only in part of script

Post by boiler » 01 Jul 2022, 15:53

Code: Select all

#If Win("Active", "ahk_exe notepad.*\.exe", "RegEx") ; the actual dot should be escaped as shown
^n::
Send, test
Return

#If Win("Exist", "folder", 2)
^!a::
Run, C:\drive\folder
Return

Win(status, winTitle, matchMode) {
	SetTitleMatchMode, % matchMode
	return Substr(status, 1, 1) = "a" ? WinActive(winTitle) : WinExist(winTitle)
}

ufunk
Posts: 54
Joined: 17 Jan 2020, 13:45

Re: Use SetTitleMatchMode only in part of script

Post by ufunk » 02 Jul 2022, 04:20

Thank you very much for the answer.
Can you please explain what the last lines do? I mean:

Code: Select all

Win(status, winTitle, matchMode) {
	SetTitleMatchMode, % matchMode
	return Substr(status, 1, 1) = "a" ? WinActive(winTitle) : WinExist(winTitle)
}
Because I didn't find Win() in the documentation.

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Use SetTitleMatchMode only in part of script

Post by boiler » 02 Jul 2022, 04:33

You won’t find Win() in the documentation because it is a user-defined function that I just created. I made it so you identify whether the window should be active or just exist, what the WinTitle is, and what match mode you want used by SetTitleMatchMode. It first applies that match mode, then returns the result of either WinActive() or WinExist(), depending on which was specified, using the supplied WinTitle.

That function gets called by the #If directive to determine whether your hotkey should be active or not. You can see from the script how you would call it using Active or Exist and with a mode of RegEx or one of the mode numbers.

ufunk
Posts: 54
Joined: 17 Jan 2020, 13:45

Re: Use SetTitleMatchMode only in part of script

Post by ufunk » 03 Jul 2022, 17:43

Thank you very much.

Post Reply

Return to “Ask for Help (v1)”