Basic UIA tasks

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Basic UIA tasks

Post by LAPIII » 09 Mar 2023, 17:29

I want to be able to open File Explorer and right-click on a selected file or folder using UIA Interface. This is what I have:

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+

feEl := UIA.ElementFromHandle("ahk_exe explorer.exe")

+1::
{
fEl.ShowContextMenu()
}
Last edited by LAPIII on 09 Mar 2023, 20:34, edited 1 time in total.

neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

Re: Basic UIA tasks

Post by neogna2 » 09 Mar 2023, 18:33

To get going with v2 UIA I recommend reading and trying the examples
https://github.com/Descolada/UIA-v2/tree/main/Examples
There is also excellent documentation at https://github.com/Descolada/UIA-v2/wiki

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

Re: Basic UIA tasks

Post by Descolada » 10 Mar 2023, 00:44

If you want to show the context menu for the selected file/folder, then you need to use it on the corresponding element, not the window element...

Code: Select all

feEl := UIA.ElementFromHandle("ahk_exe explorer.exe")
try items := feEl.FindElement({ClassName:"DUIListView"})
catch
    throw Error("No files/folders item list found")
try items.FindElement({SelectionItemIsSelected:1}).ShowContextMenu()
catch
    throw Error("No selected item found")

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

Re: Basic UIA tasks

Post by LAPIII » 10 Mar 2023, 02:14

When I have a file selected in the File Explorer and then run the script, then I get: Error: No files/folders item list found

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

Re: Basic UIA tasks

Post by Descolada » 10 Mar 2023, 03:00

Substitute items := feEl.FindElement({ClassName:"DUIListView"}) with whatever gets the corresponding element (the container element of the list of files/folders) in your computer. I am using Windows 10, but Explorer is different in Windows 11.

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

Re: Basic UIA tasks

Post by LAPIII » 10 Mar 2023, 11:50

Is still not working for me. My OS is Windows 11 and I changed it to try items := feEl.FindElement({ClassName:"UIListView"}). If I select a file and press an assigned hotkey, I get Error: No files/folders item list found.
Last edited by LAPIII on 10 Mar 2023, 15:37, edited 1 time in total.

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

Re: Basic UIA tasks

Post by Descolada » 10 Mar 2023, 12:54

@LAPIII, you said that the code you had was

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+

feEl := UIA.ElementFromHandle("ahk_exe explorer.exe")

+1::
{
fEl.ShowContextMenu()
}
This, I assumed, was an example of a semi-working script, and that you know that ahk_exe explorer.exe will not get you a File Explorer window... You can easily verify that yourself using WinActivate for example. For me, I need to specify at least the class as well UIA.ElementFromHandle("ahk_class CabinetWClass ahk_exe explorer.exe"), or the window name (which is the name of the open folder).

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

Re: Basic UIA tasks

Post by LAPIII » 10 Mar 2023, 13:58

I'm really sorry that I didn't write that I changed that in the second line of your script.

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

Re: Basic UIA tasks

Post by LAPIII » 13 Mar 2023, 13:16

This works:

Code: Select all

^F1::try ToolTip UIA.ElementFromPoint().ShowContextMenu()

Can you give me examples of how to use, with UIA_Browser, GetAllLinks() and Navigate?
Last edited by LAPIII on 14 Mar 2023, 15:57, edited 1 time in total.

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

Re: Basic UIA tasks

Post by Descolada » 14 Mar 2023, 00:25

@LAPIII, sure, here you go:

Code: Select all

#Include UIA.ahk
#include UIA_Browser.ahk

Run "chrome.exe -incognito" 
WinWaitActive "ahk_exe chrome.exe"
cUIA := UIA_Browser() ; uses Last Found Window returned by WinWaitActive
cUIA.Navigate("google.com")
links := cUIA.GetAllLinks()
txt := ""
for i, link in links
    txt .= "Link " i ": " link.Name " " link.Value "`n"
MsgBox txt
MsgBox "Clicking second link"
links[2].Click()
Also I recommend taking a look at the UIA_Browser examples in the Examples folder.

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

Re: Basic UIA tasks

Post by LAPIII » 14 Mar 2023, 15:58

What's wrong here:

Code: Select all

Numpad0::
{
npEl := UIA.ElementFromHandle("ahk_exe notepad++.exe")
npEl := UIA.FindElement({Type:"MenuBar", Name:"Application"})
A_Clipboard := El.DumpAll()
}

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

Re: Basic UIA tasks

Post by Descolada » 15 Mar 2023, 00:20

@LAPIII, what does the error message say?

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

Re: Basic UIA tasks

Post by LAPIII » 15 Mar 2023, 13:04

It says at line 5 Warning: This variable appears to never be assigned a value.

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

Re: Basic UIA tasks

Post by Descolada » 16 Mar 2023, 09:47

@LAPIII, the error message is rather clear here... Line 5 is A_Clipboard := El.DumpAll(). El hasn't been defined anywhere, whereas npEl has.

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

Re: Basic UIA tasks

Post by LAPIII » 16 Mar 2023, 10:10

Sorry, careless mistake. I changed line 5.
Last edited by LAPIII on 25 Mar 2023, 17:38, edited 1 time in total.

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

Re: Basic UIA tasks

Post by LAPIII » 25 Mar 2023, 16:11

That DumpAll() still got that error message after I fixed line 5 with your correction, but for I got the same error for line 4. I had made a DumpAll() for the app:

Code: Select all

Type: 50032 (Window) Name: "Preferences" LocalizedType: "dialog" ClassName: "#32770"
1: Type: 50008 (List) LocalizedType: "list" AutomationId: "6002" ClassName: "ListBox"
1,1: Type: 50007 (ListItem) Name: "General" LocalizedType: "list item"
1,2: Type: 50007 (ListItem) Name: "Editing" LocalizedType: "list item"
1,3: Type: 50007 (ListItem) Name: "Dark Mode" LocalizedType: "list item"
1,4: Type: 50007 (ListItem) Name: "Margins/Border/Edge" LocalizedType: "list item"
1,5: Type: 50007 (ListItem) Name: "New Document" LocalizedType: "list item"
1,6: Type: 50007 (ListItem) Name: "Default Directory" LocalizedType: "list item"
1,7: Type: 50007 (ListItem) Name: "Recent Files History" LocalizedType: "list item"
1,8: Type: 50007 (ListItem) Name: "File Association" LocalizedType: "list item"
1,9: Type: 50007 (ListItem) Name: "Language" LocalizedType: "list item"
1,10: Type: 50007 (ListItem) Name: "Highlighting" LocalizedType: "list item"
1,11: Type: 50007 (ListItem) Name: "Print" LocalizedType: "list item"
1,12: Type: 50007 (ListItem) Name: "Searching" LocalizedType: "list item"
1,13: Type: 50007 (ListItem) Name: "Backup" LocalizedType: "list item"
1,14: Type: 50007 (ListItem) Name: "Auto-Completion" LocalizedType: "list item"
1,15: Type: 50007 (ListItem) Name: "Multi-Instance & Date" LocalizedType: "list item"
1,16: Type: 50007 (ListItem) Name: "Delimiter" LocalizedType: "list item"
1,17: Type: 50007 (ListItem) Name: "Performance" LocalizedType: "list item"
1,18: Type: 50007 (ListItem) Name: "Cloud & Link" LocalizedType: "list item"
1,19: Type: 50007 (ListItem) Name: "Search Engine" LocalizedType: "list item"
1,20: Type: 50007 (ListItem) Name: "MISC." LocalizedType: "list item"
2: Type: 50000 (Button) Name: "Close" LocalizedType: "button" AutomationId: "6001" ClassName: "Button"
3: Type: 50020 (Text) Name: "Localization:" LocalizedType: "text" AutomationId: "6123" ClassName: "Static"
4: Type: 50003 (ComboBox) Name: "Localization:" Value: "English" LocalizedType: "combo box" AutomationId: "6124" ClassName: "ComboBox"
4,1: Type: 50020 (Text) Name: "Localization:" Value: "English" LocalizedType: "text"
4,2: Type: 50000 (Button) Name: "Open" LocalizedType: "button" AutomationId: "DropDown"
5: Type: 50026 (Group) Name: "Toolbar" LocalizedType: "group" AutomationId: "6101" ClassName: "Button"
6: Type: 50002 (CheckBox) Name: "Hide" LocalizedType: "check box" AutomationId: "6102" ClassName: "Button"
7: Type: 50013 (RadioButton) Name: "Fluent UI: small" LocalizedType: "radio button" AutomationId: "6103" ClassName: "Button"
8: Type: 50013 (RadioButton) Name: "Fluent UI: large" LocalizedType: "radio button" AutomationId: "6104" ClassName: "Button"
9: Type: 50013 (RadioButton) Name: "Filled Fluent UI: small" LocalizedType: "radio button" AutomationId: "6129" ClassName: "Button"
10: Type: 50013 (RadioButton) Name: "Filled Fluent UI: large" LocalizedType: "radio button" AutomationId: "6130" ClassName: "Button"
11: Type: 50013 (RadioButton) Name: "Standard icons: small" LocalizedType: "radio button" AutomationId: "6105" ClassName: "Button"
12: Type: 50026 (Group) Name: "Tab Bar" LocalizedType: "group" AutomationId: "6106" ClassName: "Button"
13: Type: 50002 (CheckBox) Name: "Hide" LocalizedType: "check box" AutomationId: "6118" ClassName: "Button"
14: Type: 50002 (CheckBox) Name: "Multi-line" LocalizedType: "check box" AutomationId: "6119" ClassName: "Button"
15: Type: 50002 (CheckBox) Name: "Vertical" LocalizedType: "check box" AutomationId: "6120" ClassName: "Button"
16: Type: 50002 (CheckBox) Name: "Reduce" LocalizedType: "check box" AutomationId: "6107" ClassName: "Button"
17: Type: 50002 (CheckBox) Name: "Alternate icons" LocalizedType: "check box" AutomationId: "6128" ClassName: "Button"
18: Type: 50002 (CheckBox) Name: "Lock (no drag and drop)" LocalizedType: "check box" AutomationId: "6108" ClassName: "Button"
19: Type: 50002 (CheckBox) Name: "Darken inactive tabs" LocalizedType: "check box" AutomationId: "6109" ClassName: "Button"
20: Type: 50002 (CheckBox) Name: "Draw a coloured bar on active tab" LocalizedType: "check box" AutomationId: "6110" ClassName: "Button"
21: Type: 50002 (CheckBox) Name: "Show close button on each tab" LocalizedType: "check box" AutomationId: "6112" ClassName: "Button"
22: Type: 50002 (CheckBox) Name: "Double click to close document" LocalizedType: "check box" AutomationId: "6113" ClassName: "Button"
23: Type: 50002 (CheckBox) Name: "Exit on close the last tab" LocalizedType: "check box" AutomationId: "6121" ClassName: "Button"
24: Type: 50002 (CheckBox) Name: "Show status bar" LocalizedType: "check box" AutomationId: "6111" ClassName: "Button"
25: Type: 50026 (Group) Name: "Menu" LocalizedType: "group" AutomationId: "6131" ClassName: "Button"
26: Type: 50002 (CheckBox) Name: "Hide menu bar (use Alt or F10 key to toggle)" LocalizedType: "check box" AutomationId: "6122" ClassName: "Button"
27: Type: 50002 (CheckBox) Name: "Hide right shortcuts + ▼ ✕ from the menu bar (Need to restart Notepad++)" LocalizedType: "check box" AutomationId: "6132" ClassName: "Button"
28: Type: 50037 (TitleBar) Value: "Preferences" LocalizedType: "title bar" AutomationId: "TitleBar"
28,1: Type: 50000 (Button) Name: "Close" LocalizedType: "button" AutomationId: "Close"


There are two elements with their type MenuBar and all Properties are the same except for the names. The paths are different and I don't know how to find an element by its path, so could you show me how to use the path to find the element?

Is it possible to start using the UIA interface in the middle of an ordinary script? E.g. with ElementFromPoint()
Last edited by LAPIII on 26 Mar 2023, 00:00, edited 3 times in total.

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

Re: Basic UIA tasks

Post by Descolada » 25 Mar 2023, 23:35

@LAPIII, you can use the array notation to travel by path, or find an unique element next to the one you want and WalkTree to it. Eg. npEl[1,19] or npEl.FindElement({Name:"Cloud & Link"}).WalkTree("+1").

What do you mean by "in the middle of an ordinary script"? You can start using UIA anywhere in the script as long as it's included.

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

Re: Basic UIA tasks

Post by LAPIII » 26 Mar 2023, 00:40

So, say I'm not navigating an app or a webpage and then I find an element that I can't activate. I'm not sure how I can go about this. Besides adding the library or libraries, do I start with ElementFromHandle() or
UIA_Browser(), and then I can continue with WaitElement() or FindElement()?

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

Re: Basic UIA tasks

Post by Descolada » 26 Mar 2023, 02:21

@LAPIII, I still don't understand the question. Can you show a working example code of the problem? Add images to explain it if necessary.

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

Re: Basic UIA tasks

Post by LAPIII » 26 Mar 2023, 04:20

I can't think of an examples right now, but I'll make one up. Let's say that on Amazon I can navigate to where it says sign in, but I couldn't click that actual Sign in button in the drop-down that shoows why I hover over the last element:

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 ADDRESS"
Send "{Tab}"
Send "PASSWORD"
Send "{Tab 3}"
Send "{Enter}"
Sleep(3000)  
MouseMove 812, 180
cUIA := UIA_Browser(Upper Deck ahk_exe msedge.exe)
Sleep(3000) ; Wait for menu to drop-down
cUIA.WaitElement({Name:"COLLECT", Type:"Link"}).Click()
Last edited by LAPIII on 03 May 2023, 13:36, edited 5 times in total.

Post Reply

Return to “Ask for Help (v2)”