### Traverse UI Hierarchy with TreeWalker and TreeVisitor in Rust Source: https://context7.com/eiz/accessibility/llms.txt Illustrates how to traverse the UI element hierarchy using `TreeWalker` and `TreeVisitor`. The `TreeWalker` performs a depth-first traversal, invoking methods on a `TreeVisitor` implementation for each element. This example uses a custom `TreePrinter` to display the UI tree structure. Requires the 'accessibility' crate. ```rust use accessibility::{ AXUIElement, AXUIElementAttributes, TreeWalker, TreeVisitor, TreeWalkerFlow }; use std::cell::Cell; // Custom visitor to print the UI tree struct TreePrinter { depth: Cell, } impl TreeVisitor for TreePrinter { fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow { let indent = " ".repeat(self.depth.get()); let role = element.role().unwrap_or_default(); let title = element.title().ok(); println!("{}{}: {:?}", indent, role, title); self.depth.set(self.depth.get() + 1); // Control traversal: // TreeWalkerFlow::Continue - Continue to children // TreeWalkerFlow::SkipSubtree - Skip this element's children // TreeWalkerFlow::Exit - Stop traversal entirely TreeWalkerFlow::Continue } fn exit_element(&self, _element: &AXUIElement) { self.depth.set(self.depth.get() - 1); } } // Walk an application's UI tree let app = AXUIElement::application(1234); let walker = TreeWalker::new(); let printer = TreePrinter { depth: Cell::new(0) }; walker.walk(&app, &printer); ``` -------------------------------- ### AXUIElement::system_wide Source: https://context7.com/eiz/accessibility/llms.txt Creates a system-wide accessibility element that can be used to query elements across all applications, such as getting the currently focused element. ```APIDOC ## AXUIElement::system_wide ### Description Creates a system-wide accessibility element that can be used to query elements across all applications, such as getting the currently focused element. ### Method `AXUIElement::system_wide() -> AXUIElement` ### Endpoint N/A (Function call) ### Parameters None ### Request Example ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; // Get system-wide element for cross-application queries let system = AXUIElement::system_wide(); // Get the currently focused element across all apps match system.focused_window() { Ok(focused) => { if let Ok(title) = focused.title() { println!("Focused window: {}", title); } } Err(e) => eprintln!("No focused window: {}", e), } ``` ### Response #### Success Response (AXUIElement) Represents a system-wide accessibility element. #### Response Example (See Request Example for usage) ``` -------------------------------- ### Create System-Wide Accessibility Element Source: https://context7.com/eiz/accessibility/llms.txt Creates a system-wide accessibility element, enabling queries across all applications. This is useful for tasks like getting the currently focused element. It requires the `accessibility` crate and `AXUIElementAttributes` trait. ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; // Get system-wide element for cross-application queries let system = AXUIElement::system_wide(); // Get the currently focused element across all apps match system.focused_window() { Ok(focused) => { if let Ok(title) = focused.title() { println!("Focused window: {}", title); } } Err(e) => eprintln!("No focused window: {}", e), } ``` -------------------------------- ### Query Application Accessibility Tree with aq (Bash) Source: https://context7.com/eiz/accessibility/llms.txt Illustrates how to build and use the `aq` command-line tool to query the accessibility tree of a target application. This involves first finding the application's Process ID (PID) using `pgrep` and then running `aq` with the PID. ```bash # Build the aq tool car go build --release -p aq # Query an application's accessibility tree by PID # First, find the PID of the target application pgrep -x "Safari" # Example output: 1234 # Then run aq with that PID ./target/release/aq 1234 # Example output: # - AXApplication (3 children) # | . AXTitle: "Safari" # | . AXRole: "AXApplication" # - AXWindow (15 children) # |. AXTitle: "Apple" # |. AXPosition: CGPoint { x: 0.0, y: 25.0 } # - AXToolbar (5 children) # ... ``` -------------------------------- ### AXUIElement::application_with_bundle Source: https://context7.com/eiz/accessibility/llms.txt Creates an accessibility element for an application using its bundle identifier (e.g., "com.apple.Safari"). Returns an error if the application is not running. ```APIDOC ## AXUIElement::application_with_bundle ### Description Creates an accessibility element for an application using its bundle identifier (e.g., "com.apple.Safari"). Returns an error if the application is not running. ### Method `AXUIElement::application_with_bundle(bundle_id: &str) -> Result` `AXUIElement::application_with_bundle_timeout(bundle_id: &str, timeout: Duration) -> Result` ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use accessibility::{AXUIElement, AXUIElementAttributes, Error}; use std::time::Duration; // Find application by bundle ID match AXUIElement::application_with_bundle("com.apple.Safari") { Ok(safari) => { if let Ok(title) = safari.title() { println!("Safari title: {}", title); } } Err(Error::NotFound) => eprintln!("Safari is not running"), Err(e) => eprintln!("Error: {}", e), } // With timeout - waits up to 5 seconds for the app to launch match AXUIElement::application_with_bundle_timeout( "com.apple.TextEdit", Duration::from_secs(5) ) { Ok(textedit) => println!("Found TextEdit"), Err(e) => eprintln!("TextEdit not found within timeout: {}", e), } ``` ### Response #### Success Response (AXUIElement) Represents an accessibility element for the specified application. #### Error Response - **NotFound**: The application with the given bundle ID is not running. - **Error**: Other potential errors during element creation. #### Response Example (See Request Example for usage) ``` -------------------------------- ### Find UI Elements with ElementFinder and Implicit Wait in Rust Source: https://context7.com/eiz/accessibility/llms.txt Demonstrates the use of `ElementFinder` for searching the UI tree, including optional implicit waiting for elements to become available. This is useful for automation where elements might not be present immediately. It requires the 'accessibility' crate and 'std::time::Duration'. ```rust use accessibility::{ AXUIElement, AXUIElementAttributes, AXUIElementActions, ElementFinder }; use std::time::Duration; let app = AXUIElement::application(1234); // Find an element by predicate with implicit wait let finder = ElementFinder::new( &app, |element| { element.role().map(|r| r.to_string() == "AXButton").unwrap_or(false) && element.title().map(|t| t.to_string() == "OK").unwrap_or(false) }, Some(Duration::from_secs(5)) // Wait up to 5 seconds ); // Find the element match finder.find() { Ok(button) => { println!("Found OK button"); // Can use attributes/actions directly on finder let _ = finder.press(); } Err(e) => eprintln!("Button not found: {}", e), } // Reset cached result for re-searching finder.reset(); ``` -------------------------------- ### AXUIElement::application Source: https://context7.com/eiz/accessibility/llms.txt Creates an accessibility element reference for an application using its process ID (PID). This is the primary entry point for interacting with a specific application's UI hierarchy. ```APIDOC ## AXUIElement::application ### Description Creates an accessibility element reference for an application by its process ID (PID). This is the primary entry point for interacting with a specific application's UI hierarchy. ### Method `AXUIElement::application(pid: i32) -> AXUIElement` ### Endpoint N/A (Function call) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; // Get accessibility element for an application by PID let pid: i32 = 1234; // Replace with actual process ID let app = AXUIElement::application(pid); // Query the application's windows match app.windows() { Ok(windows) => { println!("Application has {} windows", windows.len()); for window in windows.iter() { if let Ok(title) = window.title() { println!("Window title: {}", title); } } } Err(e) => eprintln!("Error accessing windows: {}", e), } ``` ### Response #### Success Response (AXUIElement) Represents an accessibility element for the specified application. #### Response Example (See Request Example for usage) ``` -------------------------------- ### Create Application Accessibility Element by PID Source: https://context7.com/eiz/accessibility/llms.txt Creates an accessibility element reference for a running application using its process ID (PID). This is a primary entry point for interacting with an application's UI. It requires the `accessibility` crate and `AXUIElementAttributes` trait. ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; // Get accessibility element for an application by PID let pid: i32 = 1234; // Replace with actual process ID let app = AXUIElement::application(pid); // Query the application's windows match app.windows() { Ok(windows) => { println!("Application has {} windows", windows.len()); for window in windows.iter() { if let Ok(title) = window.title() { println!("Window title: {}", title); } } } Err(e) => eprintln!("Error accessing windows: {}", e), } ``` -------------------------------- ### Read and Set AXUIElement Attributes in Rust Source: https://context7.com/eiz/accessibility/llms.txt Demonstrates how to read and write accessibility attributes of a UI element using generic methods. It covers reading attributes like 'title' and setting attributes like 'frontmost', along with checking attribute settability. Requires the 'accessibility' and 'core_foundation' crates. ```rust use accessibility::{AXUIElement, AXAttribute, AXUIElementAttributes}; use core_foundation::string::CFString; use core_foundation::boolean::CFBoolean; let app = AXUIElement::application(1234); // Read attribute using generic method let title_attr: AXAttribute = AXAttribute::title(); match app.attribute(&title_attr) { Ok(title) => println!("Title: {}", title), Err(e) => eprintln!("Error: {}", e), } // Set attribute (e.g., bring window to front) if let Ok(main_window) = app.main_window() { // Set frontmost attribute to bring to front let _ = main_window.set_frontmost(CFBoolean::true_value()); // Or use the generic set_attribute let frontmost_attr = AXAttribute::frontmost(); let _ = main_window.set_attribute(&frontmost_attr, CFBoolean::true_value()); } // Check if attribute is settable let pos_attr = AXAttribute::position(); if let Ok(settable) = app.is_settable(&pos_attr) { println!("Position is settable: {}", settable); } ``` -------------------------------- ### Perform Accessibility Actions with AXUIElementActions in Rust Source: https://context7.com/eiz/accessibility/llms.txt Shows how to perform standard accessibility actions on UI elements using the `AXUIElementActions` trait. This includes pressing buttons, incrementing/decrementing values, and showing menus. It depends on the 'accessibility' crate. ```rust use accessibility::{AXUIElement, AXUIElementAttributes, AXUIElementActions}; let app = AXUIElement::application(1234); // Find a button and press it if let Ok(children) = app.children() { for child in children.iter() { if let Ok(role) = child.role() { if role.to_string() == "AXButton" { // Press the button match child.press() { Ok(()) => println!("Button pressed"), Err(e) => eprintln!("Failed to press: {}", e), } } } } } // Available actions: // element.press() - Press a button // element.increment() - Increment a slider/stepper // element.decrement() - Decrement a slider/stepper // element.confirm() - Confirm a dialog // element.raise() - Raise a window // element.show_menu() - Show context menu // element.pick() - Pick/select an item // element.show_alternate_ui() - Show alternate UI // element.show_default_ui() - Show default UI ``` -------------------------------- ### Check and Request Accessibility Permissions (Rust) Source: https://context7.com/eiz/accessibility/llms.txt Demonstrates how to check if the current process has accessibility permissions using `AXIsProcessTrusted` and how to request these permissions with a prompt using `AXIsProcessTrustedWithOptions`. This is crucial for interacting with other applications' UI elements. ```rust use accessibility_sys::{ AXIsProcessTrusted, AXIsProcessTrustedWithOptions, kAXTrustedCheckOptionPrompt, AXUIElementCopyElementAtPosition, AXUIElementCreateSystemWide, AXUIElementRef, kAXErrorSuccess, }; use core_foundation::dictionary::CFDictionary; use core_foundation::boolean::CFBoolean; use core_foundation::string::CFString; use std::ptr; // Check if process has accessibility permissions let trusted = unsafe { AXIsProcessTrusted() }; println!("Accessibility trusted: {}", trusted); // Request accessibility permissions with prompt unsafe { let key = CFString::wrap_under_get_rule(kAXTrustedCheckOptionPrompt); let options = CFDictionary::from_CFType_pairs(&[(key, CFBoolean::true_value())]); let trusted = AXIsProcessTrustedWithOptions(options.as_concrete_TypeRef()); println!("Accessibility enabled: {}", trusted); } // Get element at screen position unsafe { let system = AXUIElementCreateSystemWide(); let mut element: AXUIElementRef = ptr::null_mut(); let result = AXUIElementCopyElementAtPosition(system, 100.0, 100.0, &mut element); if result == kAXErrorSuccess && !element.is_null() { println!("Found element at position (100, 100)"); } } ``` -------------------------------- ### Create Application Accessibility Element by Bundle ID Source: https://context7.com/eiz/accessibility/llms.txt Creates an accessibility element for an application using its bundle identifier (e.g., "com.apple.Safari"). It returns an error if the application is not running. An optional timeout can be specified for `application_with_bundle_timeout` to wait for the application to launch. Requires the `accessibility` crate and `AXUIElementAttributes` trait. ```rust use accessibility::{AXUIElement, AXUIElementAttributes, Error}; use std::time::Duration; // Find application by bundle ID match AXUIElement::application_with_bundle("com.apple.Safari") { Ok(safari) => { if let Ok(title) = safari.title() { println!("Safari title: {}", title); } } Err(Error::NotFound) => eprintln!("Safari is not running"), Err(e) => eprintln!("Error: {}", e), } // With timeout - waits up to 5 seconds for the app to launch match AXUIElement::application_with_bundle_timeout( "com.apple.TextEdit", Duration::from_secs(5) ) { Ok(textedit) => println!("Found TextEdit"), Err(e) => eprintln!("TextEdit not found within timeout: {}", e), } ``` -------------------------------- ### Read AXUIElement Attributes Source: https://context7.com/eiz/accessibility/llms.txt Demonstrates reading common UI element attributes using the `AXUIElementAttributes` trait. This trait provides accessor methods for properties like role, title, position, size, frame, enabled state, and children. It is implemented for `AXUIElement` and `ElementFinder`. Requires the `accessibility` crate. ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; let app = AXUIElement::application(1234); // Read common attributes if let Ok(role) = app.role() { println!("Role: {}", role); // e.g., "AXApplication" } if let Ok(title) = app.title() { println!("Title: {}", title); } // Get element position and size (frame) if let Ok(main_window) = app.main_window() { if let Ok(position) = main_window.position() { println!("Position: ({}, {})", position.x, position.y); } if let Ok(size) = main_window.size() { println!("Size: {}x{}", size.width, size.height); } if let Ok(frame) = main_window.frame() { println!("Frame: {:?}", frame); } } // Check if element is enabled/focused if let Ok(enabled) = app.enabled() { println!("Enabled: {}", enabled); } // Get child elements if let Ok(children) = app.children() { println!("Has {} direct children", children.len()); } ``` -------------------------------- ### AXUIElementAttributes Trait Source: https://context7.com/eiz/accessibility/llms.txt Provides convenient accessor methods for common UI element attributes like title, role, position, size, and children. Implemented for both `AXUIElement` and `ElementFinder`. ```APIDOC ## AXUIElementAttributes Trait ### Description Provides convenient accessor methods for common UI element attributes like title, role, position, size, and children. Implemented for both `AXUIElement` and `ElementFinder`. ### Method Methods include `role()`, `title()`, `position()`, `size()`, `frame()`, `enabled()`, `children()`, `focused_window()`, `windows()`, etc. ### Endpoint N/A (Trait implementation) ### Parameters N/A ### Request Example ```rust use accessibility::{AXUIElement, AXUIElementAttributes}; let app = AXUIElement::application(1234); // Read common attributes if let Ok(role) = app.role() { println!("Role: {}", role); // e.g., "AXApplication" } if let Ok(title) = app.title() { println!("Title: {}", title); } // Get element position and size (frame) if let Ok(main_window) = app.main_window() { if let Ok(position) = main_window.position() { println!("Position: ({}, {})", position.x, position.y); } if let Ok(size) = main_window.size() { println!("Size: {}x{}", size.width, size.height); } if let Ok(frame) = main_window.frame() { println!("Frame: {:?}", frame); } } // Check if element is enabled/focused if let Ok(enabled) = app.enabled() { println!("Enabled: {}", enabled); } // Get child elements if let Ok(children) = app.children() { println!("Has {} direct children", children.len()); } ``` ### Response #### Success Response Returns the requested attribute value (e.g., String for title, tuple for position, Vec for children). #### Response Example (See Request Example for usage) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.