ahk_exe and RegEx Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
autocart
Posts: 214
Joined: 12 May 2014, 07:42

ahk_exe and RegEx

11 Jun 2019, 09:28

Hi all,
I would like to use RegEx title match mode and scan for an exact match of the exe name.
Therefore I would like to use the ^ and $ anchors. However the ^ does not really work with ahk_exe.
To go with an example, I am using it on the SciTe4AHK editor.
The code

Code: Select all

SetTitleMatchMode RegEx

If WinActive("ahk_exe ^SciTE\.exe$")
	MsgBox true
else
	MsgBox false
results in MsgBox false.
The code

Code: Select all

SetTitleMatchMode RegEx

If WinActive("ahk_exe ^.{34}SciTE\.exe$")
	MsgBox true
else
	MsgBox false
results in MsgBox true.
I can work around it using the \b anchor like this

Code: Select all

SetTitleMatchMode RegEx

If WinActive("ahk_exe \bSciTE\.exe$")
	MsgBox true
else
	MsgBox false
which also results in MsgBox true, however, this is not consistent, not good looking and also not 100% safe.

1) WHY does it not work? What chars are those 34 chars?
2) Is there a way around using the . {34} part and still use the ^ anchor?

Many thx in advance. Regards, S.
Rohwedder
Posts: 7768
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: ahk_exe and RegEx

11 Jun 2019, 09:42

Hallo,
untested (I do not use the SciTe4AHK editor):

Code: Select all

SetTitleMatchMode RegEx

If WinActive("^ahk_exe SciTE\.exe$")
	MsgBox true
else
	MsgBox false
autocart
Posts: 214
Joined: 12 May 2014, 07:42

Re: ahk_exe and RegEx

11 Jun 2019, 10:01

Rohwedder wrote:
11 Jun 2019, 09:42

Code: Select all

SetTitleMatchMode RegEx

If WinActive("^ahk_exe SciTE\.exe$")
Thx, but that does not work anywhere in ahk because it is not correct ahk syntax to begin with.
autocart
Posts: 214
Joined: 12 May 2014, 07:42

Re: ahk_exe and RegEx  Topic is solved

11 Jun 2019, 11:00

Hi all,
I figured it out myself.
If RegEx is used for title match mode, then the whole path is compared for the exe.

The following code returns MsgBox true started from within SciTE editor (if SciTE.exe is located in that specific path):

Code: Select all

SetTitleMatchMode RegEx

If WinActive("ahk_exe ^C:\\Program Files\\AutoHotkey\\SciTE\\SciTE\.exe$")
	MsgBox true
else
	MsgBox false
EDIT:
I found out that the help did mention it all along: https://www.autohotkey.com/docs/misc/WinTitle.htm#ahk_exe :oops:
To my defense, it is a little confusing since SetTitleMatchMode 1 (A window's title must start with the specified WinTitle to be a match.) or 3 (A window's title must exactly match WinTitle to be a match.) match the exe also without path.

So to match an exact exe name with RegEx turned on but without path, the following code is the solution:

Code: Select all

SetTitleMatchMode RegEx

If WinActive("ahk_exe \\name\.exe$")

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 368 guests