Basic UIA tasks

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: Basic UIA tasks

Post by Descolada » 26 Mar 2023, 10:27

@LAPIII, for that particular example the element is activated by a mouse hover, which usually can't be simulated by UIA (as far as I know at least), so you would have to revert to AHK functions:

Code: Select all

#Requires Autohotkey v2.0+
#Include <UIA_Interface-v2>
#Include <UIA_Browser-v2>
RunWait "https://www.amazon.com/"
WinWaitActive "Amazon ahk_exe chrome.exe"
cUIA := UIA_Browser("Amazon ahk_exe chrome.exe")

helloPos := cUIA.WaitElement({AutomationId:"nav-link-accountList"}).GetPos()
MouseMove(helloPos.x+helloPos.w//2, helloPos.y+helloPos.h//2)
cUIA.WaitElement({Name:"Sign in", Type:"Link"}).Highlight()
In a browser you could also execute Javascript if you wanted to:

Code: Select all

cUIA.WaitElement({AutomationId:"nav-link-accountList"})
cUIA.JSExecute("document.querySelector('#nav-link-accountList').dispatchEvent(new Event('mouseover'));")
cUIA.WaitElement({Name:"Sign in", Type:"Link"}).Highlight()
Though in this case you could just click the element to get to the login page...
Last edited by Descolada on 26 Mar 2023, 10:34, edited 1 time in total.

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

Re: Basic UIA tasks

Post by LAPIII » 26 Mar 2023, 10:33

Thank you, and for hover elements I use GetFocus().

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

Re: Basic UIA tasks

Post by LAPIII » 26 Mar 2023, 19:14

I just got this error:
TEST.ahk 26_03_23 08⦂14⦂26⦂699 PM.jpg
TEST.ahk 26_03_23 08⦂14⦂26⦂699 PM.jpg (41.85 KiB) Viewed 1278 times
From this script:

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+
#Include <UIA_Interface-v2>

s::{
If !WinExist("ahk_exe ApplicationFrameHost.exe")
runApp("Xbox Console Companion")

runApp(appName) {
 For app in ComObject("Shell.Application").NameSpace("shell:AppsFolder").Items
  Run(app.Name = appName ? "explorer shell:appsFolder\" app.Path : "")
}
Sleep(4500)
winEl := UIA.ElementFromHandle("Xbox Console Companion")
UIA.WaitElement("Captures", "ListItem").Click()
}
Last edited by LAPIII on 28 Mar 2023, 10:09, edited 3 times in total.

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

Re: Basic UIA tasks

Post by Descolada » 26 Mar 2023, 22:52

@LAPIII, recheck the argument list for WaitElement.

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

Re: Basic UIA tasks

Post by LAPIII » 28 Mar 2023, 08:46

I change line 15 to:

Code: Select all

winEl.FindElement(Name:"Captures", Type:"ListItem").Click()
Now I get Error: Unexpected ":"

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Basic UIA tasks

Post by swagfag » 28 Mar 2023, 09:16

the surrounding braces are missing(check ur own earlier posts, u used to have them there still)

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

Re: Basic UIA tasks

Post by LAPIII » 28 Mar 2023, 10:51

Thanks @swagfag. Now I get for line 14 Error: No matching window found. In v1 it got detected, but for some reason in v2 it's not. I tried adding SetTitleMatchMode 3 and changing the line to winEl := UIA.ElementFromHandle("ahk_exe ApplicationFrameHost.exe").

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Basic UIA tasks

Post by swagfag » 28 Mar 2023, 11:11

u probably need A_DetectHiddenWindows := true

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

Re: Basic UIA tasks

Post by LAPIII » 28 Mar 2023, 13:35

It works now, I just change line 14 to:

Code: Select all

hWnd := WinGetID("Xbox Console Companion")
winEl := UIA.ElementFromHandle("ahk_id" hWnd)

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

Re: Basic UIA tasks

Post by LAPIII » 31 Mar 2023, 18:01

For line 16 I get an error that says that variable appears to never be assigned a value. I've tried cUIA := UIA_Browser(ahk_exe msedge.exe), but still get the error message:

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+
#Include <UIA_Interface-v2>
#Include <UIA_Browser-v2>

RunWait "https://www.upperdeckepack.com/login"
Sleep(1600)
Click 535, 466
Send "[email protected]"
Send "{Tab}"
Send "pTWnkg9H"
Send "{Tab 3}"
Send "{Enter}"
Sleep(3500)
MouseMove 812, 180
cUIA := UIA_Browser(Upper Deck ahk_exe msedge.exe)
Sleep(33000)
cUIA.WaitElement({Name:"COLLECT", Type:"Link"}).Click()

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Basic UIA tasks

Post by boiler » 31 Mar 2023, 20:03

LAPIII wrote: I've tried cUIA := UIA_Browser(ahk_exe msedge.exe)

Code: Select all

cUIA := UIA_Browser(Upper Deck ahk_exe msedge.exe)
I think it would help you learn what’s wrong with those lines if you go back read the section on expressions — either for v1 or v2 (doesn’t matter because it’s the same problem) — and see if you can find what’s wrong with them. I think you’re not learning it because people post the answer and you just plug it in and move on.

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

Re: Basic UIA tasks

Post by LAPIII » 14 Apr 2023, 10:42

LAPIII wrote:
13 Mar 2023, 13:16

Code: Select all

^F1::try ToolTip UIA.ElementFromPoint().ShowContextMenu()
Can you tell me why the code above will render this:
c9cZtMI4LS.jpg
c9cZtMI4LS.jpg (1.03 KiB) Viewed 953 times

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

Re: Basic UIA tasks

Post by Descolada » 14 Apr 2023, 11:16

@LAPIII, the ToolTip displays the result value for the DllCall that calls ShowContextMenu() from IUIAutomation, and the result of that call was 0 which means success (S_OK). Unfortunately that doesn't necessarily mean that a context menu was actually displayed/triggered. For example it returns 0 in most Chromium apps (eg Chrome), but won't display anything. Try it in Edge or File Explorer, there it works fine.

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

Re: Basic UIA tasks

Post by LAPIII » 14 Apr 2023, 11:27

I use Windows 11 which is why I wonder how mine works fine in Edge and File Explorer:
(1)_Basic_UIA_tasks_-_Page_2_-_AutoHotkey_Communit 14_04_23 12⦂24⦂56⦂434 PM.gif
(1)_Basic_UIA_tasks_-_Page_2_-_AutoHotkey_Communit 14_04_23 12⦂24⦂56⦂434 PM.gif (294.77 KiB) Viewed 946 times
Edge 14_04_23 12⦂23⦂12⦂907 PM.gif
Edge 14_04_23 12⦂23⦂12⦂907 PM.gif (36.33 KiB) Viewed 946 times
Last edited by LAPIII on 13 Mar 2024, 21:28, edited 1 time in total.

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

Re: Basic UIA tasks

Post by Descolada » 14 Apr 2023, 11:44

@LAPIII, I'm not sure what you mean? Both images show the context menu being properly shown (that is, the same thing that opens when you right-click at that same spot), so it's working exactly as expected.

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

Re: Basic UIA tasks

Post by LAPIII » 14 Apr 2023, 12:13

Oh, I was under the impression that you can get rid of the zero. Thanks

EDIT: Perhaps in an update you can change this tooltip from appearing.

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

Re: Basic UIA tasks

Post by Descolada » 14 Apr 2023, 12:42

@LAPIII, the tooltip is unrelated to UIA library. Study the difference between these two lines of code:
1) ^F1::try ToolTip UIA.ElementFromPoint().ShowContextMenu()
2) ^F1::try UIA.ElementFromPoint().ShowContextMenu()

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

Re: Basic UIA tasks

Post by LAPIII » 18 Apr 2023, 09:12

Can you tell me why I get an error on line 9:

Code: Select all

;~ ---------------If logged in
RunWait "https://www.upperdeckepack.com/Store/FreePack"
WinWaitActive "ahk_exe msedge.exe"
cUIA := UIA_Browser("ahk_exe msedge.exe")
cUIA.WaitElement({Type: "Link", Name: "Select your item..."}).Click()
cUIA.WaitElement({Type: "Link", Name: "2022-23 UD Series 1 Hockey - Daily Free Pack"}).Click()
cUIA.FindElement({Type: "Button", Name: "Open"}).Click()
;~ Opening
cUIA.ElementFromHandle("Upper Deck e-Pack - Open My Packs and 1 more page - Personal - Microsoft​ Edge ahk_exe msedge.exe")
cUIA.WaitElementFromPath("Y/YYY/YqYYYYVR0r").Click()
cUIA.FindElement({Type: "Button", Name: "Open"}).Click()
Last edited by LAPIII on 18 Apr 2023, 09:31, edited 1 time in total.

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

Re: Basic UIA tasks

Post by Descolada » 18 Apr 2023, 09:22

@LAPIII, I don't know what exactly your goal is, but the line cUIA.ElementFromHandle("Upper Deck e-Pack - Open My Packs and 1 more page - Personal - Microsoft​ Edge ahk_exe msedge.exe") doesn't do anything - it would get the ElementFromHandle and then discard it. If a new window pops up after clicking the Open button, then you can create another UIA_Browser instance to interact with it, for example

Code: Select all

cUIA2 := UIA_Browser("Upper Deck e-Pack - Open My Packs and 1 more page - Personal - Microsoft​ Edge ahk_exe msedge.exe")
cUIA2.ElementFromPath("Y/YYY/YqYYYYVR0r").Click()
cUIA2.FindElement({Type: "Button", Name: "Open"}).Click()
If it simply opens in a new tab, then you don't need ElementFromHandle. Simply use cUIA.WaitPageLoad(), and then call ElementFromPath (or perhaps WaitElementFromPath instead?).

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

Re: Basic UIA tasks

Post by LAPIII » 21 Apr 2023, 11:43

I am trying to make a hotkey, lines 19 through 25, and I would like it to only work in the TabItem On Xbox Live, line 16, and On this PC:

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+
#NoTrayIcon
#Include <UIA_Interface-v2>

runApp("Xbox Console Companion")

runApp(appName) {
 For app in ComObject("Shell.Application").NameSpace("shell:AppsFolder").Items
  Run(app.Name = appName ? "explorer shell:appsFolder\" app.Path : "")
}
Sleep(2000)
xccEl := UIA.ElementFromHandle("ahk_exe ApplicationFrameHost.exe")
                         ;~ CAPTURES
xccEl.WaitElement({Type:"ListItem", Name:"Captures"}).Click()
xccEl.WaitElement({Type:"TabItem", Name:"On Xbox Live"}).Click() ;isn't working when nothing in it
xccEl.WaitElementFromPath("X/RrIJ/8R7").SetFocus() ; focus first pic or vid

d::
switch A_ThisHotkey
{
case d":
del := xccEl.FindFirstBy("Name=Delete")
}
del.GetCurrentPatternAs("Invoke").Invoke()

Post Reply

Return to “Ask for Help (v2)”