Basic UIA tasks

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

Re: Basic UIA tasks

Post by Descolada » 21 Apr 2023, 14:10

@LAPIII, xccEl.WaitElement({Type:"TabItem", Name:"On Xbox Live"}).Click() ;isn't working when nothing in it I have no idea what you mean by "isn't working", because I don't have Xbox Live to test it. If the element might not exist at all, then you shouldn't use WaitElement without a timeout value, otherwise it'll wait indefinitely.

Your "d" hotkey is again mixing v1 and v2 code. There is no FindFirstBy in UIA v2 for starters...

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

Re: Basic UIA tasks

Post by LAPIII » 02 May 2023, 15:57

Can I possibly make this activate only on in the Captures window. At the top of the window is the word Captures (Type: "Text ", Name:"Captures" ):

Code: Select all

#HotIf WinActive("Xbox Console Companion ahk_exe ApplicationFrameHost.exe")
d::xccEl.WaitElement({Type:"Button", Name:"Delete"}).Click()

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

Re: Basic UIA tasks

Post by LAPIII » 11 May 2023, 11:16

@Descolada I think that the shortcut in the post above can use an If I just don't know how to write it. Could you help me please?

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

Re: Basic UIA tasks

Post by Descolada » 11 May 2023, 12:39

@LAPIII, #HotIf should be used with functions that have a fast execution time (otherwise the key would be too laggy), and I don't think UIA is fast enough for this. You can try it out of course, something like

Code: Select all

#HotIf WinActive("Xbox Console Companion ahk_exe ApplicationFrameHost.exe") && UIA.ElementFromHandle("Xbox Console Companion ahk_exe ApplicationFrameHost.exe").ElementExist({Type:"Text", Name:"Captures"})
d::xccEl.WaitElement({Type:"Button", Name:"Delete"}).Click()
But I think this would make the "d" key slow to respond, so I think I'd use a hotkey with a modifier (which hopefully doesn't already have an action tied to it):

Code: Select all

#HotIf WinActive("Xbox Console Companion ahk_exe ApplicationFrameHost.exe")
+d:: ; Shift+d
{
    if UIA.ElementFromHandle("Xbox Console Companion ahk_exe ApplicationFrameHost.exe").ElementExist({Type:"Text", Name:"Captures"})
        xccEl.WaitElement({Type:"Button", Name:"Delete"}).Click()
}

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

Re: Basic UIA tasks

Post by LAPIII » 11 May 2023, 12:54

I can't get the hotkey to work:

Code: Select all

if not A_IsAdmin
	Run("*RunAs `"" A_ScriptFullPath "`" /restart")
	
If WinExist("ahk_exe Brother iPrintScan.exe"){
WinActivate()
}Else
{
run("C:\Program Files (x86)\Brother\iPrint&Scan\Brother iPrint&Scan.exe")
}
Sleep(3000)
iPSEl := UIA.ElementFromHandle("ahk_exe Brother iPrint&Scan.exe")

#HotIf WinActive("ahk_exe Brother iPrint&Scan.exe")

s::iPSEl.WaitElement({Type:"Button", Name:"Scan"}).Click()

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

Re: Basic UIA tasks

Post by Descolada » 11 May 2023, 13:14

@LAPIII, "I can't get the hotkey to work" is not a helpful statement to get you a solution. You need to do some debugging to understand whether it's the WinActive("ahk_exe Brother iPrint&Scan.exe") statement that isn't correct (perhaps the exe name is incorrect), or the element isn't being found, or something else entirely. If this is about getting a hotkey to trigger properly, then your problem isn't with UIA and you should create a new thread for that.

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

Re: Basic UIA tasks

Post by LAPIII » 18 May 2023, 13:33

Can you use If and else? Please give me an example. I found the following lines of code in an example and thought that this would be more useful:

Code: Select all

if cUIA.ElementFromPath("Y/YYY/YqYYYYVRRRqRyIJ").Click() ; Not part of this demonstration
	try explorerEl.FindElement({T:0,N:"Minimize the Ribbon"}).Invoke() ; Not part of this demonstration
	
EDIT: is it faster using ElementFromPath() than FindElement()?

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

Re: Basic UIA tasks

Post by Descolada » 18 May 2023, 23:25

@LAPIII, you can use if/else, but only whether an element was found or not, and in that case you would need to use the Exist variants of the methods.
One way is using try-catch:

Code: Select all

try {
    cUIA.ElementFromPath("Y/YYY/YqYYYYVRRRqRyIJ").Click() ; If this throws an error, then the next line won't be executed
    explorerEl.FindElement({T:0,N:"Minimize the Ribbon"}).Invoke()
} catch {
    MsgBox("Something went wrong")
}
Another way is if-else with Exist methods:

Code: Select all

if button := cUIA.ElementFromPathExist("Y/YYY/YqYYYYVRRRqRyIJ")
    button.Click()
else
    MsgBox("The element doesn't exist, thus can't be clicked")
ElementFromPath is generally faster than FindElement, unless you use FindElement with TreeScope Children (which greatly limits its search scope and thus makes the search faster). Also see "Speed improvements" in the Wiki.

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

Re: Basic UIA tasks

Post by LAPIII » 02 Jul 2023, 09:49

I'm trying to make a script that works with Google Sheets, but my hotkey for moving to a cell doesn't work:

Code: Select all

#SingleInstance Force
#Requires Autohotkey v2.0+
#NoTrayIcon
#Include <UIA>
#Include <UIA_Browser>
WinWaitActive "ahk_exe msedge.exe"
cUIA := UIA_Browser()
cUIA.ElementFromHandle("ahk_exe msedge.exe")
#HotIf WinActive("Google Sheets ahk_exe msedge.exe")

q::cUIA.Move(433,125)
Here is the error that I get:

TEST.ahk 02_07_23 10⦂48⦂48⦂828 AM.jpg
TEST.ahk 02_07_23 10⦂48⦂48⦂828 AM.jpg (36.28 KiB) Viewed 744 times

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

Re: Basic UIA tasks

Post by Descolada » 02 Jul 2023, 10:34

@LAPIII, and what exactly should cUIA.Move(433,125) do? "Move" can either belong to TextPattern, used to manipulate selectable text, or to TransformPattern, used to move the window. It will not select a cell in Google sheets, nor can you easily select cells with UIA because Google hasn't done accessibility that well.
I'd recommend using some browser library to automate it (Chrome.ahk, Edge.ahk, Rufaydium etc), or you can record and edit macros in Google Sheets (Extensions -> Macros) where you can assign a hotkey to it (it allows only Ctrl+Alt+Shift+character hotkeys, which I guess you could also trigger from AHK with another hotkey).

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

Re: Basic UIA tasks

Post by LAPIII » 04 Jul 2023, 08:43

Is it possible to use the property Name, But only with the first word or part of it?

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

Re: Basic UIA tasks

Post by Descolada » 04 Jul 2023, 10:25

Yes: {Name:"somename", MatchMode:"StartsWith"} or {Name:"somename", MatchMode:"Substring"}

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

Re: Basic UIA tasks

Post by LAPIII » 04 Jul 2023, 16:16

Can I write to pick between two path, i.e. if one elements path doesn't exist then it chooses the second?

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

Re: Basic UIA tasks

Post by Descolada » 04 Jul 2023, 23:35

You can create "or" conditions with the array notation, so [{Name:"some"}, {Name:"other"}] will match an element with name "some" or "other".
If you want to do the same with paths you need to use ElementFromPathExist and use AHK-s "or" operator: found := El.ElementFromPathExist(path1) || El.ElementFromPathExist(path2)

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

Re: Basic UIA tasks

Post by LAPIII » 05 Jul 2023, 04:55

I used: $l::cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRRqRt0") || cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRR/RzrrK0").Click()
And got: Error: This value of type "Integer" has no method named "Click".

EDIT: I started my script with the following:

Code: Select all

#Requires Autohotkey v2.0+
#Include <UIA>
#Include <UIA_Browser>
WinWaitActive "ahk_exe msedge.exe"
cUIA := UIA_Browser()
cUIA.ElementFromHandle("ahk_exe msedge.exe")

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

Re: Basic UIA tasks

Post by Descolada » 06 Jul 2023, 13:00

You code cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRRqRt0") || cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRR/RzrrK0").Click() evaluates to either cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRRqRt0") or if no such element is found, cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRR/RzrrK0").Click(). You should use (cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRRqRt0") || cUIA.ElementFromPathExist("Y/YYY/YqYYYYYYVRRR/RzrrK0")).Click() instead.

Also, I won't answer any more of your questions in this thread. You are constantly making mistakes with basic AHK syntax and most of your problems have nothing to do with UIA. You should go back to the basics and learn AHK syntax with easier code and simpler libraries before tackling Acc or UIA any further.

Post Reply

Return to “Ask for Help (v2)”