TreeItem found with Acc but not UIA Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gobbedy
Posts: 6
Joined: 01 Jan 2023, 00:59
Contact:

TreeItem found with Acc but not UIA

01 Jan 2023, 10:32

Hello,

This is my first time posting. I want to first say thank you to the community in particular @jeeswg because I've referred to your helpful posts many times.

I've written some shortcuts using Acc, and they work great. I'd now like to switch to UIA (to futureproof my code, and also to try to make it work in the background; ie on inactive windows).

I'm stuck almost at square 1. I'm trying to access a TreeItem that Acc has no problem finding using a path, but UIA cannot seem to find using its find functions.

The "Profiles" TreeItem is visible in the nppFTP plugin of Notepad++, and is shown in the screenshot below, next to the UIAViewer which shows the Acc path:
AccProfiles.PNG
AccProfiles.PNG (97.71 KiB) Viewed 543 times

The Acc code below works as intended:

Code: Select all

#Include Acc.ahk
^q::
    myHwnd:= WinExist("A")
    ProfilesPath:= "4.3.4.2.4.1.4.2.4.1"
    Profiles:=Acc_Get("Object", ProfilesPath, 0, "ahk_id " myHwnd) ; first profile
    name:= Profiles.accName
    MsgBox, Profiles name is %name%
Return
The msgbox on the last line prints "Profiles name is Profiles", as expected.

However, the UIA code below cannot find the Profiles TreeItem:

Code: Select all

#include UIA_Interface.ahk
^q::
    myHwnd:= WinExist("A")
    UIA := UIA_Interface() ; Initialize UIA interface
    nppEl := UIA.ElementFromHandle(myHwnd) ; Get the element for the Notepad++ window
    ProfilesUIA := nppEl.FindFirstByName("Profiles")
    name:= ProfilesUIA.name
    MsgBox, Profiles name is %name%
Return
The msgbox on the last line prints "Profiles name is ", ie "name" is an empty string. I tried different find commands, but with no luck.

Is my UIA code all wrong?

I'd be grateful for any help!
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: TreeItem found with Acc but not UIA  Topic is solved

01 Jan 2023, 13:34

@gobbedy, your UIA code is 100% correct, it's just apparently Microsoft or Notepad++ developers have messed up with the implementation of UIAutomation (unfortunately this isn't the first time... :crazy: ). UIAViewer returns the element with ElementFromPoint and shows the element correctly, even shows that it's inside the Notepad++ window (again, correct). But when you stop the capturing and build the whole UIA tree (which uses ElementFromHandle and then displays all child elements), the element isn't shown! This means that for some reason the tree under the element from ElementFromHandle("Notepad++ main window") doesn't contain your treeview element. This is also demonstrated with nppEl.DumpAll() which won't contain an element named "Profiles", and if DumpAll can't display it then FindFirst can't find it.

Fortunately in this case there is a fix: start from the SysTreeView321 control (which I got using Window Spy), in which case the treeview elements are accessible:

Code: Select all

^q::
    myHwnd:= WinExist("A")
    ControlGet, cHwnd, Hwnd,, SysTreeView321, ahk_id %myHwnd%
    UIA := UIA_Interface() ; Initialize UIA interface
    twEl := UIA.ElementFromHandle(cHwnd) ; Get the element for the Notepad++ window
    ProfilesUIA := twEl.FindFirstByName("Profiles")
    name:= ProfilesUIA.name
    MsgBox, Profiles name is %name%
Return
If you need to also interact with other Notepad++ elements, then you can get an element for that separately (nppEl := UIA.ElementFromHandle(myHwnd)).

Btw, for TreeViews you might get better results with the Remote TreeView class.
gobbedy
Posts: 6
Joined: 01 Jan 2023, 00:59
Contact:

Re: TreeItem found with Acc but not UIA

01 Jan 2023, 21:00

@Descolada, just wow. Your answer is a literal goldmine. You fixed my code and gave a crash course at the same time.

But then, as if that wasn't enough, you pointed me to the remote TreeView class which will save me a gazillion hours of time. Seriously, the TreeView class looks to be the godsend I didn't know I needed.

Thank you so much. Really.
gobbedy
Posts: 6
Joined: 01 Jan 2023, 00:59
Contact:

Re: TreeItem found with Acc but not UIA

01 Jan 2023, 23:56

@Descolada, please let me know if I should create a new post for this as this is more of an offshoot.

Following your advice I tried the Remote TreeView class. It seems to properly traverse the same nppFTP plugin tree as per my original post. But it prints out empty strings for the names:

Code: Select all

#Include %A_ScriptDir%\Const_TreeView.ahk
#Include %A_ScriptDir%\Const_Process.ahk
#Include %A_ScriptDir%\Const_Memory.ahk
#Include %A_ScriptDir%\RemoteTreeViewClass.ahk

^q::
    myHwnd:= WinExist("A")
	ControlGet TVId, Hwnd, , SysTreeView321, ahk_id %myHwnd%
	MyTV := new RemoteTreeView(TVId)
	hItem = 0  ; Causes the loop's first iteration to start the search at the top of the tree.
	Loop
	{
		hItem := MyTV.GetNext(hItem, "Full")
		if not hItem  ; No more items in tree.
			break
		ItemText := MyTV.GetText(hItem)
		MsgBox The next Item is %hItem%, whose text is "%ItemText%".
	}
    Return
Return
The first MsgBox returns 'The next Item is 1839858020080, whose text is "".'
And the second MsgBox returns 'The next Item is 1839858019968, whose text is "".'

I would have expected the 'ItemText' for the first msgBox to be Profiles, and the 'ItemText' for the second msgBox to be gperraul.

Am I screwing something up?

Again, happy to create a new post since my original post was already answered by you (thanks again).
gobbedy
Posts: 6
Joined: 01 Jan 2023, 00:59
Contact:

Re: TreeItem found with Acc but not UIA

02 Jan 2023, 07:39

FYI, I got GetText working using user 611's suggested fix. See my reply to viewtopic.php?p=499538#p499538.

I'll come back here and delete my previous comment in a bit, just posting this here in case @Descolada (or someone else) had started looking into it.
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: TreeItem found with Acc but not UIA

02 Jan 2023, 08:24

@gobbedy, I hadn't had a chance to try out your code, but I probably would've recommended trying the proposed fix that you linked to, so I'm glad you already found it and got it working :) Cheers!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Hugh Jars, peter_ahk, Pianist and 337 guests