How to retrieve the script 'mainwindow' title?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

How to retrieve the script 'mainwindow' title?

Post by c7aesa7r » 28 May 2022, 16:32

i have some scripts running and they contains more than one gui, one of these guis is use to identify the script, the gui title is dynamic being changed according to some settings

atm this is how im searching for the 'main' GUI:

Code: Select all

WinGet, Id, List, ahk_pid %Pid%

Loop, %Id%
{
	Id := Id%A_Index%
	WinGetTitle, Title, ahk_id %Id%
	If (SubStr(Title, 1, 3) = "#0x") {
		;main GUI found
	}
}
i was reading the docs, and found that there's a 'Main Window'
https://www.autohotkey.com/docs/Program.htm#title

is possible to modify/get the title of this specific window? the script itself would modify the title and another different script would read its title

User avatar
Spawnova
Posts: 554
Joined: 08 Jul 2015, 00:12
Contact:

Re: How to retrieve the script 'mainwindow' title?

Post by Spawnova » 28 May 2022, 22:57

One easy way to do this is to apply a prefix to the title, then you can easily find it and extract the code or whatever is unique about it from the title

Code: Select all

prefix := "UniquePrefix - "
code := "0xFF123456"

gui,show,,% prefix code   ;shows a window the the title UniquePrefix - 0xFF123456

SetTitleMatchMode,1 ;requires the title matching to be at the start

WinGetTitle,title,UniquePrefix  ;onlys gets a window that has this unique prefix

if (RegExMatch(title,prefix "(.*)",code))  ;if a valid window then extract all the string after the prefix
	msgbox % "Found title!`n`n" title "`n`nCode is '" code1  "'"   ;value is stored in code1 variable

exitapp

c7aesa7r
Posts: 209
Joined: 02 Jun 2016, 21:09

Re: How to retrieve the script 'mainwindow' title?

Post by c7aesa7r » 29 May 2022, 06:28

Spawnova wrote:
28 May 2022, 22:57
One easy way to do this is to apply a prefix to the title, then you can easily find it and extract the code or whatever is unique about it from the title

Code: Select all

prefix := "UniquePrefix - "
code := "0xFF123456"

gui,show,,% prefix code   ;shows a window the the title UniquePrefix - 0xFF123456

SetTitleMatchMode,1 ;requires the title matching to be at the start

WinGetTitle,title,UniquePrefix  ;onlys gets a window that has this unique prefix

if (RegExMatch(title,prefix "(.*)",code))  ;if a valid window then extract all the string after the prefix
	msgbox % "Found title!`n`n" title "`n`nCode is '" code1  "'"   ;value is stored in code1 variable

exitapp
This method returns only one window title, when you have multiple windows (from different scripts) with the prefix, how to proceed?

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to retrieve the script 'mainwindow' title?

Post by lexikos » 30 May 2022, 03:01

c7aesa7r wrote:is possible to modify/get the title of this specific window?
Yes, just use WinSetTitle/WinGetTitle as with any other window.

As with any other hidden window, if you don't show the main window first (with something like ListLines), you will need DetectHiddenWindows On.

Keep in mind that if you change the main window's title, #SingleInstance will not work and Reload will not terminate the old instance.

Also, if each script has only one unique instance, you can uniquely identify each script's main window by a combination of the script's full path and ahk_class AutoHotkey.

Post Reply

Return to “Ask for Help (v1)”