samfisherirl / UIAViewer.ahk-for-UIAutomation.ahk

UIAViewer.ahk for UIAutomation.ahk, with some modifications
10 stars 1 forks source link

"UI" function improvement proposal #5

Open Descolada opened 1 year ago

Descolada commented 1 year ago

I think the UI function could be improved to also include the WinExist call, add an optional timeOut argument, and make the name more descriptive. Something like this:

el := WinActivateGetElement(" github.com/Descolada/UIAutomation ahk_exe AutoHotkey.exe")

WinActivateGetElement(winTitle:="", timeOut:=""){
    wId := WinExist(winTitle)
    WinActivate, ahk_id %wId%
    WinWaitActive, ahk_id %wId%,, % timeOut
    return UIA_Interface().ElementFromHandle(wId)
}

Optionally it could be modified to throw an error if the window isn't found or the element is empty. This function can also be reused: if the script detects that function in the macro text, then it doesn't have to include it any more. Also it doesn't have to depend on a global UIA variable, since it's used only once inside the function, but I guess it's a matter of choice/style whether to let this method be used in any script or not (e.g. the UI function would work in a script that uses cUIA instead of UIA).