UIA Interface help. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

UIA Interface help.

Post by LAPIII » 02 Feb 2023, 12:43

I'm trying to automate AntimicroX and I am stuck:

Code: Select all

#SingleInstance Force
#NoTrayIcon
;#IfWinActive ahk_exe antimicrox.exe
#Include <UIA_Interface>
UIA := UIA_Interface()

runApp(appName) {
 For app in ComObjCreate("Shell.Application").NameSpace("shell:AppsFolder").Items
  Run, % app.Name = appName ? "explorer shell:appsFolder\" app.Path : ""
}

if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"

1:: ; Mouse
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Mouse", "TabItem").Click()
return

2:: ; Advanced
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Advanced", "Button").Click()
return

3:: ; close window
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Close", "Button").Click()
return

$^w::winclose ; exit app


Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 03 Feb 2023, 00:10

@LAPIII,
1) Please provide a DumpAll of the window element, or a screenshot of UIAViewer after building the tree.
2) Try in UIAViewer whether Invoke() works on the Button elements you want to click
3) If you are running AntiMicroX as admin, make sure you are running the script as admin as well

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 03 Feb 2023, 15:44

I couldn't use the UIAViewer with AntimicroX until I ran the script as admin. I have no idea how to use it to Invoke().
Attachments
UIAViewer Mouse.jpg
UIAViewer Mouse.jpg (82.38 KiB) Viewed 1123 times
UIAViewer Close.jpg
UIAViewer Close.jpg (84.19 KiB) Viewed 1123 times
UIAViewer Advanced.jpg
UIAViewer Advanced.jpg (93.59 KiB) Viewed 1123 times

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 03 Feb 2023, 16:44

@LAPIII, if you had to run UIAViewer as admin, then you need to run your script as admin as well.
I see that AntiMicroX is a Qt5 app, which might not be easily automatable with UIAutomation (that is, the regular .Click() might not work). Try using .Click("left") or .ControlClick() instead.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 03 Feb 2023, 19:12

I figured out how to Invoke() and it worked on each element. Neither of their click methods are working for me:

Code: Select all

#SingleInstance Force
;#NoTrayIcon
#Include <UIA_Interface>
UIA := UIA_Interface()

if not A_IsAdmin
Run *RunAs "%A_ScriptFullPath%" /restart

runApp(appName) {
 For app in ComObjCreate("Shell.Application").NameSpace("shell:AppsFolder").Items
  Run, % app.Name = appName ? "explorer shell:appsFolder\" app.Path : ""
}

If WinExist("ahk_exe antimicroX.exe")
WinActivate
Else runApp("AntimicroX")

1:: ; Mouse
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Mouse", "TabItem").ControlClick()
return

2:: ; Advanced
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Advanced", "Button").Click("left")
return

3:: ; close window
WinGet, hWnd, ID, Antimicrox
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.FindFirstByNameAndType("Close", "Button").Click()
return

;$^w::WinClose ;PostMessage, 0x0112, 0xF060,,, "ahk_exe AntimicroX.exe" ; exit app

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 04 Feb 2023, 04:24

@LAPIII, if Invoke() worked, then Click() will work as well but this means the element isn't being found for some reason.

First I recommend checking whether the element is accessible to FindFirst in the first place from the window element that you are getting:

Code: Select all

2:: ; Advanced
WinGet, hWnd, ID, Antimicrox
MsgBox, % hWnd
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.Highlight()
MsgBox, % Clipboard := winEl.DumpAll()
return
If the first MsgBox displays a 0, then the window isn't being found (are you using the correct window name?).
If in the second MsgBox the "Advanced" Button element isn't listed in the DumpAll() output, then using FindFirst will not work either. This usually means you've got the incorrect window - one process might have multiple windows open, and the windows might all contain the word "Antimicrox" in it, but you need to get the window where the button is contained in. Check with WindowSpy if the hWnd you got with WinGet matches the hWnd displayed in WindowSpy when you are hovering the "Advanced" button.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 04 Feb 2023, 14:38

There are multiple windows. I keep get blank message boxes. I want to try to use a button in the first window with the name = Controller Mapper and Control Type = Button. I think I'm missing something in my script, where do I window name:

Code: Select all

#SingleInstance Force
;#NoTrayIcon
#Include <UIA_Interface>
UIA := UIA_Interface()

if not A_IsAdmin
Run *RunAs "%A_ScriptFullPath%" /restart

2:: ; Advanced
WinGet, hWnd, ID, Antimicrox
MsgBox, % hWnd
winEl := UIA.ElementFromHandle("ahk_id" hWnd)
winEl.Highlight()
MsgBox, % Clipboard := winEl.DumpAll()
return

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 04 Feb 2023, 15:36

@LAPIII, try this:

Code: Select all

#include <UIA_Interface>
SetBatchLines, -1

if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"

UIA := UIA_Interface()

#IfWinActive ahk_exe antimicrox.exe
1:: ; Mouse
winEl := UIA.ElementFromHandle("A")
winEl.FindFirstByNameAndType("Mouse", "TabItem").Click()
return

2:: ; Advanced
winEl := UIA.ElementFromHandle("A")
winEl.FindFirstByNameAndType("Advanced", "Button").Click()
return

3:: ; close window
winEl := UIA.ElementFromHandle("A")
winEl.FindFirstByNameAndType("Close", "Button").Click()
return

$^w::winclose ; exit app

#If
1) #IfWinActive ahk_exe antimicrox.exe makes it so the hotkeys are active only when any of AntiMicroX windows are active.
2) UIA.ElementFromHandle("A") gets the active window element, so it will try to find and click elements in the active window - this means that if you have the "As last gamepad button has been set ..." window active, the hotkeys will work properly.
3) If you want to look for the elements in a window with a specific name (such as the window we are talking about right now), you need to use the window title in place of "A", remembering to use the correct SetTitleMatchMode as well if necessary. In this case you could use UIA.ElementFromHandle("As last gamepad button") for example.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 04 Feb 2023, 15:42

Thanks very much bro!

EDIT: and it only works when AnimicroX isn't being ran as admin.

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 04 Feb 2023, 15:51

@LAPIII, AHK can only automate programs ran as admin if AHK itself has been ran as admin, or perhaps with UI Access enabled. Make sure you are running the script as admin and it should work. You can try modifying the lines

Code: Select all

if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"
to

Code: Select all

if not A_IsAdmin
	Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
which might also fix the problem.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 12 Feb 2023, 09:56

I converted this to use with version 2, can you check it because it's not working:

Code: Select all

#SingleInstance Force
#Requires AutoHotkey v2.0.2
;#NoTrayIcon
#Include <UIA-v2>

if not A_IsAdmin
	Run("*RunAs `"" A_AhkPath "`" /restart `"" A_ScriptFullPath "`"")

#HotIf WinActive("ahk_exe antimicrox.exe", )
1:: ; Mouse

{
winEl := UIA.ElementFromHandle("A")
winEl.FindElement({Type:"Mouse", Type:"TabItem"}).Click()
}

2:: ; Advanced

{
winEl := UIA.ElementFromHandle("A")
winEl.FindElement({Type:"Advanced", Type:"Button"}).Click()
}

$^w:: ; close window
{
winEl := UIA.ElementFromHandle("A")
winEl.FindElement({Type:"Close", Type:"Button"}).Click()
}

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 12 Feb 2023, 12:22

@LAPIII, instead of winEl.FindElement({Type:"Advanced", Type:"Button"}).Click() use winEl.FindElement({Name:"Advanced", Type:"Button"}).Click(). In v1 you used FindFirstByNameAndType, not FindFirstByTypeAndType :)

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.

Post by LAPIII » 12 Feb 2023, 13:24

Sorry about that careless mistake. Now I get for line number 14: Error: Invalid property name in object literal..

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: UIA Interface help.

Post by Descolada » 12 Feb 2023, 14:23

@LAPIII, well, the error says that you have written your object incorrectly... I can't really say what you did wrong if you don't provide your corrected code, but line 14 should look like this: winEl.FindElement({Name:"Mouse", Type:"TabItem"}).Click()

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: UIA Interface help.  Topic is solved

Post by LAPIII » 12 Feb 2023, 15:00

Yes, I did have it incorrect. I had double colons, Thank you.

Post Reply

Return to “Ask for Help (v1)”