Page 1 of 1

If WinExist, ClassNN

Posted: 18 Mar 2017, 08:29
by Vh_
Hello Fellow AHK friends!

I have been on the hunt to find a working functioning method to execute my idea.

GOAL: Wait for a classNN to exist, then continue.

NOTE: This isn't my actual code for what I am trying to do, all of that is stored at work. This is just an example.

My code that isn't working.....below.
------------------------------
-----------------------------


#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance,Force

home::

;not sure how to add a loop in here, but I think I need one in order for it to wait for class

#If WinExist("ahk_class button1")
{
ControlClick, Button1, MacroGamer, , Left, 1
sleep, 10
return
}


IfWinNotExist("ahk_class button1") ;this part I don't necessarily need, it's just for me to test if it worked or not. "button1" WILL exist, just needs to wait for it.
{

Msgbox not found
}

return

---------------------------
---------------------------


So while this code may get working, I need to also comment that the program I'm using at work has an environment inside of it. So to have WinWaitActive, yadaya - won't work. This is why I want to wait for a class. I am closing accounts at work.

I hope this is enough information and my goal is clear. Any help is appreciated.

Thank you,

Vh_

Re: If WinExist, ClassNN

Posted: 18 Mar 2017, 09:33
by Helgef
You might be interested in SetTimer, eg,

Code: Select all

#Persistent
SetTimer, checkIfClassExist, 100

return
checkIfClassExist:
	If WinExist("ahk_class button1")
	{
		ControlClick, Button1, MacroGamer, , Left, 1
		sleep, 10
		return
	}
	else ;this part I don't necessarily need, it's just for me to test if it worked or not. "button1" WILL exist, just needs to wait for it.
	{
		Msgbox not found 
	}
return

Re: If WinExist, ClassNN

Posted: 18 Mar 2017, 10:13
by Vh_
Helgef wrote:You might be interested in SetTimer, eg,

Code: Select all

#Persistent
SetTimer, checkIfClassExist, 100

return
checkIfClassExist:
	If WinExist("ahk_class button1")
	{
		ControlClick, Button1, MacroGamer, , Left, 1
		sleep, 10
		return
	}
	else ;this part I don't necessarily need, it's just for me to test if it worked or not. "button1" WILL exist, just needs to wait for it.
	{
		Msgbox not found 
	}
return
this code doesn't work :/

Re: If WinExist, ClassNN

Posted: 18 Mar 2017, 10:15
by Vh_
Still hops directly to msgbox when Button1 is present

Re: If WinExist, ClassNN

Posted: 18 Mar 2017, 10:41
by Helgef
That means that WinExist(...) returns 0. See WinExist, WinTitle and ControlClick.

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 09:30
by Vh__
I read these docs and can't seemingly connect the dots.. can you give an example please?

Thank you! :)

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 09:54
by Helgef
My guess is, Button1 is the ClassNN of a control, not the window class of the window in which the control resides. So, instead of WinExist("ahk_class Button1"), you migth identify the window which holds the control (Button1) by other means, eg, open WindowSpy and hover the mouse over the window, at the top, you'll see ahk_class something, use that instead, or ahk_exe something.exe. Here is an example,
Image

Good luck.

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 13:49
by Vh_
Thanks for the reply!

Here is the thing.While I can tell it to identify with the program name, my purpose is for a program at work that has an environment inside of the window. So the program will exist. I want it to execute upon class existence rather window exist. does that make sense?

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 14:06
by Helgef
Consider this,

Code: Select all

Gui, add, button,, hi
Gui show
MsgBox, % WinExist("ahk_class Button1")
ExitApp
You can use WinGet to get a list of controls for a window of your choise.

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 14:22
by jeeswg

Code: Select all

q:: ;check control (ClassNN) exists
;try this on Notepad and on Notepad's Find dialog
ControlGet, hCtl, Hwnd, , Button1, A
if hCtl
	MsgBox % "control exists"
else
	MsgBox % "control doesn't exist"
Return

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 19:57
by Vh_
Im sorry jeeswg, control existed in either state.. I am starting to think this is not possible with AHK. :(

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 20:19
by Vh_
ok guys. here is the actual code. See line 42 with ControlClick
---------------------------------------------------------------------------------
---------------------------------------------------------------------------
----------------------------------------------------------------------
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance,Force


Gui, +Caption
Gui, +AlwaysOnTop
Gui, Add, Text, x10 y5 +BackgroundTrans cWhite, Status ;add colored text in options, (after x/y coord. eg, cBlue)
Gui, Add, DropDownList, x10 y20 w180 h100 vStatus, CLOSED|INACTIVE|ACTIVE|PENDING
Gui, Add, Text, x10 y45 +BackgroundTrans cWhite, Note
Gui, Add, Edit, x10 y60 w180 h50 vMyEdit
Gui, Add, Text, x10 y114 +BackgroundTrans cWhite, Bind Key
Gui, Add, Hotkey, x10 y+4 w180 h20, vChosenHotkey gHotkey
Gui, Add, Text, x10 y160 +BackgroundTrans cWhite, Run without key (optional)
Gui, Add, Button, x10 y175 w180 h30 gOK , Run
Gui, Color, 474747

Gui, Show, w200 h220, Assistant ;Window title is Account Assistant

return

; Labels
;---------------
;----------------------
;-------------------------------
OK:

Gui, Submit, NoHide

SetTitleMatchMode, 2
WinActivate, PAYCheque
IfWinActive, PAYCheque

{

ControlClick, ThunderRT6ListBox1 ;This is where I want it to wait for the class to exist before continuing

sleep, 1

if (Status = "CLOSED")
{
Send, C
} else if (Status = "INACTIVE") {
Send, I
} else if (Status = "ACTIVE") {
Send, A
else if (Status = "PENDING") {
Send, P
}

Send, {tab}
sleep, 1
Send, %MyEdit%
}
return

Hotkey:

If (ChosenHotkey)

{
Hotkey, %ChosenHotkey%, OK
}

return

Re: If WinExist, ClassNN  Topic is solved

Posted: 19 Mar 2017, 20:42
by tidbit
as mentioned on irc (for others to see), just toss in

Code: Select all

loop
{
	sleep, 50
	ControlGet, hCtl, Hwnd, , ThunderRT6ListBox1, INSERT THE WINDOW TITLE HERE
}
until (errorlevel=0)
where "INSERT THE WINDOW TITLE HERE" is the ahk_class (as shown by helgef) or the actual title, if it never changes

Re: If WinExist, ClassNN

Posted: 19 Mar 2017, 20:43
by Nightwolf85
It looks to me you could use the example code jeeswg gave to loop check if the control exists before continuing.
something like:

Code: Select all

loop
{
	ControlGet, hCtl, Hwnd, , ThunderRT6ListBox1, A ; (If its not in the active window change 'A' to be the window it is in, either its name or class.)
	if hCtl
		Break
}
ControlClick, ThunderRT6ListBox1
I'm not sure this is the best way to accomplish what you want but it should work.

**Edit I guess I'm a tad bit too slow.

Re: If WinExist, ClassNN

Posted: 20 Mar 2017, 00:53
by jeeswg
You could also check if the control is enabled or visible.

Code: Select all

q:: ;check if control exists/is enabled/is visible (+ get styles)
vCtlClassNN := "Button1"
WinGet, hWnd, ID, A
ControlGet, hCtl, Hwnd, , % vCtlClassNN, % "ahk_id " hWnd
ControlGet, vIsEnabled, Enabled, , % vCtlClassNN, % "ahk_id " hWnd
ControlGet, vIsVisible, Visible, , % vCtlClassNN, % "ahk_id " hWnd
ControlGet, vCtlStyle, Style, , % vCtlClassNN, % "ahk_id " hWnd
ControlGet, vCtlExStyle, ExStyle, , % vCtlClassNN, % "ahk_id " hWnd

vOutput := hCtl " -- " vIsEnabled " -- " vIsVisible " -- " vCtlStyle " -- " vCtlExStyle
Clipboard := vOutput
MsgBox % vOutput
Return

Re: If WinExist, ClassNN

Posted: 20 Mar 2017, 17:17
by Vh_
Ladies & Gentlemen. I must extend great appreciation for joining me on this quest. I deem this solved. Thank you for your patience and EXCELLENT INPUT, ALL!

Vh
:D