### Initiate UIA_Browser Instance Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Starts a new instance of UIA_Browser, hooking onto the browser window and pre-fetching common elements. ```APIDOC ## Initiate UIA_Browser ### Description Initiates a new instance of UIA_Browser, which hooks onto the browser and pre-fetches commonly used elements like the Document element and URL bar. ### Method `cUIA := UIA_Browser(wTitle="")` ### Parameters * **wTitle** (string) - Optional - The title of the browser window to hook onto. ``` -------------------------------- ### UIA Initialization and Setup Source: https://context7.com/descolada/uia-v2/llms.txt Configure UIA settings like IUIAutomation version, screen-reader activation, or custom DLL path before the first call by setting global variables. ```APIDOC ## UIA Initialization and Setup The `UIA` class is initialized automatically on first use. Optionally configure the IUIAutomation version, screen-reader activation, or a custom DLL path before the first call by setting global variables prior to `#include`. ```ahk #Requires AutoHotkey v2 ; Optional: disable screen-reader mode (some apps render differently with it on) global IUIAutomationActivateScreenReader := 0 ; Optional: pin to a specific IUIAutomation version for cross-Windows compatibility global IUIAutomationMaxVersion := 4 ; Optional: use a specific UIAutomationCore.dll global IUIAutomationDllPath := "C:\\Windows\\System32\\UIAutomationCore.dll" #include Lib\\UIA.ahk ; UIA is now ready; check running version MsgBox "IUIAutomation version: " UIA.IUIAutomationVersion ``` ``` -------------------------------- ### UIA Path Example Source: https://github.com/descolada/uia-v2/wiki/01.-Getting-started Use UIA paths for a compact way to travel down the element tree using element types and indexes. This method is short but can break if the UI changes significantly. ```text NotepadElement["ABq"].Name == "Edit" ``` -------------------------------- ### Starting Search from a Specific Element Source: https://github.com/descolada/uia-v2/wiki/08.-Common-pitfalls;-tips-and-tricks Utilize the 'startingElement' argument to begin searches from a known element, rather than the root of the tree. This can improve performance when searching for elements relative to an already found element. ```python WindowEl.FindElement({Name:"Password", startingElement:usernameElement}) ``` -------------------------------- ### Condition Path Example Source: https://github.com/descolada/uia-v2/wiki/01.-Getting-started Condition paths offer a less compact but more robust way to navigate the tree, using shortened conditions like Type, Index, ClassName, AutomationId, and Name properties. ```text NotepadElement["ABq"] == NotepadElement[{T:10}, {T:11, i:2}] ``` -------------------------------- ### GetWinId Source: https://github.com/descolada/uia-v2/wiki/04.-Elements Gets the parent window handle from the element. ```APIDOC ## GetWinId ### Description Retrieves the handle of the parent window associated with the element. ### Method Signature `GetWinId()` ### Returns The parent window handle. ``` -------------------------------- ### Numeric Path Example Source: https://github.com/descolada/uia-v2/wiki/01.-Getting-started Numeric paths rely solely on indexes for tree traversal, making them the least reliable and most prone to breaking with UI changes. ```text NotepadElement[4,2].Name == "Edit" ``` -------------------------------- ### DumpAll Source: https://github.com/descolada/uia-v2/wiki/04.-Elements `DumpAll(delimiter:=" ", maxDepth:=-1)` calls Dump on the whole Subtree starting from this element. ```APIDOC ## DumpAll ### Description Calls `Dump` on the entire subtree starting from this element. ### Method Signature `DumpAll(delimiter:=" ", maxDepth:=-1)` ### Parameters * **delimiter** - Optional. Delimiter to separate the returned properties. Defaults to a space. * **maxDepth** - Optional. Maximal depth of tree levels traversal. Defaults to unlimited (-1). ``` -------------------------------- ### Create and Use Automation Event Handler Source: https://github.com/descolada/uia-v2/wiki/09.-Advanced:-Events Example of creating a common automation event handler and defining its callback function. Ensure the callback function matches the expected signature (senderElement, eventId). ```AutoHotkey handler := UIA.CreateAutomationEventHandler("HandleMyEvent") ; Add event listener with UIA.AddAutomationEventHandler(handler, eventId) HandleMyEvent(senderElement, eventId) { ; Do something } ``` -------------------------------- ### Get the Desktop Root Element using UIA.GetRootElement Source: https://context7.com/descolada/uia-v2/llms.txt Returns the root (desktop) element, which is the parent of all open windows. Useful for enumerating all top-level windows. ```ahk #include Lib\UIA.ahk root := UIA.GetRootElement() ; Enumerate all top-level windows for child in root.Children OutputDebug child.Name " [" child.Type "]" ``` -------------------------------- ### Get Element at Screen Coordinates using UIA.ElementFromPoint Source: https://context7.com/descolada/uia-v2/llms.txt Retrieves the UI element at a specific screen position, or at the current mouse cursor if no coordinates are given. Use Highlight() to briefly visualize the element. ```ahk #include Lib\UIA.ahk CoordMode "Mouse", "Screen" MouseGetPos &mx, &my el := UIA.ElementFromPoint(mx, my) MsgBox "Element under cursor: " el.Dump() ; Get element at fixed screen coordinates el2 := UIA.ElementFromPoint(500, 300) el2.Highlight(-1000) ; highlight for 1 second without blocking ``` -------------------------------- ### GetRootElement Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Retrieves the root element of the UI Automation tree, which represents the entire desktop. This is the starting point for navigating the UI hierarchy. ```APIDOC ## GetRootElement ### Description Gets the root element, which is the Desktop itself. This element contains all other elements on the screen. ### Method `UIA.GetRootElement()` ### Endpoint N/A (Method call) ### Parameters None ### Request Example ``` rootElement := UIA.GetRootElement() ``` ### Response #### Success Response (Element) - **rootElement** (Element) - The root element representing the Desktop. ``` -------------------------------- ### ElementFromPath Source: https://github.com/descolada/uia-v2/wiki/04.-Elements Tries to get an element from a path. If no element is found, an error is thrown. Paths can be numeric, UIA paths, or conditions. ```APIDOC ## ElementFromPath ### Description Retrieves an element by traversing a specified path. Throws an error if the element is not found. ### Method Signature `ElementFromPath(path1[, path2, ...])` ### Parameters - **path1, path2, ...**: Defines the traversal path. Can be: 1. Comma-separated numeric path (e.g., "3,2"). 2. UIA path string (e.g., "bAx3"). 3. One or more conditions (e.g., `{Type:"Button"}, {Type:"List"}`). ``` -------------------------------- ### MultipleView Pattern Methods Source: https://github.com/descolada/uia-v2/wiki/07.-Patterns The MultipleViewPattern allows switching between different views of a control. Use GetSupportedViews to get available views, GetViewName for a view's name, and SetView to switch views. ```csharp GetViewName(view) SetView(view) GetSupportedViews() ``` -------------------------------- ### Get Element for a Window or Control using UIA.ElementFromHandle Source: https://context7.com/descolada/uia-v2/llms.txt Retrieves the UIA element for a window or control handle. Automatically activates Chromium accessibility for Electron/web apps. Use the third parameter to disable Chromium activation for non-browser apps. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" ; Get window element by WinTitle npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") MsgBox "Window name: " npEl.Name ; e.g. "Untitled - Notepad" ; Get element from a specific control handle (useful when sub-trees differ) hCtrl := ControlGetHwnd("RichEditD2DPT1", "ahk_exe notepad.exe") ctrlEl := UIA.ElementFromHandle(hCtrl) MsgBox "Control type: " ctrlEl.Type ; e.g. "Document" ; Disable Chromium activation for non-browser apps (performance) el := UIA.ElementFromHandle("ahk_exe myapp.exe",, False) ``` -------------------------------- ### Get All Text from Browser Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Extracts all text content from the browser, including the Name properties of all child elements. This provides a comprehensive text dump of the current page. ```AutoHotkey GetAllText() ``` -------------------------------- ### Get Current URL Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Retrieves the current URL of the browser. The 'fromAddressBar' parameter allows fetching directly from the URL bar, though this may be less reliable. ```AutoHotkey GetCurrentURL(fromAddressBar:=False) ``` -------------------------------- ### Get Root Element Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Retrieves the root element of the UI Automation tree, which represents the entire desktop. This is the starting point for navigating the UI hierarchy. ```AutoHotkey UIA.GetRootElement() ``` -------------------------------- ### Get Next Sibling Element with TreeWalker Source: https://github.com/descolada/uia-v2/wiki/06.-TreeWalker Demonstrates how to use TreeWalkerTrue to get the next sibling element of a specific UI element. This is useful for iterating through elements at the same level in the UI hierarchy. ```AutoHotkey #Include Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") ; Get the element for the Notepad window editEl := npEl.FindElement({Name:"Edit"}) ; Should return the "Edit" menuitem MsgBox "Next sibling from `"Edit`" menu item: " UIA.TreeWalkerTrue.GetNextSiblingElement(editEl).Dump() ExitApp ``` -------------------------------- ### Initialize Specific Browser Instance (Mozilla with Bookmark JS) Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Initiates a UIA_Mozilla instance with Javascript execution set to 'Bookmark'. This requires a pre-configured bookmark for execution. ```AutoHotkey cUIA := UIA_Mozilla(wTitle="", javascriptExecutionMethod:="Bookmark") ``` -------------------------------- ### UIA_Browser Properties Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Access properties of the UIA_Browser object to get information about the browser instance. ```APIDOC ## UIA_Browser Properties ### BrowserId * **Description**: ahk_id of the browser window. ### BrowserType * **Description**: Type of the browser: "Chrome", "Edge", "Mozilla", or "Unknown". ### BrowserElement * **Description**: The browser window element. Methods called on `cUIA` (e.g., `cUIA.FindElement`) are delegated to this element. ### MainPaneElement * **Description**: The element representing the upper part of the browser, including the URL bar, tabs, and extensions. ### URLEditElement * **Description**: The element representing the browser's address bar. ``` -------------------------------- ### Get Cached Children Source: https://github.com/descolada/uia-v2/wiki/10.-Advanced:-Caching Retrieves cached children of an element. This is faster than fetching live children. ```Python cachedElement.GetCachedChildren() ``` -------------------------------- ### Initialize Specific Browser Instance (Edge) Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Initiates a UIA_Edge instance. While UIA_Browser detects browsers automatically, specific instances can be created if needed. ```AutoHotkey cUIA := UIA_Edge(wTitle="") ``` -------------------------------- ### Initialize Specific Browser Instance (Mozilla with Console JS) Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Initiates a UIA_Mozilla instance with the default Javascript execution method set to 'Console'. ```AutoHotkey cUIA := UIA_Mozilla(wTitle="", javascriptExecutionMethod:="Console") ``` -------------------------------- ### WaitElementNotExist Source: https://github.com/descolada/uia-v2/wiki/04.-Elements Waits for a specified element to disappear. Example: `startingElement.WaitElementNotExist({Name:"TargetElement"})` ```APIDOC ## WaitElementNotExist ### Description Waits for a specified element to no longer exist. ### Method Signature `WaitElementNotExist(condition, timeout := -1, scope := 4, index := 1, startingElement := 0, order := 0, cacheRequest := 0)` ### Parameters - **condition**: The condition of the element to wait for its disappearance. - **timeout** (int, optional): The maximum time in milliseconds to wait. Defaults to -1 (indefinite). - **scope** (int, optional): The scope for searching. Defaults to 4. - **index** (int, optional): The index for searching. Defaults to 1. - **startingElement** (int, optional): The element to start the search from. Defaults to 0. - **order** (int, optional): The order for searching. Defaults to 0. - **cacheRequest** (int, optional): Cache request flag. Defaults to 0. ``` -------------------------------- ### ElementFromPath Source: https://github.com/descolada/uia-v2/wiki/06.-TreeWalker Retrieves an element based on a relative path from a starting element. The path can navigate downwards through children and descendants. ```APIDOC ## ElementFromPath(path1[, path2, ...]) ### Description Gets an element by a relative "path" from a starting element. This path can go only "downwards". If no element is found at the path, then an IndexError is thrown. ### Parameters #### Path Paths can be: 1) Comma-separated numeric path that defines which path to travel down the tree. In addition to integer values, or TypeN which selects the nth occurrence of Type. - Eg. Element.ElementFromPath("3,2") => selects Elements third childs second child - Eg. Element.ElementFromPath("Button3,2") => selects Elements third child of type Button, then its second child 2) UIA path copied from UIAViewer. - Eg. Element.ElementFromPath("bAx3") 3) A condition or conditions. In this case the provided conditions define the route of tree-traversal, by default with Scope Children. - Eg. Element.ElementFromPath({Type:"Button"}, {Type:"List"}) => finds the first Button type child of Element, then the first List type child of that element ``` -------------------------------- ### Initialize Specific Browser Instance (Chrome) Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Initiates a UIA_Chrome instance. While UIA_Browser detects browsers automatically, specific instances can be created if needed. ```AutoHotkey cUIA := UIA_Chrome(wTitle="") ``` -------------------------------- ### Interact with Notepad Window using UIA Source: https://github.com/descolada/uia-v2/wiki/01.-Getting-started Includes the UIA library, runs Notepad, waits for it to be active, finds the Notepad window element, locates the document/edit control, highlights it, and sets its value. Use this to automate interactions with Notepad. ```AutoHotkey #include ; Uncomment if you have moved UIA.ahk to your main Lib folder #include ..\Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" ; Get the element for the Notepad window npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") ; Find the first Document or Edit control (in Notepad there is only one). The type of the edit area depends on your Windows version, in newer ones it's usually Document type. documentEl := npEl.FindElement([{Type:"Document"}, {Type:"Edit"}]) ; Highlight the found element documentEl.Highlight() ; Set the value for the document control. documentEl.Value := "Lorem ipsum" ``` -------------------------------- ### ScrollPattern Source: https://github.com/descolada/uia-v2/wiki/07.-Patterns Allows scrolling of an element. It provides properties to get the current scroll state and methods to perform scrolling actions. ```APIDOC ## ScrollPattern ### Description Provides access to a control that can be scrolled, such as a list or a document. ### Properties * **HorizontalScrollPercent** (number) - The percentage of the content that is scrolled horizontally. * **VerticalScrollPercent** (number) - The percentage of the content that is scrolled vertically. * **HorizontalViewSize** (number) - The visible portion of the content horizontally, expressed as a percentage. * **VerticalViewSize** (number) - The visible portion of the content vertically, expressed as a percentage. * **HorizontallyScrollable** (boolean) - Indicates if the element can be scrolled horizontally. * **VerticallyScrollable** (boolean) - Indicates if the element can be scrolled vertically. ### Methods * **Scroll(horizontal, vertical)** - Scrolls the element by a specified amount. - Parameters: - **horizontal** (ScrollAmount) - The amount to scroll horizontally. Must be one of the UIA.ScrollAmount enumerations. - **vertical** (ScrollAmount) - The amount to scroll vertically. Must be one of the UIA.ScrollAmount enumerations. * **SetScrollPercent(horizontal, vertical)** - Sets the scroll position to a specific percentage. - Parameters: - **horizontal** (number) - The target horizontal scroll percentage (-100 to 100). - **vertical** (number) - The target vertical scroll percentage (-100 to 100). ### ScrollAmount Enumeration * **NoScroll** (-1) * **LargeDecrement** (0) * **SmallDecrement** (1) * **NoAmount** (2) * **LargeIncrement** (3) * **SmallIncrement** (4) ``` -------------------------------- ### Initialize UIA_Browser Instance Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Initiates a new instance of UIA_Browser, hooking onto the browser and pre-fetching common elements. Supports automatic browser type detection. ```AutoHotkey cUIA := UIA_Browser(wTitle="") ``` -------------------------------- ### Initialize UIA-v2 with Optional Configurations Source: https://context7.com/descolada/uia-v2/llms.txt Configure IUIAutomation version, screen-reader mode, or DLL path before including UIA.ahk. The UIA class initializes automatically on first use. ```ahk #Requires AutoHotkey v2 ; Optional: disable screen-reader mode (some apps render differently with it on) global IUIAutomationActivateScreenReader := 0 ; Optional: pin to a specific IUIAutomation version for cross-Windows compatibility global IUIAutomationMaxVersion := 4 ; Optional: use a specific UIAutomationCore.dll global IUIAutomationDllPath := "C:\\Windows\\System32\\UIAutomationCore.dll" #include Lib\UIA.ahk ; UIA is now ready; check running version MsgBox "IUIAutomation version: " UIA.IUIAutomationVersion ``` -------------------------------- ### ElementFromPathExist Source: https://github.com/descolada/uia-v2/wiki/06.-TreeWalker Checks for the existence of an element at a specified path from a starting element. Returns 0 if no element is found, otherwise returns the element. ```APIDOC ## ElementFromPathExist(path1[, path2, ...]) ### Description This is the same as ElementFromPath, but if no element is found at the path then 0 is returned instead of throwing an IndexError. ``` -------------------------------- ### Dump Cached Element Properties and Patterns Source: https://github.com/descolada/uia-v2/wiki/10.-Advanced:-Caching Demonstrates how to create a cache request, add properties and patterns, retrieve an element with its cache, and then dump the cached information. Useful for scenarios where only specific, pre-fetched data is needed. ```AutoHotkey #Include cacheRequest := UIA.CreateCacheRequest() cacheRequest.TreeScope := 5 ; Set TreeScope to include the starting element and all descendants as well ; Add all the necessary properties that DumpAll uses: ControlType, LocalizedControlType, AutomationId, Name, Value, ClassName, AcceleratorKey cacheRequest.AddProperty("ControlType") cacheRequest.AddProperty("LocalizedControlType") cacheRequest.AddProperty("AutomationId") cacheRequest.AddProperty("Name") cacheRequest.AddProperty("Value") cacheRequest.AddProperty("ClassName") cacheRequest.AddProperty("AcceleratorKey") ; To use cached patterns, first add the pattern cacheRequest.AddPattern("Window") ; Also need to add any pattern properties we wish to use cacheRequest.AddProperty("WindowCanMaximize") Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" ; Get element and also build the cache npEl:= UIA.ElementFromHandle("ahk_exe notepad.exe", cacheRequest) ; We now have a cached "snapshot" of the window from which we can access our desired elements faster. MsgBox npEl.CachedDump() MsgBox npEl.CachedWindowPattern.CachedCanMaximize ``` -------------------------------- ### Load Custom UIAutomationCore.dll Source: https://github.com/descolada/uia-v2/wiki/03.-UIA-main-class Specify a custom UIAutomationCore.dll path using `IUIAutomationDllPath` before including UIA.ahk to standardize script functioning and control visible UIA tree elements. ```autohotkey IUIAutomationDllPath := "C:\\Windows\\System32\\UIAutomationCore.dll" #include UIA.ahk MsgBox UIA.ElementFromHandle("A").Dump() ``` -------------------------------- ### Get All Link Elements Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Retrieves an array of all hyperlink elements present on the current browser page. This is useful for interacting with or extracting information from links. ```AutoHotkey GetAllLinks() ``` -------------------------------- ### Navigate Home Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Simulates clicking the browser's Home button, if one is configured and present. ```AutoHotkey Home() ``` -------------------------------- ### Refresh and Get Current Main Pane Element Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Refreshes the UIA_Browser.MainPaneElement and returns it. This is useful for ensuring you have the latest reference to the main browser pane. ```AutoHotkey GetCurrentMainPaneElement() ``` -------------------------------- ### Get Focused Element Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Retrieves the UI Automation element that currently has input focus. This is typically an interactive element like a textbox or button. ```AutoHotkey UIA.GetFocusedElement(cacheRequest:=0) ``` -------------------------------- ### UIA.CreateCacheRequest for Performance Source: https://context7.com/descolada/uia-v2/llms.txt Demonstrates how to create and use cache requests to pre-fetch UI Automation tree data for faster access. ```APIDOC ## `UIA.CreateCacheRequest` — Caching for Performance Cache requests pre-fetch a snapshot of the UIA tree, enabling fast repeated access without re-querying the live tree. ```ahk #include Lib\UIA.ahk ; Create cache request with specific properties, patterns, and scope cacheRequest := UIA.CreateCacheRequest( ["Type", "Name", "Value", "AutomationId", "ClassName", "AcceleratorKey", "WindowCanMaximize"], ["Window"], ; patterns to cache "Subtree" ; TreeScope: cache starting element + all descendants ) ; Alternatively build it step-by-step cacheRequest2 := UIA.CreateCacheRequest() cacheRequest2.TreeScope := 5 ; Subtree cacheRequest2.AddProperty("Type") cacheRequest2.AddProperty("Name") cacheRequest2.AddProperty("Value") cacheRequest2.AddPattern("Window") cacheRequest2.AddProperty("WindowCanMaximize") Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" ; Fetch element + build cache in one call npEl := UIA.ElementFromHandle("ahk_exe notepad.exe", cacheRequest) ; Access cached properties (no live UIA calls) MsgBox npEl.CachedDump() MsgBox "Can maximize: " npEl.CachedWindowPattern.CachedCanMaximize ; Search within cached tree (very fast) cachedDoc := npEl.FindCachedElement({Type:"Document"}) MsgBox "Cached doc name: " cachedDoc.CachedName ``` ``` -------------------------------- ### Get Document Element from Chromium Apps using UIA.ElementFromChromium Source: https://context7.com/descolada/uia-v2/llms.txt Retrieves the Chromium web-content element from Electron/Chrome-based apps. Specify a timeout for accessibility activation. ```ahk #include Lib\UIA.ahk Run "chrome.exe" WinWaitActive "ahk_exe chrome.exe" ; activateChromiumAccessibility: timeout in ms to wait for accessibility activation chromiumEl := UIA.ElementFromChromium("ahk_exe chrome.exe", 1000) MsgBox chromiumEl.DumpAll() ``` -------------------------------- ### Navigate to URL and Wait Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Navigates the browser to a specified URL and then waits for the page to load. Includes options for target title and load timeout. ```AutoHotkey Navigate(url, targetTitle:="", waitLoadTimeOut:=-1, sleepAfter:=500) ``` -------------------------------- ### Get the Currently Focused Element using UIA.GetFocusedElement Source: https://context7.com/descolada/uia-v2/llms.txt Returns the element that currently holds keyboard focus. Useful for interacting with the active UI component. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" focused := UIA.GetFocusedElement() MsgBox "Focused element: " focused.Dump() ; Output: "ControlType: Edit Name: Value: ..." ``` -------------------------------- ### Get All Tab Elements Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Retrieves an array of all tab elements, optionally filtered by a search phrase and matching mode. If no phrase is provided, all tabs are returned. ```AutoHotkey GetTabs(searchPhrase:="", matchMode:=3, caseSensitive:=True) ``` -------------------------------- ### Open New Tab Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Simulates clicking the 'New Tab' button in the browser. ```AutoHotkey NewTab() ``` -------------------------------- ### Performance: UIA Cache Request Source: https://context7.com/descolada/uia-v2/llms.txt Enables performance optimization by pre-fetching a snapshot of the UIA tree for fast, repeated access without re-querying the live tree. Cache requests can be built step-by-step or created with specific properties, patterns, and scope. ```ahk #include Lib\UIA.ahk ; Create cache request with specific properties, patterns, and scope cacheRequest := UIA.CreateCacheRequest( ["Type", "Name", "Value", "AutomationId", "ClassName", "AcceleratorKey", "WindowCanMaximize"], ["Window"], ; patterns to cache "Subtree" ; TreeScope: cache starting element + all descendants ) ; Alternatively build it step-by-step cacheRequest2 := UIA.CreateCacheRequest() cacheRequest2.TreeScope := 5 ; Subtree cacheRequest2.AddProperty("Type") cacheRequest2.AddProperty("Name") cacheRequest2.AddProperty("Value") cacheRequest2.AddPattern("Window") cacheRequest2.AddProperty("WindowCanMaximize") Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" ; Fetch element + build cache in one call npEl := UIA.ElementFromHandle("ahk_exe notepad.exe", cacheRequest) ; Access cached properties (no live UIA calls) MsgBox npEl.CachedDump() MsgBox "Can maximize: " npEl.CachedWindowPattern.CachedCanMaximize ; Search within cached tree (very fast) cachedDoc := npEl.FindCachedElement({Type:"Document"}) MsgBox "Cached doc name: " cachedDoc.CachedName ``` -------------------------------- ### Get Current Document Element Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Retrieves the current document or content element of the browser. This element represents the main content area of the web page. ```AutoHotkey GetCurrentDocumentElement() ``` -------------------------------- ### Iterating Siblings with TreeWalker Source: https://github.com/descolada/uia-v2/wiki/06.-TreeWalker Illustrates how to get the second sibling of an element by calling GetNextSiblingElement twice. This method can be chained to access further siblings in the UI tree. ```AutoHotkey TreeWalkerTrue.GetNextSiblingElement(TreeWalkerTrue.GetNextSiblingElement(Element)) ``` -------------------------------- ### Navigate Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Navigates the browser to a specified URL and waits for the page to load. ```APIDOC ## Navigate ### Description Navigates the browser to the specified URL and then waits for the page to load. Includes options for setting a target title for waiting and a timeout. ### Method `Navigate(url, targetTitle:="", waitLoadTimeOut:=-1, sleepAfter:=500)` ### Parameters * **url** (string) - The URL to navigate to. * **targetTitle** (string) - Optional - The specific title of the page to wait for after navigation. Defaults to waiting for any title change. * **waitLoadTimeOut** (integer) - Optional - The maximum time in milliseconds to wait for the page to load. Defaults to -1 (indefinite). * **sleepAfter** (integer) - Optional - Additional sleep time in milliseconds after the page has loaded. Defaults to 500ms. ``` -------------------------------- ### ShowContextMenu Source: https://github.com/descolada/uia-v2/wiki/04.-Elements Displays the context menu for the element. ```APIDOC ## ShowContextMenu() ### Description Displays the context menu for the element. ``` -------------------------------- ### UIA.GetRootElement Source: https://context7.com/descolada/uia-v2/llms.txt Returns the root (desktop) element, which is the parent of all open windows. ```APIDOC ## `UIA.GetRootElement` — Get the Desktop Root Element Returns the root (desktop) element, which is the parent of all open windows. ```ahk #include Lib\\UIA.ahk root := UIA.GetRootElement() ; Enumerate all top-level windows for child in root.Children OutputDebug child.Name " [" child.Type "]" ``` ``` -------------------------------- ### IUIAutomationAndCondition Methods Source: https://github.com/descolada/uia-v2/wiki/05.-Conditions Provides access to the child conditions of an AndCondition. ```APIDOC ## IUIAutomationAndCondition `GetChildren()` returns an array of conditions that the AndCondition consists of. The type of the conditions can be determined with Type(condition) ``` -------------------------------- ### ElementFromIAccessible Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Gets a UI Automation element from a Microsoft Active Accessibility (MSAA) object. This method is generally discouraged as UIA provides more reliable alternatives for most elements. ```APIDOC ## ElementFromIAccessible ### Description Gets a UI Automation element from a MSAA/Acc object. This method is less preferred and may not work for all window types. ### Method `UIA.ElementFromIAccessible(IAcc, childId:=0)` ### Endpoint N/A (Method call) ### Parameters - **IAcc** (object) - The MSAA/Acc object. - **childId** (number) - Optional - The child ID of the element within the MSAA object. ### Request Example ``` accElement := UIA.ElementFromIAccessible(myAccObject) ``` ### Response #### Success Response (Element) - **element** (Element) - The UI Automation element derived from the MSAA object. ``` -------------------------------- ### Element Debugging: Highlight and Dump Source: https://context7.com/descolada/uia-v2/llms.txt Utilities for visual and textual debugging of element identification. Highlight draws a border around an element, and Dump provides a one-line summary or a full subtree dump. Highlight can be configured for duration, color, and thickness, and can be cleared. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") docEl := npEl.FindElement([{Type:"Document"}, {Type:"Edit"}]) ; Highlight: draws a colored border around the element docEl.Highlight() ; flash for 2 seconds (default) docEl.Highlight(0) ; highlight indefinitely docEl.Highlight(-1000) ; highlight 1s without blocking execution docEl.Highlight("clear") ; remove any active highlight docEl.Highlight(2000, "Blue", 4) ; 2s, blue border, 4px thick ; Dump: returns a one-line summary of the element MsgBox docEl.Dump() ; e.g. "ControlType: Document Name: AutomationId: 15 ClassName: RichEditD2DPT" ; Dump with scope MsgBox npEl.Dump(UIA.TreeScope.Children) ; dump element + direct children ; DumpAll: full subtree dump A_Clipboard := npEl.DumpAll() ; Useful for pasting into Notepad to study the full UIA tree structure ``` -------------------------------- ### CreatePropertyCondition Example Source: https://github.com/descolada/uia-v2/wiki/05.-Conditions Creates a condition that filters elements based on a specific property ID and value. Use UIA.Property.Name or its numerical equivalent (30005) for the Name property. ```javascript UIA.CreatePropertyCondition(UIA.Property.Name, "test") ``` ```javascript CreatePropertyCondition(30005, "test") ``` -------------------------------- ### Home Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Navigates to the browser's configured home page, if a home button is present. ```APIDOC ## Home ### Description Simulates the action of clicking the browser's Home button to navigate to the default home page. ### Method `Home()` ``` -------------------------------- ### Navigate Back Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Simulates clicking the browser's Back button to navigate to the previous page in the history. ```AutoHotkey Back() ``` -------------------------------- ### Element Interaction: Click, SetFocus, Value, Invoke, Toggle, Select, Expand/Collapse, Scroll, Window Source: https://context7.com/descolada/uia-v2/llms.txt Provides high-level shortcuts and pattern-specific methods for interacting with UI elements. Supports various interaction patterns like Click, SetFocus, Value, Invoke, Toggle, Select, ExpandCollapse, Scroll, and Window manipulation. Click can be customized with mouse button and double-click options. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") ; ---- Click (uses Invoke/Toggle/Select patterns automatically) ---- fileMenuBtn := npEl.FindElement({Type:"MenuItem", Name:"File"}) fileMenuBtn.Click() ; auto-uses InvokePattern fileMenuBtn.Click(200) ; click then sleep 200ms fileMenuBtn.Click("left") ; force AHK mouse click fileMenuBtn.Click("left", 2) ; double-click ; ---- SetFocus ---- docEl := npEl.FindElement([{Type:"Document"}, {Type:"Edit"}]) docEl.SetFocus() ; ---- Value (read/write on editable controls) ---- docEl.Value := "Automated text" MsgBox "Current value: " docEl.Value ; ---- InvokePattern ---- btn := npEl.FindElement({Type:"Button", Name:"Close"}) btn.Invoke() ; via auto-dispatch btn.InvokePattern.Invoke() ; explicit pattern access ; ---- TogglePattern ---- chk := npEl.FindElement({Type:"CheckBox", Name:"Hidden items"}) chk.Toggle() MsgBox "Toggle state: " chk.ToggleState ; 0=Off, 1=On, 2=Indeterminate chk.ToggleState := 1 ; set directly ; ---- SelectionItem (tabs, list items) ---- tab := npEl.FindElement({Type:"TabItem", Name:"View"}) tab.Select() MsgBox "Selected: " tab.IsSelected ; ---- ExpandCollapse (menus, combo boxes) ---- menu := npEl.FindElement({Type:"MenuItem", Name:"File"}) menu.Expand() Sleep 500 menu.Collapse() MsgBox "State: " menu.ExpandCollapseState ; 0=Collapsed, 1=Expanded ; ---- ScrollPattern ---- list := npEl.FindElement({Type:"List"}) list.Scroll(UIA.ScrollAmount.NoScroll, UIA.ScrollAmount.LargeIncrement) list.SetScrollPercent(-1, 50) ; scroll vertical to 50% ; ---- WindowPattern ---- winEl := UIA.ElementFromHandle("ahk_exe notepad.exe") winEl.SetWindowVisualState(UIA.WindowVisualState.Maximized) MsgBox "Can maximize: " winEl.CanMaximize winEl.Close() ``` -------------------------------- ### Combined OR Condition with Type Source: https://github.com/descolada/uia-v2/wiki/05.-Conditions This example demonstrates combining a 'Type' condition with an 'or' condition on the 'Name' property. It matches elements of type 'Button' whose name is either 'Something' or 'Else'. ```javascript {Type:"Button", or:[{Name:"Something"}, {Name:"Else"}]} ``` -------------------------------- ### Navigate Forward Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Simulates clicking the browser's Forward button to navigate to the next page in the history. ```AutoHotkey Forward() ``` -------------------------------- ### Traverse UI Tree using Paths Source: https://context7.com/descolada/uia-v2/llms.txt Navigate the UIA tree using numeric indices, UIAViewer paths, or condition sequences. `ElementFromPathExist` returns 0 if not found, avoiding errors. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") ; Numeric path: 4th child, then its 2nd child el1 := npEl.ElementFromPath("4,2") ; UIA path (copied from UIAViewer) el2 := npEl.ElementFromPath("ABq") ; MenuBar -> 2nd MenuItem = "Edit" ; Condition path: first MenuBar child, then child named "File" el3 := npEl.ElementFromPath({Type:"MenuBar"}, {Name:"File"}) ; Array notation (same as ElementFromPath) el4 := npEl[{T:10, A:"MenuBar"}, {T:11, N:"Edit"}] el5 := npEl[4, 2] ; Type+index path: 3rd Button-type child, then its 2nd child el6 := npEl.ElementFromPath("Button3,2") ; ElementFromPathExist returns 0 instead of throwing if not found el7 := npEl.ElementFromPathExist("99,99") ; returns 0 ; WaitElementFromPath waits for element to appear at path el8 := npEl.WaitElementFromPath({Type:"Button", Name:"OK"}, 3000) ``` -------------------------------- ### WindowPattern Source: https://github.com/descolada/uia-v2/wiki/07.-Patterns Provides access to the fundamental functionality of a window, such as maximizing, minimizing, and closing. ```APIDOC ## WindowPattern ### Description Provides access to the fundamental functionality of a window. ### Properties * **CanMaximize** (bool) * **CanMinimize** (bool) * **IsModal** (bool) * **IsTopmost** (bool) * **WindowVisualState** (UIA.WindowVisualState) - Normal, Maximized, Minimized * **WindowInteractionState** (UIA.WindowInteractionState) - Running, Closing, ReadyForUserInteraction, BlockedByModalWindow, NotResponding ### Methods * **Close()** * **WaitForInputIdle(milliseconds)** * **SetWindowVisualState(state)** - state can be one of UIA.WindowVisualState enums (Normal, Maximized, Minimized) ``` -------------------------------- ### Get Element from Chromium Application Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Retrieves the UI Automation element from a Chromium-based application (e.g., Chrome, Edge, Electron apps). It can optionally activate UIA accessibility if it's not already enabled. ```AutoHotkey UIA.ElementFromChromium(winTitle:="", activateChromiumAccessibility:=500, cacheRequest:=0) ``` -------------------------------- ### Back Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Simulates clicking the browser's Back button to navigate to the previous page. ```APIDOC ## Back ### Description Simulates the action of clicking the browser's Back button. ### Method `Back()` ``` -------------------------------- ### UIA_Browser for Chrome, Edge, and Firefox Automation Source: https://context7.com/descolada/uia-v2/llms.txt Automate browsers with UIA_Browser, supporting navigation, tab management, and JavaScript execution. Ensure the UIA and UIA_Browser libraries are included. ```ahk #include Lib\UIA.ahk #include Lib\UIA_Browser.ahk Run "chrome.exe -incognito" WinWaitActive "ahk_exe chrome.exe" Sleep 500 ; Initialize — auto-detects browser type cUIA := UIA_Browser() MsgBox "Browser: " cUIA.BrowserType ; "Chrome" ; Navigate and wait for page load cUIA.Navigate("https://example.com", "Example Domain", 10000) ; Get current URL MsgBox "URL: " cUIA.GetCurrentURL() ; Dump page content A_Clipboard := cUIA.GetCurrentDocumentElement().DumpAll() ; Get all links for link in cUIA.GetAllLinks() OutputDebug link.Name " => " link.Value ; Tab management cUIA.NewTab() cUIA.Navigate("https://google.com") cUIA.WaitPageLoad("Google", 10000) allTabNames := cUIA.GetAllTabNames() MsgBox "Open tabs: " allTabNames.Length cUIA.SelectTab("Example Domain", 2) ; matchMode=2: contains cUIA.CloseTab("Google") ; close by name ; JavaScript execution cUIA.JSExecute("document.title = 'My Title'") result := cUIA.JSReturnThroughClipboard("return document.title") MsgBox "JS result: " result ; Find elements within the page pageEl := cUIA.GetCurrentDocumentElement() searchBox := pageEl.FindElement({Type:"Edit", Name:"Search"}) searchBox.Value := "AutoHotkey" ; Browser navigation cUIA.Back() cUIA.Forward() cUIA.Reload() ; Wait for title change cUIA.WaitTitleChange("Search Results", 5000) ``` -------------------------------- ### Get Specific Tab Element Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Retrieves a tab element based on a search phrase and matching mode. If no phrase is provided, it returns the currently selected tab. Throws TargetError if no matching tab is found. ```AutoHotkey GetTab(searchPhrase:="", matchMode:=3, caseSensitive:=True) ``` -------------------------------- ### Build Cache with FindElement Source: https://github.com/descolada/uia-v2/wiki/10.-Advanced:-Caching Builds the cache by providing a CacheRequest to a FindElement method. This pre-fetches specified data. ```Python cachedElement := FindElement(condition,,,, cacheRequest) ``` -------------------------------- ### Get Element from IAccessible Object Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Obtains a UI Automation element from a legacy MSAA/Acc object. This method is generally discouraged as it only works with objects that natively implement IAccessible and UIA often provides more reliable alternatives. ```AutoHotkey UIA.ElementFromIAccessible(IAcc, childId:=0) ``` -------------------------------- ### Creating Event Handlers Source: https://github.com/descolada/uia-v2/wiki/09.-Advanced:-Events Event handlers are created using `UIA.Create...EventHandler(callback)`. The specific function depends on the event type. The callback function signature varies based on the handler type. ```APIDOC ## Creating Handlers New event handlers can be created with `UIA.Create...EventHandler(callback)`, that returns a new handler object. * `funcName`: the function that will receive the calls when an event happens The "..." depends on the type of event that the handler will be used with. For example, if using in `UIA.AddAutomationEventHandler`, then use `UIA.CreateAutomationEventHandler` to create the handler. Depending on the type of handler, the callback function needs to accept a certain number of parameters: * `HandleAutomationEvent(sender, eventId)` <--- this is the most common handler type created with AddAutomationEventHandler, and the handler function needs to have exactly two arguments: sender (the element which sent the event), and eventId. * `HandleFocusChangedEvent(sender)` * `HandlePropertyChangedEvent(sender, propertyId, newValue)` * `HandleStructureChangedEvent(sender, changeType, runtimeId)` * `HandleTextEditTextChangedEvent(sender, changeType, eventStrings)` * `HandleChangesEvent(sender, uiaChanges, changesCount)` * `HandleNotificationEvent(sender, notificationKind, notificationProcessing, displayString, activityId)` For example, the most common event handler is the AddAutomationEventHandler. For this type of handler you only need to specify a function name that will be called when the event happens, and that function needs to have two arguments: ``` handler := UIA.CreateAutomationEventHandler("HandleMyEvent") ; Add event listener with UIA.AddAutomationEventHandler(handler, eventId) HandleMyEvent(senderElement, eventId) { ; Do something } ``` ``` -------------------------------- ### Find Element within Tree Source: https://github.com/descolada/uia-v2/wiki/02.-UIA-tree Searches for a specific element within the UI Automation tree starting from a given element, using a defined condition. This is a common way to navigate and locate desired UI components. ```AutoHotkey Element.FindElement({Name:"Something"}) ``` -------------------------------- ### JSReturnThroughClipboard Source: https://github.com/descolada/uia-v2/wiki/12.-UIA_Browser Executes JavaScript code and returns its result via the system clipboard. The clipboard content is reset after retrieval. ```APIDOC ## JSReturnThroughClipboard Executes Javascript code using the address bar and returns the return value of the code using the clipboard (resetting it back afterwards) ### Method POST ### Endpoint /js/execute/clipboard ### Parameters #### Request Body - **js** (string) - Required - The JavaScript code to execute. ``` -------------------------------- ### Accessing Grid Data with GridPattern Source: https://context7.com/descolada/uia-v2/llms.txt Use GridPattern to get row and column counts, access specific cells by index, and iterate through all cells in a data grid. GridItemPattern can provide row and column information for individual cells. ```ahk #include Lib\UIA.ahk ; Assumes a window with a DataGrid control is active gridEl := UIA.ElementFromHandle("A").FindElement({Type:"DataGrid"}) gp := gridEl.GridPattern MsgBox "Rows: " gp.RowCount " Columns: " gp.ColumnCount ; Access a specific cell (0-based) cell := gp.GetItem(0, 1) ; row 0, column 1 MsgBox "Cell[0,1]: " cell.Name ; Iterate all cells loop gp.RowCount { row := A_Index - 1 loop gp.ColumnCount { col := A_Index - 1 c := gp.GetItem(row, col) OutputDebug "[" row "," col "]: " c.Name } } ; GridItem properties item := gp.GetItem(1, 2) ip := item.GridItemPattern MsgBox "Row: " ip.Row " Col: " ip.Column " RowSpan: " ip.RowSpan ; TablePattern (includes row/column headers) tableEl := UIA.ElementFromHandle("A").FindElement({Type:"Table"}) tp := tableEl.TablePattern rowHeaders := tp.GetCurrentRowHeaders() colHeaders := tp.GetCurrentColumnHeaders() for h in colHeaders OutputDebug "Column header: " h.Name ``` -------------------------------- ### Element.ElementFromPath / Array Notation Source: https://context7.com/descolada/uia-v2/llms.txt Traverse the UI element tree using numeric paths, UIA paths, or condition sequences. Includes methods to check existence and wait for elements at a path. ```APIDOC ## `Element.ElementFromPath` / Array Notation — Tree Path Traversal Traverses the UIA tree step-by-step downward using numeric paths, UIA paths from UIAViewer, or condition sequences. ```ahk #include Lib\UIA.ahk Run "notepad.exe" WinWaitActive "ahk_exe notepad.exe" npEl := UIA.ElementFromHandle("ahk_exe notepad.exe") ; Numeric path: 4th child, then its 2nd child el1 := npEl.ElementFromPath("4,2") ; UIA path (copied from UIAViewer) el2 := npEl.ElementFromPath("ABq") ; MenuBar -> 2nd MenuItem = "Edit" ; Condition path: first MenuBar child, then child named "File" el3 := npEl.ElementFromPath({Type:"MenuBar"}, {Name:"File"}) ; Array notation (same as ElementFromPath) el4 := npEl[{T:10, A:"MenuBar"}, {T:11, N:"Edit"}] el5 := npEl[4, 2] ; Type+index path: 3rd Button-type child, then its 2nd child el6 := npEl.ElementFromPath("Button3,2") ; ElementFromPathExist returns 0 instead of throwing if not found el7 := npEl.ElementFromPathExist("99,99") ; returns 0 ; WaitElementFromPath waits for element to appear at path el8 := npEl.WaitElementFromPath({Type:"Button", Name:"OK"}, 3000) ``` ``` -------------------------------- ### Dump Source: https://github.com/descolada/uia-v2/wiki/04.-Elements `Dump(scope:=1, delimiter:=" ", maxDepth:=-1)` returns info about the element. ```APIDOC ## Dump ### Description Returns information about the element, including ControlType, Name, Value, LocalizedControlType, AutomationId, and AcceleratorKey. ### Method Signature `Dump(scope:=1, delimiter:=" ", maxDepth:=-1)` ### Parameters * **scope** - Optional. UIA.TreeScope value specifying the scope of the dump. Defaults to `Element`. * **delimiter** - Optional. Delimiter to separate the returned properties. Defaults to a space. * **maxDepth** - Optional. Maximal depth of tree levels traversal. Defaults to unlimited (-1). ```