WinGetTitle Looping Error

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
daqbancrackr
Posts: 12
Joined: 14 Jul 2023, 12:05

WinGetTitle Looping Error

16 May 2024, 08:04

hi all, i'm trying to create a loop which will search for text on screen and when its found, i need it to do an action (thats the short version)

i have this constantly looping -- i commented in my logic behind the coding

Code: Select all

loop
;round and round we go, when do we stop, nobody knows

{
	if WinExist("ahk_exe PharmAssist.exe")
	;check if window is active (or does this just check if its running though not necessarily active?)

	{
		windowName := WinGetTitle("A")
		;get the name of the current window
		
		windowNameText := WinGetText(windowName)
		;get the text from the current window using previous variable
		
		haystack := windowNameText
		needle := "does not belong"
		if instr(haystack, needle)
		;search for the needle in the haystack
		
		{
			CoordMode "Mouse"
			Click(1842,993)
			Sleep 150
			Click(1072,611)
			;if it finds the needle, run this
		}	
		
		else
		{
			sleep 5000	
			;if nothing is found, take a nap
		}
		windowName := ""
		windowNameText := ""
		;figured this would help with the errors to have it start empty (im burnt out at this point) :headwall: 
	}
}
it runs ok -- when it does run.

The title of the screen i want to look at says Ship Orders (user name), so it will change for every user, not sure if i'd have to change the titlemode to regex and do "^Ship Orders" or something like that

they bounce between 2 screens. both are titled Ship Orders (user name) so that shouldn't be an issue, but when i go to another screen thats not called Ship Orders (user name), i get an error -- either an Error: Failed, or a TargetError specific to WinGetText(activeWindow1)

Code: Select all

Error: Failed
	028: {
	029: activeWindow1 := WinGetTitle("A")
▶	030: windowText1 := WinGetText(activeWindow1)
	031: name1 := "Ship Order"
	032: If instr(windowText1, name1)

Error: Failed
029: {
030: windowName := WinGetTitle("A")
▶ 031: windowNameText := WinGetText(windowName)
032: haystack := windowNameText
033: needle := "does not belong"

this is what i need it to do

search the active screen over and over for a string of text which is shown in windows spy visible text.
if it sees it, then do a couple of clicks.
i need it to only be within a specific exe running and a specific window title which always shows Ship Order (user name -- where user name will always change)
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: WinGetTitle Looping Error

16 May 2024, 08:12

daqbancrackr wrote:

Code: Select all

	if WinExist("ahk_exe PharmAssist.exe")
	;check if window is active (or does this just check if its running though not necessarily active?)
It only checks to see if the window exists, hence the name. You want to use WinActive instead of WinExist to determine whether it's active.

By the way, you're introducing new variables for really no reason. This:

Code: Select all

		haystack := windowNameText
		needle := "does not belong"
		if instr(haystack, needle)
...could just be this:

Code: Select all

		if instr(windowNameText, "does not belong")
daqbancrackr
Posts: 12
Joined: 14 Jul 2023, 12:05

Re: WinGetTitle Looping Error

16 May 2024, 08:40

would i be able to further "reduce" that into something like this

if instr(WinGetText(WinGetTitle"A"))
{

}
or is that too much / not proper

anywho

would this be proper?

Code: Select all

loop
{
	if WinActive("ahk_exe PharmAssist.exe")
	;checks that the actual application is active as shown in windows spy
	{
		window1 := WinGetTitle("A")
		window1Text := WinGetText(windowName)
		if instr(window1Name, "Ship Order")
		;checks to make sure i'm on the proper window as shown in windows spy
		{
			windowName := WinGetTitle("A")
			windowNameText := WinGetText(windowName)
			if instr(windowNameText, "does not belong")
			{
				;do what i need here
			}	
		
			else
			{
				sleep 5000	
				;if nothing is found, take a nap
			}
		}
	}
}
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: WinGetTitle Looping Error

16 May 2024, 09:36

Getting the title and then using that is an extra step that only adds ambiguity because then it opens it up to other windows that may also match (or partially match) the title. Just specify the active window directly as the WinTitle parameter without getting its title:

Code: Select all

if  InStr(WinGetText("A"), "Ship Order")

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: TAC109 and 33 guests