Being more generic when using WinActivate and ControlSetText

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mbeck
Posts: 7
Joined: 24 Jan 2017, 17:38

Being more generic when using WinActivate and ControlSetText

04 Dec 2017, 11:31

Good morning everyone,

I was hoping that somebody may have some input for me. I have need to run an AHK script on several different machines. They're all built similarly with the same software, but the window handles seem to be different. In the testing that I did when I wrote the script initially, the handles were the same and it work. I then distributed the script to a fellow staff member and the script failed. I believe that it has to do with the handles. I could be wrong, but I'm going with this theory for now. :-)

So...

Code: Select all

WinActivate, ahk_class WindowsForms10.Window.8.app.0.26ac0ad_r14_ad1 ahk_exe Nuance.PowerScribe360.exe ; activate the window
Sleep, 150
Click, 247, 163 Left, 1 ; left click in the report edit control (this works a bit better than 'controlsettext' commands, which were causing PowerScribe to misbehave on this particular step.
Sleep, 2000 ; give the mouse a short amount of time to move in case the pointer is 3 screens away
Send, {Control Down}{a}{Control Up} ; select all
Sleep, 150
Send, {Delete} ; clear the existing text, if any 
Sleep, 150
'Report' variable
ControlSetText, WindowsForms10.RICHEDIT50W.app.0.26ac0ad_R14_ad11, %Report%, ahk_class WindowsForms10.Window.8.app.0.26ac0ad_r14_ad1 ahk_exe Nuance.PowerScribe360.exe ; replace the text in the powerscribe window with the contents of 'Report' variable
What happens above is the script looks for a specific application's window, then copy some text from the %Report% variable into it. Having tested this on two different machines, the code works.

What I would like to do is try and make this more generic. I want to just use ahk_exe to identify the application and then I need some way to identify the control within it. Does anyone know how to do this?
mbeck
Posts: 7
Joined: 24 Jan 2017, 17:38

Re: Being more generic when using WinActivate and ControlSetText

04 Dec 2017, 12:28

Interesting BoBo. Thank you. I'll investigate that.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Being more generic when using WinActivate and ControlSetText

04 Dec 2017, 14:52

I think the problem is that the classes of the window and controls are changing each time. See this link for some relevant information:

How to account for changing ClassNN info - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=40120

Re. BoBo's link. Interesting, I didn't know this:
SetTitleMatchMode
https://autohotkey.com/docs/commands/Se ... .htm#RegEx
RegEx also applies to ahk_class and ahk_exe
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Being more generic when using WinActivate and ControlSetText

04 Dec 2017, 15:11

I think something like this is going to be a common problem, and I've a written a function for it, for handling controls. SetTitleMatchMode can already handle windows.

Code: Select all

q:: ;Notepad - get nth control with a particular class (RegEx pattern)
WinGet, hWnd, ID, A
vCtlClassNN := JEE_WinGetCtlClassNNRegEx(hWnd, "^E..t$", 1)
;vCtlClassNN := JEE_WinGetCtlClassNNRegEx(hWnd, "^\QWindowsForms10.COMBOBOX.app.0.\E......._r9_ad1$", 1)
;vCtlClassNN := JEE_WinGetCtlClassNNRegEx(hWnd, "^\QWindowsForms10.RICHEDIT50W.app.0.\E......._R14_ad1$", 1)
ControlGetText, vText, % vCtlClassNN, A
MsgBox, % vCtlClassNN "`r`n`r`n" vText
return

JEE_WinGetCtlClassNNRegEx(hWnd, vClassRegEx, vNum:=1)
{
	if !hWnd
		WinGet, hWnd, ID, A
	vCount := 0
	WinGet, vCtlList, ControlList, % "ahk_id " hWnd
	Loop, Parse, vCtlList, `n
	{
		vCtlClassNN := A_LoopField
		ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
		WinGetClass, vCtlClass, % "ahk_id " hCtl
		if RegExMatch(vCtlClass, vClassRegEx)
			vCount++
		if (vCount = vNum)
			return vCtlClassNN
	}
}
[EDIT:] Changed function slightly, hWnd should always go first.
From: JEE_CtlGetClassNNRegEx(vClassRegEx, vNum, hWnd)
To: JEE_WinGetCtlClassNNRegEx(hWnd, vClassRegEx, vNum:=1)

[EDIT:] It may be that the pattern in the window class matches the pattern in each control ClassNN.
[SOLVED] Using a fixed subpattern for changing subpatterns in CONTROL's ClassNN ? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 03#p218503
Last edited by jeeswg on 17 May 2018, 13:05, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Being more generic when using WinActivate and ControlSetText

04 Dec 2017, 18:41

jeeswg, is there anything that you haven't already written a function for?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Being more generic when using WinActivate and ControlSetText

05 Dec 2017, 02:06

No, no there isn't.

You know, SKAN was always good for functions. Still is. E.g. here:
Spoiler
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ReyAHK and 263 guests