How to account for changing ClassNN info

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

How to account for changing ClassNN info

17 Nov 2017, 12:59

Hello,

The program I'm using (SDL Trados Studio) doesn't always use the same ClassNN for controls, so for example, a script that I write on my desktop computer won't work on my laptop, and a colleague has mentioned that control names may even change on the same computer after closing and restarting the program. Is there something that can be done to compensate for this?

Here's an example of a script that I'd like to make more reliable across computers:

Code: Select all

~LButton::
IfWinExist, Batch Processing
{
  WinWaitClose, Batch Processing
  Sleep, 1000
  Control, Choose, 2, WindowsForms10.COMBOBOX.app.0.29531c8_r9_ad14,  ahk_class WindowsForms10.Window.8.app.0.29531c8_r9_ad1    ; Selects option 2
 }
Return
Thank you in advance.
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 13:14

Thank you, I had seen that but I haven't understood how it might apply in this case. If I use SetTitleMatchMode, 2, will it also apply to the COMBOBOX control, or only to the Window title?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

17 Nov 2017, 14:37

- Can you give an example of 2 versions of the same control? 2 different ClassNNs.
- Something like this might work to get the 14th control that starts with 'WindowsForms10.COMBOBOX'.

Code: Select all

WinGet, hWnd, ID, A
vCount := 0
WinGet, vCtlList, ControlList, % "ahk_id " hWnd
Loop, Parse, vCtlList, `n
{
	vCtlClassNN := A_LoopField
	if (SubStr(vCtlClassNN, 1, 23) = "WindowsForms10.COMBOBOX")
		vCount++
	if (vCount = 14)
		break
}
ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
MsgBox, % hCtl "`r`n" vCtlClassNN
return
- AFAIK SetTitleMatchMode only applies to the text of window titles, and not window/control classes or control ClassNNs.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 14:44

Thank you! Two versions of the same control are:

On my desktop: WindowsForms10.COMBOBOX.app.0.29531c8_r9_ad14
On my laptop: WindowsForms10.COMBOBOX.app.0.201d787_r9_ad11

I'm afraid you code is way over my head, being just a newbie with AHK. How could I incorporate that into my script?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

17 Nov 2017, 14:52

The ClassNN appears to have a consistent pattern:
WindowsForms10.COMBOBOX.app.0.#######_r9_ad##
But the NN varies, it is 14 or 11 (or possibly 4 or 1, I'm not certain if the class ends in 'ad1' or 'ad').
So this appears to be a problem, the number at the end is not consistent.

[EDIT:] The class appears to be as follows:
WindowsForms10.COMBOBOX.app.0.#######_r9_ad1
And so the ClassNN would be:
WindowsForms10.COMBOBOX.app.0.#######_r9_ad1# for the first 9 ComboBoxes
WindowsForms10.COMBOBOX.app.0.#######_r9_ad1## for the next 90

To explain what my script does, it gets the window handle for the active window, it retrieves all the ClassNNs for the window and stores them in a string. The string is LF-separated (linefeed characters between each ClassNN). It checks the start of each ClassNN that matches the string we're looking for, and counts them, and when it has found the 14th one, it breaks out of the loop. Then it receives the handle to the control (which may or may not be useful) and shows the results in a MsgBox.

- We have a problem if the NN varies, not just the class. You could try things like ControlGetPos. Or possibly if the controls are in a hierarchy, you can find a particular easily identifiable control, and retrieve its children. And check those children for the matching control.
- Here's an example of finding the children of a window, and then the children of one of the children.

Code: Select all

q:: ;internet explorer - list controls (for main window and for Find Bar)
;note: show the Find Bar by pressing Ctrl+F, before running the script
;note: the same controls may have different ClassNNs in each loop, since the NNs are relative to the main window/control
WinGet, hWnd, ID, A

;list Internet Explorer controls
vOutput := ""
WinGet, vCtlList, ControlList, % "ahk_id " hWnd
Loop, Parse, vCtlList, `n
{
	vCtlClassNN := A_LoopField
	ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
	vOutput .= hCtl "`t" vCtlClassNN "`r`n"
}
MsgBox, % vOutput

;list Internet Explorer Find Bar controls
ControlGet, hCtl, Hwnd,, FindBarClass1, % "ahk_id " hWnd
vOutput := ""
WinGet, vCtlList, ControlList, % "ahk_id " hCtl
Loop, Parse, vCtlList, `n
{
	vCtlClassNN := A_LoopField
	ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
	vOutput .= hCtl "`t" vCtlClassNN "`r`n"
}
MsgBox, % vOutput

Clipboard := vOutput
MsgBox, % "done"
return
Last edited by jeeswg on 17 Nov 2017, 15:12, edited 2 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
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 15:03

Thank you so much, in case it helps clarify things further, here's some more info from Window Spy:

Window Title, Class and Process:
SDL Trados Studio
ahk_class WindowsForms10.Window.8.app.0.29531c8_r9_ad1
ahk_exe SDLTradosStudio.exe


Control Under Mouse Position:
ClassNN: WindowsForms10.COMBOBOX.app.0.29531c8_r9_ad14
Text: English (United States)
Color: 2E3032 (Red=2E Green=30 Blue=32)
x: 23 y: 191 w: 323 h: 24
Client: x: 23 y: 191 w: 323 h: 24


I guess that means the class is "ad1", right?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

17 Nov 2017, 15:07

Yes and no. The class of the window ends in 'ad1', this is good evidence. So I'll assume that 'ad1' is also the end of the control class. To double-check I could do this:

Code: Select all

ControlGetFocus, vCtlClassNN, A
ControlGet, hCtl, Hwnd,, % vCtlClassNN, A
WinGetClass, vCtlClass, % "ahk_id " hCtl
MsgBox, % vCtlClass
return
Also, if we count how many ComboBoxes there are, 14 is quite a lot. Does it look like there are 14 or 4?

Hopefully if the number stays consistent e.g. the 1st or 4th ComboBox, then my script will work for you (if you specify the desired number). Otherwise we need a way to find the right ComboBox.

If you can retrieve text from the ComboBoxes, this might help to identify the right one:

Code: Select all

q:: ;notepad (windows 7) - save as - get ComboBox text
ControlGetText, vText, ComboBox2, A
ControlGet, vText2, List,, ComboBox2, A
MsgBox, % vText
MsgBox, % vText2
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 15:27

14 definitely sounds like a lot. Here's a screenshot of the program, with an arrow pointing to the Dropdown list I need to access.
2017-11-17_131730.png
2017-11-17_131730.png (62.68 KiB) Viewed 3340 times
Getting the text in the ComboBox:
I tried the script, but pressing q only gives me an empty message box. Does that mean it's not possible to get the text?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

17 Nov 2017, 15:32

My script above was for use with Notepad. Use this script, to see if you can get the control text:

The ComboBox should be the active control when you run it.

Code: Select all

q:: ;active control - get class (and get text if control compatible)
ControlGetFocus, vCtlClassNN, A
ControlGet, hCtl, Hwnd,, % vCtlClassNN, A
WinGetClass, vCtlClass, % "ahk_id " hCtl
ControlGetText, vText, % vCtlClassNN, A
ControlGet, vText2, List,, % vCtlClassNN, A
MsgBox, % vCtlClass
MsgBox, % vText
MsgBox, % vText2
return
If it doesn't retrieve any text, then you might want to try Acc to get the text:
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 15:54

Thank you for your patience in guiding me through this. I ran the script and get the following:

WindowsForms10.COMBOBOX.app.0.29531c8_r9_ad1
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 15:56

Oh sorry, some more text in other message boxes:

English (United States)

Spanish (Mexico)
English (United States)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

17 Nov 2017, 16:11

Hmm, well in that case, hopefully that information can be used to confirm that you've got the correct ComboBox as you loop through them. It looks like everything is doable if you combine the information I've put together on this page.

Also, the first MsgBox confirms that the ComboBox class name ends in 'ad1'. And so in your examples, they were the 1st / 4th ComboBoxes.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
ndiaz
Posts: 73
Joined: 10 Jan 2017, 17:05
Contact:

Re: How to account for changing ClassNN info

17 Nov 2017, 16:41

Yes, thank you very much, this has been very informative.

Thank you for taking the time to provide all this info.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to account for changing ClassNN info

04 Dec 2017, 15:14

I've written a function here, to handle this problem more generally:

Being more generic when using WinActivate and ControlSetText - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 24#p186624
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: No registered users and 148 guests