Do I have to put `SetTitleMatchMode` on top of the script?

Sample script:

#NoEnv
#Warn
#SingleInstance Force

#IfWinActive Foo ahk_exe foo.exe
  !A::Send Foo

SetTitleMatchMode Regex

#IfWinActive Bar$ ahk_exe bar.exe
  !A::Send Bar

When running it, it throws:

enter image description here

I want to apply SetTitleMatchMode Regex only to bar.exe, is it achievable without putting it on top of the script?


I don't understand your script, but the answer is that you don't have to place it at the top. It will affect all following commands until a return.

Proof of concept script:

F1::
   MsgBox, This should give 1`nA_TitleMatchMode=%A_TitleMatchMode%
   SetTitleMatchMode, 2
   MsgBox, This should give 2`nA_TitleMatchMode=%A_TitleMatchMode%
   SetTitleMatchMode, regex
   MsgBox, This should give regex`nA_TitleMatchMode=%A_TitleMatchMode%
return

F2::
   MsgBox, This should give 1`nA_TitleMatchMode=%A_TitleMatchMode%
return

To be convinced, run the above and press consecutively F2, F1, F2.

Each key-press starts a new thread, so SetTitleMatchMode is re-initialized to 1.


SetTitleMatchMode is redundant. Your code should look like (I didn't test):

#NOEnv
#Warn
#SingleInstance Force
SetTitleMatchMode Regex

!A::
if WinActive("Foo ahk_exe foo.exe")
  MsgBox, Foo
if WinActive("Bar$ ahk_exe bar.exe")
  MsgBox, Bar