Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

playwright for your deskop (#1549)

Louis Beaumontf833c8f60b1a
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.toml b/screenpipe-app-tauri/src-tauri/Cargo.toml
index 9368a4c26..253c329e6 100644
--- a/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -92,7 +92,7 @@ tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] }
tauri-plugin-updater = "2.0.0"
[target.'cfg(target_os = "macos")'.dependencies]
-core-foundation = "0.9.4"
+core-foundation = "=0.10.0"
libc = "0.2.152"
cocoa = "0.26.0"
objc = "0.2.7"
diff --git a/screenpipe-core/Cargo.toml b/screenpipe-core/Cargo.toml
index cd467cbdf..3ad470701 100644
--- a/screenpipe-core/Cargo.toml
+++ b/screenpipe-core/Cargo.toml
@@ -58,3 +58,15 @@ metal = ["candle/metal", "candle-nn/metal", "candle-transformers/metal"]
cuda = ["candle/cuda", "candle-nn/cuda", "candle-transformers/cuda"]
mkl = ["candle/mkl", "candle-nn/mkl", "candle-transformers/mkl"]
llm = []
+
+[target.'cfg(target_os = "macos")'.dependencies]
+# accessibility-sys = "0.1.3"
+accessibility-sys = { git = "https://github.com/eiz/accessibility.git", branch = "master" }
+# accessibility = "0.1.6"
+accessibility = { git = "https://github.com/eiz/accessibility.git", branch = "master" }
+objc = "0.2.7"
+objc-foundation = "0.1.1"
+
+thiserror = "2.0.12"
+core-foundation = "=0.10.0"
+core-graphics = "=0.24.0"
diff --git a/screenpipe-core/src/lib.rs b/screenpipe-core/src/lib.rs
index 505927844..a72fe12bd 100644
--- a/screenpipe-core/src/lib.rs
+++ b/screenpipe-core/src/lib.rs
@@ -37,3 +37,7 @@ pub use network::*;
pub use language::{Language, TESSERACT_LANGUAGES};
pub mod embedding;
+pub use embedding::*;
+
+pub mod operator;
+pub use operator::*;
diff --git a/screenpipe-core/src/operator/element.rs b/screenpipe-core/src/operator/element.rs
new file mode 100644
index 000000000..1ffbb8ff2
--- /dev/null
+++ b/screenpipe-core/src/operator/element.rs
@@ -0,0 +1,188 @@
+use crate::operator::errors::AutomationError;
+use crate::operator::selector::Selector;
+use std::collections::HashMap;
+use std::fmt::Debug;
+
+use super::Locator;
+/// Represents a UI element in a desktop application
+#[derive(Debug)]
+pub struct UIElement {
+ inner: Box<dyn UIElementImpl>,
+}
+
+/// Attributes associated with a UI element
+#[derive(Debug)]
+pub struct UIElementAttributes {
+ pub role: String,
+ pub label: Option<String>,
+ pub value: Option<String>,
+ pub description: Option<String>,
+ pub properties: HashMap<String, Option<serde_json::Value>>,
+}
+
+/// Interface for platform-specific element implementations
+pub(crate) trait UIElementImpl: Send + Sync + Debug {
+ fn object_id(&self) -> usize;
+ fn id(&self) -> Option<String>;
+ fn role(&self) -> String;
+ fn attributes(&self) -> UIElementAttributes;
+ fn children(&self) -> Result<Vec<UIElement>, AutomationError>;
+ fn parent(&self) -> Result<Option<UIElement>, AutomationError>;
+ fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError>; // x, y, width, height
+ fn click(&self) -> Result<(), AutomationError>;
+ fn double_click(&self) -> Result<(), AutomationError>;
+ fn right_click(&self) -> Result<(), AutomationError>;
+ fn hover(&self) -> Result<(), AutomationError>;
+ fn focus(&self) -> Result<(), AutomationError>;
+ fn type_text(&self, text: &str) -> Result<(), AutomationError>;
+ fn press_key(&self, key: &str) -> Result<(), AutomationError>;
+ fn get_text(&self, max_depth: usize) -> Result<String, AutomationError>;
+ fn set_value(&self, value: &str) -> Result<(), AutomationError>;
+ fn is_enabled(&self) -> Result<bool, AutomationError>;
+ fn is_visible(&self) -> Result<bool, AutomationError>;
+ fn is_focused(&self) -> Result<bool, AutomationError>;
+ fn perform_action(&self, action: &str) -> Result<(), AutomationError>;
+ fn as_any(&self) -> &dyn std::any::Any;
+ fn create_locator(&self, selector: Selector) -> Result<Locator, AutomationError>;
+
+ // Add a method to clone the box
+ fn clone_box(&self) -> Box<dyn UIElementImpl>;
+}
+
+impl UIElement {
+ /// Create a new UI element from a platform-specific implementation
+ pub(crate) fn new(impl_: Box<dyn UIElementImpl>) -> Self {
+ Self { inner: impl_ }
+ }
+
+ /// Get the element's ID
+ pub fn id(&self) -> Option<String> {
+ self.inner.id()
+ }
+
+ /// Get the element's role (e.g., "button", "textfield")
+ pub fn role(&self) -> String {
+ self.inner.role()
+ }
+
+ /// Get all attributes of the element
+ pub fn attributes(&self) -> UIElementAttributes {
+ self.inner.attributes()
+ }
+
+ /// Get child elements
+ pub fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
+ self.inner.children()
+ }
+
+ /// Get parent element
+ pub fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
+ self.inner.parent()
+ }
+
+ /// Get element bounds (x, y, width, height)
+ pub fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
+ self.inner.bounds()
+ }
+
+ /// Click on this element
+ pub fn click(&self) -> Result<(), AutomationError> {
+ self.inner.click()
+ }
+
+ /// Double-click on this element
+ pub fn double_click(&self) -> Result<(), AutomationError> {
+ self.inner.double_click()
+ }
+
+ /// Right-click on this element
+ pub fn right_click(&self) -> Result<(), AutomationError> {
+ self.inner.right_click()
+ }
+
+ /// Hover over this element
+ pub fn hover(&self) -> Result<(), AutomationError> {
+ self.inner.hover()
+ }
+
+ /// Focus this element
+ pub fn focus(&self) -> Result<(), AutomationError> {
+ self.inner.focus()
+ }
+
+ /// Type text into this element
+ pub fn type_text(&self, text: &str) -> Result<(), AutomationError> {
+ self.inner.type_text(text)
+ }
+
+ /// Press a key while this element is focused
+ pub fn press_key(&self, key: &str) -> Result<(), AutomationError> {
+ self.inner.press_key(key)
+ }
+
+ /// Get text content of this element
+ pub fn text(&self, max_depth: usize) -> Result<String, AutomationError> {
+ self.inner.get_text(max_depth)
+ }
+
+ /// Set value of this element
+ pub fn set_value(&self, value: &str) -> Result<(), AutomationError> {
+ self.inner.set_value(value)
+ }
+
+ /// Check if element is enabled
+ pub fn is_enabled(&self) -> Result<bool, AutomationError> {
+ self.inner.is_enabled()
+ }
+
+ /// Check if element is visible
+ pub fn is_visible(&self) -> Result<bool, AutomationError> {
+ self.inner.is_visible()
+ }
+
+ /// Check if element is focused
+ pub fn is_focused(&self) -> Result<bool, AutomationError> {
+ self.inner.is_focused()
+ }
+
+ /// Perform a named action on this element
+ pub fn perform_action(&self, action: &str) -> Result<(), AutomationError> {
+ self.inner.perform_action(action)
+ }
+
+ /// Get the underlying implementation as a specific type
+ pub(crate) fn as_any(&self) -> &dyn std::any::Any {
+ self.inner.as_any()
+ }
+
+ /// Find elements matching the selector within this element
+ pub fn locator(&self, selector: impl Into<Selector>) -> Result<Locator, AutomationError> {
+ let selector = selector.into();
+ self.inner.create_locator(selector)
+ }
+}
+
+impl PartialEq for UIElement {
+ fn eq(&self, other: &Self) -> bool {
+ self.inner.object_id() == other.inner.object_id()
+ }
+}
+
+impl Eq for UIElement {}
+
+impl std::hash::Hash for UIElement {
+ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
+ self.inner.object_id().hash(state);
+ }
+}
+
+impl Clone for UIElement {
+ fn clone(&self) -> Self {
+ // We can't directly clone the inner Box<dyn UIElementImpl>,
+ // but we can create a new UIElement with the same identity
+ // that will behave the same way
+ Self {
+ inner: self.inner.clone_box(),
+ }
+ }
+}
diff --git a/screenpipe-core/src/operator/errors.rs b/screenpipe-core/src/operator/errors.rs
new file mode 100644
index 000000000..f665a688c
--- /dev/null
+++ b/screenpipe-core/src/operator/errors.rs
@@ -0,0 +1,28 @@
+use thiserror::Error;
+
+#[derive(Error, Debug)]
+pub enum AutomationError {
+ #[error("Element not found: {0}")]
+ ElementNotFound(String),
+
+ #[error("Operation timed out: {0}")]
+ Timeout(String),
+
+ #[error("Permission denied: {0}")]
+ PermissionDenied(String),
+
+ #[error("Platform-specific error: {0}")]
+ PlatformError(String),
+
+ #[error("Unsupported operation: {0}")]
+ UnsupportedOperation(String),
+
+ #[error("Unsupported platform: {0}")]
+ UnsupportedPlatform(String),
+
+ #[error("Invalid argument: {0}")]
+ InvalidArgument(String),
+
+ #[error("Internal error: {0}")]
+ Internal(String),
+}
diff --git a/screenpipe-core/src/operator/locator.rs b/screenpipe-core/src/operator/locator.rs
new file mode 100644
index 000000000..c91a4ca27
--- /dev/null
+++ b/screenpipe-core/src/operator/locator.rs
@@ -0,0 +1,117 @@
+use crate::operator::platforms::AccessibilityEngine;
+use crate::operator::{AutomationError, Selector, UIElement};
+use std::sync::Arc;
+use std::time::Duration;
+
+/// A high-level API for finding and interacting with UI elements
+pub struct Locator {
+ engine: Arc<dyn AccessibilityEngine>,
+ selector: Selector,
+ timeout: Duration,
+ root: Option<UIElement>,
+}
+
+impl Locator {
+ /// Create a new locator with the given selector
+ pub(crate) fn new(engine: Arc<dyn AccessibilityEngine>, selector: Selector) -> Self {
+ Self {
+ engine,
+ selector,
+ timeout: Duration::from_secs(30),
+ root: None,
+ }
+ }
+
+ /// Set timeout for waiting operations
+ pub fn timeout(mut self, timeout: Duration) -> Self {
+ self.timeout = timeout;
+ self
+ }
+
+ /// Set the root element for this locator
+ pub fn within(mut self, element: UIElement) -> Self {
+ self.root = Some(element);
+ self
+ }
+
+ /// Get the first element matching this locator
+ pub fn first(&self) -> Result<Option<UIElement>, AutomationError> {
+ let element = self
+ .engine
+ .find_element(&self.selector, self.root.as_ref())?;
+ Ok(Some(element))
+ }
+
+ /// Get all elements matching this locator
+ pub fn all(&self) -> Result<Vec<UIElement>, AutomationError> {
+ // Check if we can use platform-specific find_elements method
+ if let Ok(elements) = self
+ .engine
+ .find_elements(&self.selector, self.root.as_ref())
+ {
+ return Ok(elements);
+ }
+
+ // Fallback implementation - get the first element, then get its siblings
+ // Note: This is a naive implementation and might not work correctly in all cases
+ match self.first()? {
+ Some(first) => {
+ let result = vec![first];
+ // In a proper implementation, we would need to search for siblings
+ // or implement a custom ElementCollector that gathers all matches
+ Ok(result)
+ }
+ None => Ok(vec![]),
+ }
+ }
+
+ /// Wait for an element to be available
+ pub async fn wait(&self) -> Result<UIElement, AutomationError> {
+ let start = std::time::Instant::now();
+
+ while start.elapsed() < self.timeout {
+ if let Some(element) = self.first()? {
+ return Ok(element);
+ }
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ }
+
+ Err(AutomationError::Timeout(format!(
+ "Timed out waiting for selector: {:?}",
+ self.selector
+ )))
+ }
+
+ /// Get a nested locator
+ pub fn locator(&self, selector: impl Into<Selector>) -> Locator {
+ let selector = selector.into();
+ Locator {
+ engine: self.engine.clone(),
+ selector: Selector::Chain(vec![self.selector.clone(), selector]),
+ timeout: self.timeout,
+ root: self.root.clone(),
+ }
+ }
+
+ // Convenience methods for common actions
+
+ /// Click on the first matching element
+ pub async fn click(&self) -> Result<(), AutomationError> {
+ self.wait().await?.click()
+ }
+
+ /// Type text into the first matching element
+ pub async fn type_text(&self, text: &str) -> Result<(), AutomationError> {
+ self.wait().await?.type_text(text)
+ }
+
+ /// Press a key on the first matching element
+ pub async fn press_key(&self, key: &str) -> Result<(), AutomationError> {
+ self.wait().await?.press_key(key)
+ }
+
+ /// Get text from the first matching element
+ pub async fn text(&self, max_depth: usize) -> Result<String, AutomationError> {
+ self.wait().await?.text(max_depth)
+ }
+}
diff --git a/screenpipe-core/src/operator/mod.rs b/screenpipe-core/src/operator/mod.rs
new file mode 100644
index 000000000..cce6eea33
--- /dev/null
+++ b/screenpipe-core/src/operator/mod.rs
@@ -0,0 +1,60 @@
+//! Desktop UI automation through accessibility APIs
+//!
+//! This module provides a cross-platform API for automating desktop applications
+//! through accessibility APIs, inspired by Playwright's web automation model.
+
+use std::sync::Arc;
+
+mod element;
+mod errors;
+mod locator;
+mod platforms;
+mod selector;
+#[cfg(test)]
+mod tests;
+mod tree_search;
+
+pub use element::{UIElement, UIElementAttributes};
+pub use errors::AutomationError;
+pub use locator::Locator;
+pub use selector::Selector;
+
+/// The main entry point for UI automation
+pub struct Desktop {
+ engine: Arc<dyn platforms::AccessibilityEngine>,
+}
+
+impl Desktop {
+ /// Create a new instance with the default platform-specific implementation
+ pub fn new(use_background_apps: bool, activate_app: bool) -> Result<Self, AutomationError> {
+ let boxed_engine = platforms::create_engine(use_background_apps, activate_app)?;
+ // Move the boxed engine into an Arc
+ let engine = Arc::from(boxed_engine);
+ Ok(Self { engine })
+ }
+
+ /// Get the root UI element representing the entire desktop
+ pub fn root(&self) -> UIElement {
+ self.engine.get_root_element()
+ }
+
+ /// Create a locator to find elements matching the given selector
+ pub fn locator(&self, selector: impl Into<Selector>) -> Locator {
+ Locator::new(Arc::clone(&self.engine), selector.into())
+ }
+
+ /// Get the currently focused element
+ pub fn focused_element(&self) -> Result<UIElement, AutomationError> {
+ self.engine.get_focused_element()
+ }
+
+ /// List all running applications
+ pub fn applications(&self) -> Result<Vec<UIElement>, AutomationError> {
+ self.engine.get_applications()
+ }
+
+ /// Find an application by name
+ pub fn application(&self, name: &str) -> Result<UIElement, AutomationError> {
+ self.engine.get_application_by_name(name)
+ }
+}
diff --git a/screenpipe-core/src/operator/platforms/linux.rs b/screenpipe-core/src/operator/platforms/linux.rs
new file mode 100644
index 000000000..2cbe0bcd5
--- /dev/null
+++ b/screenpipe-core/src/operator/platforms/linux.rs
@@ -0,0 +1,197 @@
+use crate::operator::element::UIElementImpl;
+use crate::operator::platforms::AccessibilityEngine;
+use crate::operator::{AutomationError, Locator, Selector, UIElement, UIElementAttributes};
+use std::fmt::Debug;
+
+pub struct LinuxEngine;
+
+impl LinuxEngine {
+ pub fn new() -> Result<Self, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+}
+
+impl AccessibilityEngine for LinuxEngine {
+ fn get_root_element(&self) -> UIElement {
+ panic!("Linux implementation is not yet available")
+ }
+
+ fn get_element_by_id(&self, _id: &str) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_focused_element(&self) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_applications(&self) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_application_by_name(&self, _name: &str) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn find_elements(
+ &self,
+ _selector: &Selector,
+ _root: Option<&UIElement>,
+ ) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+}
+
+// Placeholder LinuxUIElement that implements UIElementImpl
+pub struct LinuxUIElement;
+
+impl Debug for LinuxUIElement {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("LinuxUIElement").finish()
+ }
+}
+
+impl UIElementImpl for LinuxUIElement {
+ fn object_id(&self) -> usize {
+ 0
+ }
+
+ fn id(&self) -> Option<String> {
+ None
+ }
+
+ fn role(&self) -> String {
+ "".to_string()
+ }
+
+ fn attributes(&self) -> UIElementAttributes {
+ UIElementAttributes {
+ role: "".to_string(),
+ label: None,
+ value: None,
+ description: None,
+ properties: std::collections::HashMap::new(),
+ }
+ }
+
+ fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn double_click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn right_click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn hover(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn focus(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn type_text(&self, _text: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn press_key(&self, _key: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_text(&self) -> Result<String, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn set_value(&self, _value: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_enabled(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_visible(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_focused(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn perform_action(&self, _action: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
+ fn create_locator(&self, _selector: Selector) -> Result<Locator, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Linux implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn clone_box(&self) -> Box<dyn UIElementImpl> {
+ Box::new(LinuxUIElement)
+ }
+}
diff --git a/screenpipe-core/src/operator/platforms/macos.rs b/screenpipe-core/src/operator/platforms/macos.rs
new file mode 100644
index 000000000..12e0c92a1
--- /dev/null
+++ b/screenpipe-core/src/operator/platforms/macos.rs
@@ -0,0 +1,1753 @@
+use crate::operator::platforms::AccessibilityEngine;
+use crate::operator::tree_search::{ElementFinderWithWindows, TreeWalkerWithWindows};
+use crate::operator::{
+ element::UIElementImpl, AutomationError, Locator, Selector, UIElement, UIElementAttributes,
+};
+
+use accessibility::AXUIElementAttributes;
+use accessibility::{AXAttribute, AXUIElement};
+use anyhow::Result;
+use core_foundation::array::{
+ CFArrayGetCount, CFArrayGetTypeID, CFArrayGetValueAtIndex, __CFArray,
+};
+use core_foundation::base::{CFGetTypeID, TCFType};
+use core_foundation::boolean::CFBoolean;
+use core_foundation::dictionary::CFDictionary;
+use core_foundation::string::CFString;
+use core_graphics::display::{CGPoint, CGSize};
+use serde_json::{self, Value};
+use std::collections::{HashMap, HashSet, VecDeque};
+use std::fmt;
+use std::sync::Arc;
+use tracing::{debug, trace};
+
+// Import the C function for setting attributes
+#[link(name = "ApplicationServices", kind = "framework")]
+extern "C" {
+ fn AXUIElementSetAttributeValue(
+ element: *mut ::std::os::raw::c_void,
+ attribute: *const ::std::os::raw::c_void,
+ value: *const ::std::os::raw::c_void,
+ ) -> i32;
+}
+
+// Add these extern "C" declarations if not already present
+extern "C" {
+ fn AXValueGetValue(
+ value: *const ::std::os::raw::c_void,
+ type_: u32,
+ out: *mut ::std::os::raw::c_void,
+ ) -> i32;
+}
+
+// Add these constant definitions instead - these are the official values from Apple's headers
+const K_AXVALUE_CGPOINT_TYPE: u32 = 1;
+const K_AXVALUE_CGSIZE_TYPE: u32 = 2;
+
+// Thread-safe wrapper for AXUIElement
+#[derive(Clone)]
+pub struct ThreadSafeAXUIElement(Arc<AXUIElement>);
+
+// Implement Send and Sync for our wrapper
+// SAFETY: AXUIElement is safe to send and share between threads as Apple's
+// accessibility API is designed to be called from any thread. The underlying
+// Core Foundation objects manage their own thread safety.
+unsafe impl Send for ThreadSafeAXUIElement {}
+unsafe impl Sync for ThreadSafeAXUIElement {}
+
+impl ThreadSafeAXUIElement {
+ pub fn new(element: AXUIElement) -> Self {
+ Self(Arc::new(element))
+ }
+
+ pub fn system_wide() -> Self {
+ Self(Arc::new(AXUIElement::system_wide()))
+ }
+
+ pub fn application(pid: i32) -> Self {
+ Self(Arc::new(AXUIElement::application(pid)))
+ }
+
+ pub fn clone(&self) -> Self {
+ Self(self.0.clone())
+ }
+}
+
+// Implement Debug
+impl fmt::Debug for ThreadSafeAXUIElement {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_tuple("ThreadSafeAXUIElement")
+ .field(&"<AXUIElement>")
+ .finish()
+ }
+}
+
+pub struct MacOSEngine {
+ system_wide: ThreadSafeAXUIElement,
+ use_background_apps: bool,
+ activate_app: bool,
+}
+
+impl MacOSEngine {
+ pub fn new(use_background_apps: bool, activate_app: bool) -> Result<Self, AutomationError> {
+ // Check accessibility permissions using FFI directly
+ // Since accessibility::AXIsProcessTrustedWithOptions is not available
+ let accessibility_enabled = unsafe {
+ use core_foundation::dictionary::CFDictionaryRef;
+
+ #[link(name = "ApplicationServices", kind = "framework")]
+ extern "C" {
+ fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> bool;
+ }
+
+ let check_attr = CFString::new("AXTrustedCheckOptionPrompt");
+ let options = CFDictionary::from_CFType_pairs(&[(
+ check_attr.as_CFType(),
+ CFBoolean::true_value().as_CFType(),
+ )])
+ .as_concrete_TypeRef();
+
+ AXIsProcessTrustedWithOptions(options)
+ };
+
+ if !accessibility_enabled {
+ return Err(AutomationError::PermissionDenied(
+ "Accessibility permissions not granted".to_string(),
+ ));
+ }
+
+ Ok(Self {
+ system_wide: ThreadSafeAXUIElement::system_wide(),
+ use_background_apps,
+ activate_app,
+ })
+ }
+
+ // Helper to convert ThreadSafeAXUIElement to our UIElement
+ fn wrap_element(&self, ax_element: ThreadSafeAXUIElement) -> UIElement {
+ // Try to check element validity
+ let is_valid = match ax_element.0.role() {
+ Ok(_) => true,
+ Err(e) => {
+ debug!(target: "operator", "Warning: Potentially invalid AXUIElement: {:?}", e);
+ false
+ }
+ };
+
+ if !is_valid {
+ debug!(target: "operator", "Warning: Wrapping possibly invalid AXUIElement");
+ }
+
+ UIElement::new(Box::new(MacOSUIElement {
+ element: ax_element,
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ }))
+ }
+
+ // Add this new method to refresh the accessibility tree
+ pub fn refresh_accessibility_tree(
+ &self,
+ app_name: Option<&str>,
+ ) -> Result<(), AutomationError> {
+ if !self.activate_app {
+ return Ok(());
+ }
+
+ debug!(target: "operator", "Refreshing accessibility tree");
+
+ // If app name is provided, try to activate that app first
+ if let Some(name) = app_name {
+ unsafe {
+ use objc::{class, msg_send, sel, sel_impl};
+
+ let workspace_class = class!(NSWorkspace);
+ let shared_workspace: *mut objc::runtime::Object =
+ msg_send![workspace_class, sharedWorkspace];
+ let apps: *mut objc::runtime::Object =
+ msg_send![shared_workspace, runningApplications];
+ let count: usize = msg_send![apps, count];
+
+ for i in 0..count {
+ let app: *mut objc::runtime::Object = msg_send![apps, objectAtIndex:i];
+ let app_name_obj: *mut objc::runtime::Object = msg_send![app, localizedName];
+
+ if !app_name_obj.is_null() {
+ let app_name_str: &str = {
+ let nsstring = app_name_obj as *const objc::runtime::Object;
+ let bytes: *const std::os::raw::c_char =
+ msg_send![nsstring, UTF8String];
+ let len: usize = msg_send![nsstring, lengthOfBytesUsingEncoding:4]; // NSUTF8StringEncoding = 4
+ let bytes_slice = std::slice::from_raw_parts(bytes as *const u8, len);
+ std::str::from_utf8_unchecked(bytes_slice)
+ };
+
+ if app_name_str.to_lowercase() == name.to_lowercase() {
+ // Found the app, activate it
+ let _: () = msg_send![app, activateWithOptions:1]; // NSApplicationActivateIgnoringOtherApps = 1
+ debug!(target: "operator", "Activated application: {}", name);
+
+ // Give the system a moment to update
+ std::thread::sleep(std::time::Duration::from_millis(100));
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ // Force a refresh of the system-wide element
+ // This is a bit of a hack, but querying the system-wide element
+ // can force the accessibility API to refresh its cache
+ let _ = self.system_wide.0.attribute_names();
+
+ Ok(())
+ }
+}
+
+// Modified to return Vec<String> for multiple possible role matches
+fn map_generic_role_to_macos_roles(role: &str) -> Vec<String> {
+ match role.to_lowercase().as_str() {
+ "window" => vec!["AXWindow".to_string()],
+ "button" => vec![
+ "AXButton".to_string(),
+ "AXMenuItem".to_string(),
+ "AXMenuBarItem".to_string(),
+ "AXStaticText".to_string(), // Some text might be clickable buttons
+ "AXImage".to_string(), // Some images might be clickable buttons
+ ], // Button can be any of these
+ "checkbox" => vec!["AXCheckBox".to_string()],
+ "menu" => vec!["AXMenu".to_string()],
+ "menuitem" => vec!["AXMenuItem".to_string(), "AXMenuBarItem".to_string()], // Include both types
+ "dialog" => vec!["AXSheet".to_string(), "AXDialog".to_string()], // macOS often uses Sheet or Dialog
+ "text" | "textfield" | "input" | "textbox" => vec![
+ "AXTextField".to_string(),
+ "AXTextArea".to_string(),
+ "AXText".to_string(),
+ "AXComboBox".to_string(),
+ "AXTextEdit".to_string(),
+ "AXSearchField".to_string(),
+ "AXWebArea".to_string(), // Web content might contain inputs
+ "AXGroup".to_string(), // Twitter uses groups that contain editable content
+ "AXGenericElement".to_string(), // Generic elements that might be inputs
+ "AXURIField".to_string(), // Explicit URL field type
+ "AXAddressField".to_string(), // Another common name for URL fields
+ "AXStaticText".to_string(), // Static text fields
+ ],
+ // Add specific support for URL fields
+ "url" | "urlfield" => vec![
+ "AXTextField".to_string(), // URL fields are often text fields
+ "AXURIField".to_string(), // Explicit URL field type
+ "AXAddressField".to_string(), // Another common name for URL fields
+ ],
+ "list" => vec!["AXList".to_string()],
+ "listitem" => vec!["AXCell".to_string()], // List items are often cells in macOS
+ "combobox" => vec!["AXPopUpButton".to_string(), "AXComboBox".to_string()],
+ "tab" => vec!["AXTabGroup".to_string()],
+ "tabitem" => vec!["AXRadioButton".to_string()], // Tab items are sometimes radio buttons
+ "toolbar" => vec!["AXToolbar".to_string()],
+
+ _ => vec![role.to_string()], // Keep as-is for unknown roles
+ }
+}
+
+fn macos_role_to_generic_role(role: &str) -> Vec<String> {
+ match role.to_lowercase().as_str() {
+ "AXWindow" => vec!["window".to_string()],
+ "AXButton" | "AXMenuItem" | "AXMenuBarItem" => vec!["button".to_string()],
+ "AXTextField" | "AXTextArea" | "AXTextEdit" | "AXSearchField" | "AXURIField"
+ | "AXAddressField" => vec![
+ "textfield".to_string(),
+ "input".to_string(),
+ "textbox".to_string(),
+ "url".to_string(),
+ "urlfield".to_string(),
+ ],
+ "AXList" => vec!["list".to_string()],
+ "AXCell" => vec!["listitem".to_string()],
+ "AXSheet" | "AXDialog" => vec!["dialog".to_string()],
+ "AXGroup" | "AXGenericElement" | "AXWebArea" => {
+ vec!["group".to_string(), "genericElement".to_string()]
+ }
+ _ => vec![role.to_string()],
+ }
+}
+// Helper function to get PIDs of running applications using NSWorkspace
+#[allow(clippy::all)]
+fn get_running_application_pids(use_background_apps: bool) -> Result<Vec<i32>, AutomationError> {
+ // Implementation using Objective-C bridging
+ unsafe {
+ use objc::{class, msg_send, sel, sel_impl};
+
+ let workspace_class = class!(NSWorkspace);
+ let shared_workspace: *mut objc::runtime::Object =
+ msg_send![workspace_class, sharedWorkspace];
+ let apps: *mut objc::runtime::Object = msg_send![shared_workspace, runningApplications];
+ let count: usize = msg_send![apps, count];
+
+ let mut pids = Vec::with_capacity(count);
+ for i in 0..count {
+ let app: *mut objc::runtime::Object = msg_send![apps, objectAtIndex:i];
+
+ if !use_background_apps {
+ let activation_policy: i32 = msg_send![app, activationPolicy];
+ // NSApplicationActivationPolicyRegular = 0
+ // NSApplicationActivationPolicyAccessory = 1
+ // NSApplicationActivationPolicyProhibited = 2 (background only)
+ if activation_policy == 2 || activation_policy == 1 {
+ // NSApplicationActivationPolicyProhibited or NSApplicationActivationPolicyAccessory
+ continue;
+ }
+ }
+ // Filter out common background workers by bundle identifier
+ let bundle_id: *mut objc::runtime::Object = msg_send![app, bundleIdentifier];
+ if !bundle_id.is_null() {
+ let bundle_id_str: &str = {
+ let nsstring = bundle_id as *const objc::runtime::Object;
+ let bytes: *const std::os::raw::c_char = msg_send![nsstring, UTF8String];
+ let len: usize = msg_send![nsstring, lengthOfBytesUsingEncoding:4]; // NSUTF8StringEncoding = 4
+ let bytes_slice = std::slice::from_raw_parts(bytes as *const u8, len);
+ std::str::from_utf8_unchecked(bytes_slice)
+ };
+
+ // Skip common background processes and workers
+ if bundle_id_str.contains(".worker")
+ || bundle_id_str.contains("com.apple.WebKit")
+ || bundle_id_str.contains("com.apple.CoreServices")
+ || bundle_id_str.contains(".helper")
+ || bundle_id_str.contains(".agent")
+ {
+ debug!(target: "operator", "Filtered out background worker: {}", bundle_id_str);
+ continue;
+ }
+ }
+
+ let pid: i32 = msg_send![app, processIdentifier];
+ pids.push(pid);
+ }
+
+ debug!(target: "operator", "Found {} application PIDs", pids.len());
+ Ok(pids)
+ }
+}
+
+impl AccessibilityEngine for MacOSEngine {
+ fn get_applications(&self) -> Result<Vec<UIElement>, AutomationError> {
+ // Get running application PIDs using NSWorkspace
+ let pids = get_running_application_pids(self.use_background_apps)?;
+
+ debug!(target: "operator", "Found {} running applications", pids.len());
+
+ // Create AXUIElements for each application
+ let mut app_elements = Vec::new();
+ for pid in pids {
+ trace!(target: "operator", "Creating AXUIElement for application with PID: {}", pid);
+ let app_element = ThreadSafeAXUIElement::application(pid);
+
+ app_elements.push(self.wrap_element(app_element));
+ }
+
+ Ok(app_elements)
+ }
+ fn get_root_element(&self) -> UIElement {
+ self.wrap_element(self.system_wide.clone())
+ }
+
+ fn get_focused_element(&self) -> Result<UIElement, AutomationError> {
+ // not implemented
+ Err(AutomationError::UnsupportedOperation(
+ "get_focused_element not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn get_application_by_name(&self, name: &str) -> Result<UIElement, AutomationError> {
+ // Refresh the accessibility tree before searching
+ self.refresh_accessibility_tree(Some(name))?;
+
+ // Get all applications first, then filter by name
+ let apps = self.get_applications()?;
+
+ debug!(target: "operator", "Searching for application '{}' among {} applications", name, apps.len());
+
+ // Look for an application with a matching name
+ for app in apps {
+ let app_name = app.attributes().label.unwrap_or_default();
+ debug!(target: "operator", "Checking application: '{}'", app_name);
+
+ // Case-insensitive comparison since macOS app names might have different casing
+ if app_name.to_lowercase() == name.to_lowercase() {
+ debug!(target: "operator", "Found matching application: '{}'", app_name);
+ return Ok(app);
+ }
+ }
+
+ // No matching application found
+ Err(AutomationError::ElementNotFound(format!(
+ "Application '{}' not found",
+ name
+ )))
+ }
+
+ fn find_element(
+ &self,
+ selector: &Selector,
+ root: Option<&UIElement>,
+ ) -> Result<UIElement, AutomationError> {
+ // If we have a root element that's an application, refresh the tree for that app
+ if let Some(root_elem) = root {
+ if let Some(macos_el) = root_elem.as_any().downcast_ref::<MacOSUIElement>() {
+ if macos_el
+ .element
+ .0
+ .role()
+ .map_or(false, |r| r.to_string() == "AXApplication")
+ {
+ if let Some(app_name) = root_elem.attributes().label {
+ self.refresh_accessibility_tree(Some(&app_name))?;
+ }
+ }
+ }
+ }
+
+ let start_element = root
+ .map(|el| {
+ if let Some(macos_el) = el.as_any().downcast_ref::<MacOSUIElement>() {
+ &macos_el.element.0
+ } else {
+ panic!("Root element is not a macOS element")
+ }
+ })
+ .unwrap_or(&self.system_wide.0);
+
+ // Regular element finding logic
+ match selector {
+ Selector::Role { role, name: _ } => {
+ // Get all possible macOS roles for this generic role
+ let macos_roles = map_generic_role_to_macos_roles(role);
+
+ let collector = ElementFinderWithWindows::new(
+ &self.system_wide.0,
+ move |e| {
+ let element_role = e.role().unwrap_or(CFString::new("")).to_string();
+ macos_roles.contains(&element_role)
+ },
+ None,
+ );
+ let walker: TreeWalkerWithWindows = TreeWalkerWithWindows::new();
+
+ walker.walk(start_element, &collector);
+
+ let ax_ui_element = match collector.find() {
+ Ok(ax_ui_element) => ax_ui_element,
+ Err(_) => {
+ return Err(AutomationError::ElementNotFound(format!(
+ "Element with role '{}' not found",
+ role
+ )))
+ }
+ };
+ Ok(self.wrap_element(ThreadSafeAXUIElement::new(ax_ui_element)))
+ }
+ Selector::Id(id) => {
+ let id_owned = id.clone(); // Create an owned copy
+ let collector = ElementFinderWithWindows::new(
+ &self.system_wide.0,
+ move |e| {
+ // Use move to take ownership of id_owned
+ e.identifier().unwrap_or(CFString::new("")).to_string() == id_owned
+ },
+ None,
+ );
+ let walker: TreeWalkerWithWindows = TreeWalkerWithWindows::new();
+
+ walker.walk(start_element, &collector);
+
+ let ax_ui_element = match collector.find() {
+ Ok(ax_ui_element) => ax_ui_element,
+ Err(_) => {
+ return Err(AutomationError::ElementNotFound(format!(
+ "Element with ID '{}' not found",
+ id
+ )))
+ }
+ };
+ Ok(self.wrap_element(ThreadSafeAXUIElement::new(ax_ui_element)))
+ }
+ Selector::Name(name) => {
+ let name_owned = name.clone(); // Create an owned copy
+ let collector = ElementFinderWithWindows::new(
+ &self.system_wide.0,
+ move |e| {
+ // Use move to take ownership of name_owned
+ e.title().unwrap_or(CFString::new("")).to_string() == name_owned
+ },
+ None,
+ );
+ let walker: TreeWalkerWithWindows = TreeWalkerWithWindows::new();
+
+ walker.walk(start_element, &collector);
+
+ let ax_ui_element = match collector.find() {
+ Ok(ax_ui_element) => ax_ui_element,
+ Err(_) => {
+ return Err(AutomationError::ElementNotFound(format!(
+ "Element with name '{}' not found",
+ name
+ )))
+ }
+ };
+ Ok(self.wrap_element(ThreadSafeAXUIElement::new(ax_ui_element)))
+ }
+
+ Selector::Text(text) => {
+ let text_owned = text.clone(); // Create an owned copy
+
+ // Create a collector that recursively checks children
+ let collector = ElementFinderWithWindows::new(
+ &self.system_wide.0,
+ move |e| {
+ // First check if element itself contains the text in any attribute
+ if element_contains_text(e, &text_owned) {
+ return true;
+ }
+
+ false
+ },
+ None,
+ );
+
+ let walker: TreeWalkerWithWindows = TreeWalkerWithWindows::new();
+
+ walker.walk(start_element, &collector);
+
+ let ax_ui_element = match collector.find() {
+ Ok(ax_ui_element) => ax_ui_element,
+ Err(_) => {
+ return Err(AutomationError::ElementNotFound(format!(
+ "Element with text '{}' not found",
+ text
+ )))
+ }
+ };
+ Ok(self.wrap_element(ThreadSafeAXUIElement::new(ax_ui_element)))
+ }
+ Selector::Attributes(_attrs) => Err(AutomationError::UnsupportedOperation(
+ "Attributes selector not implemented".to_string(),
+ )),
+ Selector::Path(_) => Err(AutomationError::UnsupportedOperation(
+ "Path selector not implemented".to_string(),
+ )),
+ _ => {
+ // For more complex selectors, we'll mark as unimplemented for now
+ Err(AutomationError::UnsupportedOperation(
+ "Complex selector not implemented".to_string(),
+ ))
+ }
+ }
+ }
+
+ fn find_elements(
+ &self,
+ selector: &Selector,
+ root: Option<&UIElement>,
+ ) -> Result<Vec<UIElement>, AutomationError> {
+ use crate::operator::tree_search::ElementsCollectorWithWindows;
+
+ // Get the start element from the provided root or fall back to system_wide
+ let start_element = root
+ .map(|el| {
+ if let Some(macos_el) = el.as_any().downcast_ref::<MacOSUIElement>() {
+ &macos_el.element.0
+ } else {
+ panic!("Root element is not a macOS element")
+ }
+ })
+ .unwrap_or(&self.system_wide.0);
+
+ match selector {
+ Selector::Role { role, name: _ } => {
+ let macos_roles = map_generic_role_to_macos_roles(role);
+
+ let collector = ElementsCollectorWithWindows::new(start_element, move |e| {
+ let element_role = e.role().unwrap_or(CFString::new("")).to_string();
+ macos_roles.contains(&element_role)
+ });
+
+ let ax_ui_elements = collector.find_all();
+
+ // Convert AXUIElements to UIElements
+ let ui_elements = ax_ui_elements
+ .into_iter()
+ .map(|e| self.wrap_element(ThreadSafeAXUIElement::new(e)))
+ .collect();
+
+ Ok(ui_elements)
+ }
+ Selector::Id(id) => {
+ let id_owned = id.clone();
+ let collector = ElementsCollectorWithWindows::new(start_element, move |e| {
+ e.identifier().unwrap_or(CFString::new("")).to_string() == id_owned
+ });
+
+ let ax_ui_elements = collector.find_all();
+
+ // Convert AXUIElements to UIElements
+ let ui_elements = ax_ui_elements
+ .into_iter()
+ .map(|e| self.wrap_element(ThreadSafeAXUIElement::new(e)))
+ .collect();
+
+ Ok(ui_elements)
+ }
+ Selector::Name(name) => {
+ let name_owned = name.clone();
+ let collector = ElementsCollectorWithWindows::new(start_element, move |e| {
+ e.title().unwrap_or(CFString::new("")).to_string() == name_owned
+ });
+
+ let ax_ui_elements = collector.find_all();
+
+ // Convert AXUIElements to UIElements
+ let ui_elements = ax_ui_elements
+ .into_iter()
+ .map(|e| self.wrap_element(ThreadSafeAXUIElement::new(e)))
+ .collect();
+
+ Ok(ui_elements)
+ }
+ Selector::Text(text) => {
+ let text_owned = text.clone();
+ let collector = ElementsCollectorWithWindows::new(start_element, move |e| {
+ element_contains_text(e, &text_owned)
+ });
+
+ let ax_ui_elements = collector.find_all();
+
+ // Convert AXUIElements to UIElements
+ let ui_elements = ax_ui_elements
+ .into_iter()
+ .map(|e| self.wrap_element(ThreadSafeAXUIElement::new(e)))
+ .collect();
+
+ Ok(ui_elements)
+ }
+ Selector::Attributes(_attrs) => Err(AutomationError::UnsupportedOperation(
+ "Attributes selector not implemented for find_elements".to_string(),
+ )),
+ Selector::Path(_) => Err(AutomationError::UnsupportedOperation(
+ "Path selector not implemented for find_elements".to_string(),
+ )),
+ Selector::Filter(_) => Err(AutomationError::UnsupportedOperation(
+ "Filter selector not implemented for find_elements".to_string(),
+ )),
+ Selector::Chain(_) => Err(AutomationError::UnsupportedOperation(
+ "Chain selector not implemented for find_elements".to_string(),
+ )),
+ }
+ }
+}
+
+// Our concrete UIElement implementation for macOS
+pub struct MacOSUIElement {
+ element: ThreadSafeAXUIElement,
+ use_background_apps: bool,
+ activate_app: bool,
+}
+
+impl std::fmt::Debug for MacOSUIElement {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("MacOSUIElement")
+ .field("element", &self.element)
+ .finish()
+ }
+}
+
+impl MacOSUIElement {
+ // Helper function to get the containing application
+ fn get_application(&self) -> Option<MacOSUIElement> {
+ let attr = AXAttribute::new(&CFString::new("AXTopLevelUIElement"));
+ match self.element.0.attribute(&attr) {
+ Ok(value) => {
+ if let Some(app) = value.downcast::<AXUIElement>() {
+ Some(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(app),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ })
+ } else {
+ None
+ }
+ }
+ Err(_) => None,
+ }
+ }
+
+ // Add the extract_web_content_text method here as a regular method
+ fn extract_web_content_text(&self, max_depth: usize) -> Result<String, AutomationError> {
+ // Default to a reasonably high but not infinite depth
+ self.extract_web_content_text_with_depth(max_depth)
+ }
+
+ fn extract_web_content_text_with_depth(
+ &self,
+ max_depth: usize,
+ ) -> Result<String, AutomationError> {
+ let mut all_text = Vec::new();
+ let mut visited = HashSet::new();
+
+ // Simple breadth-first traversal with depth tracking
+ let mut queue = VecDeque::new();
+ queue.push_back((self.clone_box(), 0)); // (element, depth)
+
+ while let Some((element, depth)) = queue.pop_front() {
+ // Check depth limit
+ if depth > max_depth {
+ continue;
+ }
+
+ let elem = element.as_any().downcast_ref::<MacOSUIElement>().unwrap();
+ let id = elem.object_id();
+
+ if visited.contains(&id) {
+ continue;
+ }
+ visited.insert(id);
+
+ // Extract ALL text attributes, no filtering
+ for attr_name in &[
+ "AXValue",
+ "AXTitle",
+ "AXDescription",
+ "AXHelp",
+ "AXLabel",
+ "AXText",
+ ] {
+ let attr = AXAttribute::new(&CFString::new(attr_name));
+ if let Ok(value) = elem.element.0.attribute(&attr) {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ let text = cf_string.to_string();
+ if !text.is_empty() && !all_text.contains(&text) {
+ all_text.push(text);
+ }
+ }
+ }
+ }
+
+ // Get ALL children and add to queue with incremented depth
+ let next_depth = depth + 1;
+
+ // Try navigation order children first
+ let nav_attr = AXAttribute::new(&CFString::new("AXChildrenInNavigationOrder"));
+ if let Ok(value) = elem.element.0.attribute(&nav_attr) {
+ if let Some(array) = extract_children_from_array(value) {
+ for child in array {
+ queue.push_back((
+ Box::new(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(child),
+ use_background_apps: elem.use_background_apps,
+ activate_app: elem.activate_app,
+ }),
+ next_depth,
+ ));
+ }
+ }
+ }
+
+ // Add regular children too to be thorough
+ if let Ok(children) = elem.children() {
+ for child in children {
+ let child_impl = child.as_any().downcast_ref::<MacOSUIElement>().unwrap();
+ queue.push_back((child_impl.clone_box(), next_depth));
+ }
+ }
+ }
+
+ Ok(all_text.join("\n"))
+ }
+
+ fn click_with_applescript(&self) -> Result<(), AutomationError> {
+ // Get element position
+ let (x, y, width, height) = self.bounds()?;
+ let center_x = x + width / 2.0;
+ let center_y = y + height / 2.0;
+
+ // Create AppleScript to click at position
+ let script = format!(
+ "tell application \"System Events\" to click at {{{}, {}}}",
+ center_x as i32, center_y as i32
+ );
+
+ // Execute AppleScript
+ use std::process::Command;
+ let output = Command::new("osascript")
+ .arg("-e")
+ .arg(&script)
+ .output()
+ .map_err(|e| {
+ AutomationError::PlatformError(format!("Failed to execute AppleScript: {}", e))
+ })?;
+
+ if !output.status.success() {
+ let error = String::from_utf8_lossy(&output.stderr);
+ return Err(AutomationError::PlatformError(format!(
+ "AppleScript execution failed: {}",
+ error
+ )));
+ }
+
+ Ok(())
+ }
+}
+
+impl UIElementImpl for MacOSUIElement {
+ fn object_id(&self) -> usize {
+ // Use the pointer address of the inner AXUIElement as a unique ID
+ self.element.0.as_ref() as *const _ as usize
+ }
+
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
+ fn id(&self) -> Option<String> {
+ Some(self.object_id().to_string())
+ }
+
+ fn role(&self) -> String {
+ // Get the actual role
+ let role = self
+ .element
+ .0
+ .role()
+ .map(|r| r.to_string())
+ .unwrap_or_default();
+
+ debug!(target: "operator", "Original role from AXUIElement: {}", role);
+
+ // Map macOS-specific roles to generic roles
+ // TODO: why first? any issue?
+ macos_role_to_generic_role(&role)
+ .first()
+ .unwrap_or(&role)
+ .to_string()
+ }
+
+ fn attributes(&self) -> UIElementAttributes {
+ let properties = HashMap::new();
+
+ // Check if this is a window element first
+ let is_window = self
+ .element
+ .0
+ .role()
+ .map_or(false, |r| r.to_string() == "AXWindow");
+
+ // Special case for windows
+ if is_window {
+ debug!(target: "operator", "Getting attributes for window element");
+
+ let mut attrs = UIElementAttributes {
+ role: "window".to_string(),
+ label: None,
+ value: None,
+ description: None,
+ properties,
+ };
+
+ // Special handling for window title - try multiple attributes
+ let title_attrs = [
+ "AXTitle",
+ "AXTitleUIElement",
+ "AXDocument",
+ "AXFilename",
+ "AXName",
+ ];
+
+ for title_attr_name in title_attrs {
+ let title_attr = AXAttribute::new(&CFString::new(title_attr_name));
+ if let Ok(value) = self.element.0.attribute(&title_attr) {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ attrs.label = Some(cf_string.to_string());
+ debug!(target: "operator", "Found window title via {}: {:?}", title_attr_name, attrs.label);
+ break;
+ }
+ }
+ }
+
+ // Try to get window position and size for debugging
+ let pos_attr = AXAttribute::new(&CFString::new("AXPosition"));
+ if let Ok(_) = self.element.0.attribute(&pos_attr) {
+ debug!(target: "operator", "Window has position attribute");
+ }
+
+ // Try to get standard macOS window attributes
+ let std_attrs = ["AXMinimized", "AXMain", "AXFocused"];
+
+ for attr_name in std_attrs {
+ let attr = AXAttribute::new(&CFString::new(attr_name));
+ if let Ok(value) = self.element.0.attribute(&attr) {
+ if let Some(cf_bool) = value.downcast_into::<CFBoolean>() {
+ attrs.properties.insert(
+ attr_name.to_string(),
+ Some(Value::String(format!("{:?}", cf_bool))),
+ );
+ }
+ }
+ }
+
+ return attrs;
+ }
+
+ // For non-window elements, use standard attribute retrieval
+ let mut attrs = UIElementAttributes {
+ // Use our role() method which handles the mapping of AXMenuItem to button
+ role: self.role(),
+ label: None,
+ value: None,
+ description: None,
+ properties,
+ };
+
+ // Debug attribute collection
+ debug!(target: "operator", "Collecting attributes for element");
+
+ // Directly try common macOS attributes one by one
+ let label_attr = AXAttribute::new(&CFString::new("AXTitle"));
+ match self.element.0.attribute(&label_attr) {
+ Ok(value) => {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ attrs.label = Some(cf_string.to_string());
+ debug!(target: "operator", "Found AXTitle: {:?}", attrs.label);
+ }
+ }
+ Err(e) => {
+ debug!(target: "operator", "Error getting AXTitle: {:?}", e);
+
+ // Fallback to AXLabel if AXTitle fails
+ let alt_label_attr = AXAttribute::new(&CFString::new("AXLabel"));
+ if let Ok(value) = self.element.0.attribute(&alt_label_attr) {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ attrs.label = Some(cf_string.to_string());
+ debug!(target: "operator", "Found AXLabel: {:?}", attrs.label);
+ }
+ }
+ }
+ }
+
+ // Try to get description
+ let desc_attr = AXAttribute::new(&CFString::new("AXDescription"));
+ match self.element.0.attribute(&desc_attr) {
+ Ok(value) => {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ attrs.description = Some(cf_string.to_string());
+ debug!(target: "operator", "Found AXDescription: {:?}", attrs.description);
+ }
+ }
+ Err(e) => {
+ debug!(target: "operator", "Error getting AXDescription: {:?}", e);
+ }
+ }
+
+ // Collect all other attributes
+ if let Ok(attr_names) = self.element.0.attribute_names() {
+ debug!(target: "operator", "Found {} attributes", attr_names.len());
+
+ for name in attr_names.iter() {
+ let attr = AXAttribute::new(&name);
+ match self.element.0.attribute(&attr) {
+ Ok(value) => {
+ let parsed_value = parse_ax_attribute_value(&name.to_string(), value);
+ attrs.properties.insert(name.to_string(), parsed_value);
+ }
+ Err(e) => {
+ // Avoid logging for common expected errors to reduce noise
+ if !matches!(
+ e,
+ accessibility::Error::Ax(-25212)
+ | accessibility::Error::Ax(-25205)
+ | accessibility::Error::Ax(-25204)
+ ) {
+ debug!(target: "operator", "Error getting attribute {:?}: {:?}", name, e);
+ }
+ }
+ }
+ }
+ } else {
+ debug!(target: "operator", "Failed to get attribute names");
+ }
+
+ attrs
+ }
+
+ fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
+ debug!(target: "operator", "Getting children for element: {:?}", self.element.0.role());
+ let mut all_children = Vec::new();
+
+ // First try to get windows
+ if let Ok(windows) = self.element.0.windows() {
+ debug!(target: "operator", "Found {} windows", windows.len());
+
+ // Add all windows to our collection
+ for window in windows.iter() {
+ all_children.push(UIElement::new(Box::new(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(window.clone()),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ })));
+ }
+ }
+ // try main window
+ if let Ok(window) = self.element.0.main_window() {
+ debug!(target: "operator", "Found main window");
+ all_children.push(UIElement::new(Box::new(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(window.clone()),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ })));
+ }
+
+ // Then get regular children
+ match self.element.0.children() {
+ Ok(children) => {
+ // Add regular children to our collection
+ for child in children.iter() {
+ all_children.push(UIElement::new(Box::new(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(child.clone()),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ })));
+ }
+
+ Ok(all_children)
+ }
+ Err(e) => {
+ // If we have windows but failed to get children, return the windows
+ if !all_children.is_empty() {
+ debug!(target: "operator", "Failed to get regular children but returning {} windows", all_children.len());
+ Ok(all_children)
+ } else {
+ // Otherwise return the error
+ Err(AutomationError::PlatformError(format!(
+ "Failed to get children: {}",
+ e
+ )))
+ }
+ }
+ }
+ }
+
+ fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
+ // Get parent of this element
+ let attr = AXAttribute::new(&CFString::new("AXParent"));
+
+ match self.element.0.attribute(&attr) {
+ Ok(value) => {
+ if let Some(parent) = value.downcast::<AXUIElement>() {
+ Ok(Some(UIElement::new(Box::new(MacOSUIElement {
+ element: ThreadSafeAXUIElement::new(parent),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ }))))
+ } else {
+ Ok(None) // No parent
+ }
+ }
+ Err(_) => Ok(None),
+ }
+ }
+
+ fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
+ let mut x = 0.0;
+ let mut y = 0.0;
+ let mut width = 0.0;
+ let mut height = 0.0;
+
+ // Get position
+ if let Ok(position) = self
+ .element
+ .0
+ .attribute(&AXAttribute::new(&CFString::new("AXPosition")))
+ {
+ unsafe {
+ let value_ref = position.as_CFTypeRef();
+
+ // Use AXValueGetValue to extract CGPoint data directly
+ let mut point: CGPoint = CGPoint { x: 0.0, y: 0.0 };
+ let point_ptr = &mut point as *mut CGPoint as *mut ::std::os::raw::c_void;
+
+ if AXValueGetValue(value_ref as *const _, K_AXVALUE_CGPOINT_TYPE, point_ptr) != 0 {
+ x = point.x;
+ y = point.y;
+ }
+ }
+ }
+
+ // Get size
+ if let Ok(size) = self
+ .element
+ .0
+ .attribute(&AXAttribute::new(&CFString::new("AXSize")))
+ {
+ unsafe {
+ let value_ref = size.as_CFTypeRef();
+
+ // Use AXValueGetValue to extract CGSize data directly
+ let mut cg_size: CGSize = CGSize {
+ width: 0.0,
+ height: 0.0,
+ };
+ let size_ptr = &mut cg_size as *mut CGSize as *mut ::std::os::raw::c_void;
+
+ if AXValueGetValue(value_ref as *const _, K_AXVALUE_CGSIZE_TYPE, size_ptr) != 0 {
+ width = cg_size.width;
+ height = cg_size.height;
+ }
+ }
+ }
+
+ debug!(target: "operator", "Element bounds: x={}, y={}, width={}, height={}", x, y, width, height);
+
+ Ok((x, y, width, height))
+ }
+
+ fn click(&self) -> Result<(), AutomationError> {
+ // Try multiple approaches to click the element
+
+ // 1. Try AXPress action first (the original approach)
+ let press_attr = AXAttribute::new(&CFString::new("AXPress"));
+ let press_result = self.element.0.perform_action(&press_attr.as_CFString());
+
+ if press_result.is_ok() {
+ debug!(target: "operator", "Successfully clicked element with AXPress");
+ // return Ok(());
+ } else {
+ debug!(target: "operator", "AXPress failed, trying alternative methods");
+ }
+
+ // 2. Try the more specific AXClick action
+ let click_attr = AXAttribute::new(&CFString::new("AXClick"));
+ let click_result = self.element.0.perform_action(&click_attr.as_CFString());
+
+ if click_result.is_ok() {
+ debug!(target: "operator", "Successfully clicked element with AXClick");
+ // return Ok(());
+ } else {
+ debug!(target: "operator", "AXClick failed, trying alternative methods");
+ }
+
+ // match self.click_with_applescript() {
+ // Ok(_) => {
+ // debug!(target: "operator", "Successfully clicked element with AppleScript");
+ // return Ok(());
+ // }
+ // Err(e) => {
+ // debug!(target: "operator", "AppleScript click failed: {}", e);
+ // }
+ // }
+
+ // 3. If both direct methods failed, try simulating click using mouse events
+ // Only do this as a last resort, as it's more invasive
+ match self.bounds() {
+ Ok((x, y, width, height)) => {
+ // Calculate center point of the element
+ let center_x = x + width / 2.0;
+ let center_y = y + height / 2.0;
+
+ // Use CGEventCreateMouseEvent to simulate mouse click
+ use core_graphics::event::{CGEvent, CGEventType, CGMouseButton};
+ use core_graphics::event_source::CGEventSource;
+ use core_graphics::geometry::CGPoint;
+
+ let point = CGPoint::new(center_x, center_y);
+
+ // Create event source
+ let source = CGEventSource::new(
+ core_graphics::event_source::CGEventSourceStateID::HIDSystemState,
+ )
+ .map_err(|_| {
+ AutomationError::PlatformError("Failed to create event source".to_string())
+ })?;
+
+ // Move mouse to position
+ let mouse_move = CGEvent::new_mouse_event(
+ source.clone(),
+ CGEventType::MouseMoved,
+ point,
+ CGMouseButton::Left,
+ )
+ .map_err(|_| {
+ AutomationError::PlatformError("Failed to create mouse move event".to_string())
+ })?;
+ mouse_move.post(core_graphics::event::CGEventTapLocation::HID);
+
+ // Brief pause to allow UI to respond
+ std::thread::sleep(std::time::Duration::from_millis(50));
+
+ debug!(target: "operator", "Mouse down at ({}, {})", center_x, center_y);
+
+ // Mouse down
+ let mouse_down = CGEvent::new_mouse_event(
+ source.clone(),
+ CGEventType::LeftMouseDown,
+ point,
+ CGMouseButton::Left,
+ )
+ .map_err(|_| {
+ AutomationError::PlatformError("Failed to create mouse down event".to_string())
+ })?;
+ mouse_down.post(core_graphics::event::CGEventTapLocation::HID);
+
+ // Brief pause
+ std::thread::sleep(std::time::Duration::from_millis(50));
+
+ debug!(target: "operator", "Mouse up at ({}, {})", center_x, center_y);
+
+ // Mouse up
+ let mouse_up = CGEvent::new_mouse_event(
+ source,
+ CGEventType::LeftMouseUp,
+ point,
+ CGMouseButton::Left,
+ )
+ .map_err(|_| {
+ AutomationError::PlatformError("Failed to create mouse up event".to_string())
+ })?;
+ mouse_up.post(core_graphics::event::CGEventTapLocation::HID);
+
+ debug!(target: "operator", "Performed simulated mouse click at ({}, {})", center_x, center_y);
+ return Ok(());
+ }
+ Err(e) => {
+ return Err(AutomationError::PlatformError(format!(
+ "Failed to determine element bounds for click: {}",
+ e
+ )));
+ }
+ }
+ }
+
+ fn double_click(&self) -> Result<(), AutomationError> {
+ // Not directly supported, so call click twice
+ self.click()?;
+ self.click()
+ }
+
+ fn right_click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedOperation(
+ "Right-click not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn hover(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedOperation(
+ "Hover not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn focus(&self) -> Result<(), AutomationError> {
+ // Implement proper focus functionality using AXUIElementPerformAction with the "AXRaise" action
+ // or by setting it as the AXFocusedUIElement of its parent window
+
+ // First try using the AXRaise action
+ let raise_attr = AXAttribute::new(&CFString::new("AXRaise"));
+ if let Ok(_) = self.element.0.perform_action(&raise_attr.as_CFString()) {
+ debug!(target: "operator", "Successfully raised element");
+
+ // Now try to directly focus the element
+ // Get the application element
+ if let Some(app) = self.get_application() {
+ // Set the focused element
+ unsafe {
+ let app_ref =
+ app.element.0.as_concrete_TypeRef() as *mut ::std::os::raw::c_void;
+ let attr_str = CFString::new("AXFocusedUIElement");
+ let attr_str_ref =
+ attr_str.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+ let elem_ref =
+ self.element.0.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+
+ let result = AXUIElementSetAttributeValue(app_ref, attr_str_ref, elem_ref);
+ if result == 0 {
+ debug!(target: "operator", "Successfully set focus to element");
+ return Ok(());
+ } else {
+ debug!(
+ target: "operator",
+ "Failed to set element as focused: error code {}", result
+ );
+ }
+ }
+ }
+ }
+
+ // If we can't use AXRaise or set focus directly, try to click the element
+ // which often gives it focus as a side effect
+ debug!(target: "operator", "Attempting to focus by clicking the element");
+ self.click()
+ }
+
+ fn type_text(&self, text: &str) -> Result<(), AutomationError> {
+ // First, try to focus the element, but continue even if focus fails for web inputs
+ match self.focus() {
+ Ok(_) => debug!(target: "operator", "Successfully focused element for typing"),
+ Err(e) => {
+ debug!(target: "operator", "Focus failed, but continuing with type_text: {:?}", e);
+ // Click the element, which is often needed for web inputs
+ if let Err(click_err) = self.click() {
+ debug!(target: "operator", "Click also failed: {:?}", click_err);
+ }
+ }
+ }
+
+ // Check if this is a web input by examining the role
+ let is_web_input = {
+ let role = self.role().to_lowercase();
+ role.contains("web") || role.contains("generic")
+ };
+
+ // For web inputs, we might need a different approach
+ if is_web_input {
+ debug!(target: "operator", "Detected web input, using specialized handling");
+
+ // Try different attribute names that web inputs might use
+ for attr_name in &["AXValue", "AXValueAttribute", "AXText"] {
+ let cf_string = CFString::new(text);
+ unsafe {
+ let element_ref =
+ self.element.0.as_concrete_TypeRef() as *mut ::std::os::raw::c_void;
+ let attr_str = CFString::new(attr_name);
+ let attr_str_ref =
+ attr_str.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+ let value_ref =
+ cf_string.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+
+ let result = AXUIElementSetAttributeValue(element_ref, attr_str_ref, value_ref);
+ if result == 0 {
+ debug!(target: "operator", "Successfully set text using {}", attr_name);
+ return Ok(());
+ }
+ }
+ }
+ }
+
+ // Standard approach for native controls
+ // Create a CFString from the input text
+ let cf_string = CFString::new(text);
+
+ // Set the value of the element using direct AXUIElementSetAttributeValue call
+ unsafe {
+ let element_ref = self.element.0.as_concrete_TypeRef() as *mut ::std::os::raw::c_void;
+ let attr_str = CFString::new("AXValue");
+ let attr_str_ref = attr_str.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+ let value_ref = cf_string.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+
+ let result = AXUIElementSetAttributeValue(element_ref, attr_str_ref, value_ref);
+
+ if result != 0 {
+ debug!(
+ target: "operator",
+ "Failed to set text value via AXValue: error code {}", result
+ );
+
+ return Err(AutomationError::PlatformError(format!(
+ "Failed to set text: error code {}",
+ result
+ )));
+ }
+ }
+
+ Ok(())
+ }
+
+ fn press_key(&self, _key: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedOperation(
+ "press_key not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn get_text(&self, max_depth: usize) -> Result<String, AutomationError> {
+ // Special case for web content - we need a different approach
+ if self
+ .element
+ .0
+ .role()
+ .map_or(false, |r| r.to_string() == "AXWebArea")
+ || self
+ .element
+ .0
+ .role()
+ .map_or(false, |r| r.to_string().contains("Browser"))
+ {
+ debug!(target: "operator", "Using specialized web content extraction for: {:?}",
+ self.element.0.role().map(|r| r.to_string()));
+
+ return self.extract_web_content_text(max_depth);
+ }
+
+ // Regular implementation for non-web elements
+ let mut text_parts = Vec::new();
+
+ // First try getting text from the current element's AXValue
+ let value_attr = AXAttribute::new(&CFString::new("AXValue"));
+ if let Ok(value) = self.element.0.attribute(&value_attr) {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ let val = cf_string.to_string();
+ if !val.is_empty() {
+ text_parts.push(val);
+ }
+ }
+ }
+
+ // Then check for AXDescription and AXTitle
+ let desc_attr = AXAttribute::new(&CFString::new("AXDescription"));
+ if let Ok(desc) = self.element.0.attribute(&desc_attr) {
+ if let Some(cf_string) = desc.downcast_into::<CFString>() {
+ let text = cf_string.to_string();
+ if !text.is_empty() {
+ text_parts.push(text);
+ }
+ }
+ }
+
+ let title_attr = AXAttribute::new(&CFString::new("AXTitle"));
+ if let Ok(title) = self.element.0.attribute(&title_attr) {
+ if let Some(cf_string) = title.downcast_into::<CFString>() {
+ let text = cf_string.to_string();
+ if !text.is_empty() {
+ text_parts.push(text);
+ }
+ }
+ }
+
+ // Get text from children
+ if let Ok(children) = self.children() {
+ for child in children {
+ if let Ok(child_text) = child.text(max_depth) {
+ if !child_text.is_empty() {
+ text_parts.push(child_text);
+ }
+ }
+ }
+ }
+
+ Ok(text_parts.join("\n"))
+ }
+
+ fn set_value(&self, value: &str) -> Result<(), AutomationError> {
+ // This is essentially the same implementation as type_text for macOS,
+ // as both rely on setting the AXValue attribute
+
+ // Create a CFString from the input value
+ let cf_string = CFString::new(value);
+
+ // Set the value of the element using direct AXUIElementSetAttributeValue call
+ unsafe {
+ let element_ref = self.element.0.as_concrete_TypeRef() as *mut ::std::os::raw::c_void;
+ let attr_str = CFString::new("AXValue");
+ let attr_str_ref = attr_str.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+ let value_ref = cf_string.as_concrete_TypeRef() as *const ::std::os::raw::c_void;
+
+ let result = AXUIElementSetAttributeValue(element_ref, attr_str_ref, value_ref);
+
+ if result != 0 {
+ debug!(
+ target: "operator",
+ "Failed to set value via AXValue: error code {}", result
+ );
+
+ return Err(AutomationError::PlatformError(format!(
+ "Failed to set value: error code {}",
+ result
+ )));
+ }
+ }
+
+ Ok(())
+ }
+
+ fn is_enabled(&self) -> Result<bool, AutomationError> {
+ // not implemented
+ Err(AutomationError::UnsupportedOperation(
+ "is_enabled not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn is_visible(&self) -> Result<bool, AutomationError> {
+ // There's no direct "visible" attribute, but we can approximate with bounds
+ match self.bounds() {
+ Ok((_, _, width, height)) => {
+ // If element has non-zero size, it's probably visible
+ Ok(width > 0.0 && height > 0.0)
+ }
+ Err(_) => {
+ // If we can't get bounds, assume it's not visible
+ Ok(false)
+ }
+ }
+ }
+
+ fn is_focused(&self) -> Result<bool, AutomationError> {
+ // not implemented
+ Err(AutomationError::UnsupportedOperation(
+ "is_focused not yet implemented for macOS".to_string(),
+ ))
+ }
+
+ fn perform_action(&self, action: &str) -> Result<(), AutomationError> {
+ // Perform a named action
+ let action_attr = AXAttribute::new(&CFString::new(action));
+
+ self.element
+ .0
+ .perform_action(&action_attr.as_CFString())
+ .map_err(|e| {
+ AutomationError::PlatformError(format!(
+ "Failed to perform action {}: {}",
+ action, e
+ ))
+ })
+ }
+
+ fn create_locator(&self, selector: Selector) -> Result<Locator, AutomationError> {
+ // Get the platform-specific instance of the engine
+ let engine = MacOSEngine::new(self.use_background_apps, self.activate_app)?;
+
+ // If this is an application element, refresh the tree
+ if self
+ .element
+ .0
+ .role()
+ .map_or(false, |r| r.to_string() == "AXApplication")
+ {
+ if let Some(app_name) = self.attributes().label {
+ engine.refresh_accessibility_tree(Some(&app_name))?;
+ }
+ }
+
+ // Add some debug output to understand the current element
+ let attrs = self.attributes();
+ debug!(target: "operator", "Creating locator for element: role={}, label={:?}", attrs.role, attrs.label);
+
+ // Create a new locator with this element as root
+ let self_element = UIElement::new(Box::new(MacOSUIElement {
+ element: self.element.clone(),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ }));
+
+ // Create a locator for the selector with the engine, then set root to this element
+ let locator = Locator::new(std::sync::Arc::new(engine), selector).within(self_element);
+
+ Ok(locator)
+ }
+
+ fn clone_box(&self) -> Box<dyn UIElementImpl> {
+ Box::new(MacOSUIElement {
+ element: self.element.clone(),
+ use_background_apps: self.use_background_apps,
+ activate_app: self.activate_app,
+ })
+ }
+}
+
+// Helper function to extract children from an array attribute
+fn extract_children_from_array(value: core_foundation::base::CFType) -> Option<Vec<AXUIElement>> {
+ use core_foundation::array::{CFArrayGetCount, CFArrayGetTypeID, CFArrayGetValueAtIndex};
+ use core_foundation::base::{CFGetTypeID, TCFType};
+
+ unsafe {
+ let value_ref = value.as_CFTypeRef();
+ let type_id = CFGetTypeID(value_ref);
+
+ if type_id == CFArrayGetTypeID() {
+ let array_ref = value_ref as *const __CFArray;
+ let count = CFArrayGetCount(array_ref);
+
+ let mut children = Vec::with_capacity(count as usize);
+ for i in 0..count {
+ let item = CFArrayGetValueAtIndex(array_ref, i as isize);
+ if !item.is_null() {
+ let ax_element = AXUIElement::wrap_under_get_rule(item as *mut _);
+ children.push(ax_element);
+ }
+ }
+ return Some(children);
+ }
+ }
+
+ None
+}
+
+// Helper function to parse AXUIElement attribute values into appropriate types
+fn parse_ax_attribute_value(
+ name: &str,
+ value: core_foundation::base::CFType,
+) -> Option<serde_json::Value> {
+ use core_foundation::base::TCFType;
+ use core_foundation::boolean::CFBoolean;
+ use core_foundation::number::CFNumber;
+ use core_foundation::string::CFString;
+ use core_graphics::geometry::{CGPoint, CGSize};
+ use serde_json::{json, Value};
+
+ // Handle different types based on known attribute names and value types
+ match name {
+ // String values (text, identifiers, descriptions)
+ "AXRole" | "AXRoleDescription" | "AXIdentifier" | "AXValue" => {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ return Some(Value::String(cf_string.to_string()));
+ }
+ }
+
+ // Boolean values
+ "AXEnabled" | "AXFocused" => {
+ if let Some(cf_bool) = value.downcast_into::<CFBoolean>() {
+ return Some(Value::Bool(cf_bool == CFBoolean::true_value()));
+ }
+ }
+
+ // Numeric values
+ "AXNumberOfCharacters" | "AXInsertionPointLineNumber" => {
+ if let Some(cf_num) = value.downcast_into::<CFNumber>() {
+ if let Some(num) = cf_num.to_i64() {
+ return Some(Value::Number(serde_json::Number::from(num)));
+ } else if let Some(num) = cf_num.to_f64() {
+ // Need to handle possible NaN/Infinity which aren't allowed in JSON
+ if num.is_finite() {
+ return serde_json::Number::from_f64(num).map(Value::Number);
+ } else {
+ return Some(Value::Null);
+ }
+ }
+ }
+ }
+
+ // Position, Size and Frame require special handling with AXValue
+ "AXPosition" => {
+ // Try to extract CGPoint using AXValueGetValue
+ unsafe {
+ let value_ref = value.as_CFTypeRef();
+ let mut point = CGPoint { x: 0.0, y: 0.0 };
+ let point_ptr = &mut point as *mut CGPoint as *mut ::std::os::raw::c_void;
+
+ if AXValueGetValue(value_ref, K_AXVALUE_CGPOINT_TYPE, point_ptr) != 0 {
+ return Some(json!({
+ "x": point.x,
+ "y": point.y
+ }));
+ }
+ }
+ }
+
+ "AXSize" => {
+ // Try to extract CGSize using AXValueGetValue
+ unsafe {
+ let value_ref = value.as_CFTypeRef();
+ let mut size = CGSize {
+ width: 0.0,
+ height: 0.0,
+ };
+ let size_ptr = &mut size as *mut CGSize as *mut ::std::os::raw::c_void;
+
+ if AXValueGetValue(value_ref, K_AXVALUE_CGSIZE_TYPE, size_ptr) != 0 {
+ return Some(json!({
+ "width": size.width,
+ "height": size.height
+ }));
+ }
+ }
+ }
+
+ // For attributes that are references to other UI elements
+ "AXParent" | "AXWindow" | "AXTopLevelUIElement" => {
+ // get object id
+ if let Some(ax_element) = value.downcast_into::<AXUIElement>() {
+ let address = &ax_element as *const _ as usize;
+ return Some(Value::String(format!("{}", address)));
+ }
+ }
+
+ // For array types (children)
+ name if name.starts_with("AXChildren") => {
+ debug!(target: "operator", "Processing AXChildren attribute");
+
+ unsafe {
+ let value_ref = value.as_CFTypeRef();
+ let type_id = CFGetTypeID(value_ref);
+
+ if type_id == CFArrayGetTypeID() {
+ // Cast to CFArrayRef
+ let array_ref = value_ref as *const __CFArray;
+ let count = CFArrayGetCount(array_ref);
+ debug!(target: "operator", "AXChildren array with {} elements", count);
+
+ // Create an array of element addresses
+ let mut items = Vec::with_capacity(count as usize);
+ for i in 0..count {
+ let item = CFArrayGetValueAtIndex(array_ref, i as isize);
+ if !item.is_null() {
+ // Correctly wrap the raw pointer into AXUIElement
+ let ax_element = AXUIElement::wrap_under_get_rule(item as *mut _);
+ let address = &ax_element as *const _ as usize;
+ items.push(json!(format!("{}", address)));
+ }
+ }
+ return Some(Value::Array(items));
+ }
+ }
+
+ return None;
+ }
+
+ _ => {}
+ }
+
+ // Fallback for unhandled types
+ None
+}
+
+// Add this helper function after the selector handler
+fn element_contains_text(e: &AXUIElement, text: &str) -> bool {
+ // Check immediate element attributes for text
+ let contains_in_value = e
+ .value()
+ .ok()
+ .and_then(|v| v.downcast_into::<CFString>())
+ .map_or(false, |s| s.to_string().contains(text));
+
+ if contains_in_value {
+ return true;
+ }
+
+ // Check title, description and other text attributes
+ let contains_in_title = e
+ .title()
+ .ok()
+ .map_or(false, |t| t.to_string().contains(text));
+
+ let contains_in_desc = e
+ .description()
+ .ok()
+ .map_or(false, |d| d.to_string().contains(text));
+
+ // Check common text attributes
+ for attr_name in &[
+ "AXValue",
+ "AXTitle",
+ "AXDescription",
+ "AXHelp",
+ "AXLabel",
+ "AXText",
+ ] {
+ let attr = AXAttribute::new(&CFString::new(attr_name));
+ if let Ok(value) = e.attribute(&attr) {
+ if let Some(cf_string) = value.downcast_into::<CFString>() {
+ if cf_string.to_string().contains(text) {
+ return true;
+ }
+ }
+ }
+ }
+
+ contains_in_title || contains_in_desc
+}
diff --git a/screenpipe-core/src/operator/platforms/mod.rs b/screenpipe-core/src/operator/platforms/mod.rs
new file mode 100644
index 000000000..8c416479a
--- /dev/null
+++ b/screenpipe-core/src/operator/platforms/mod.rs
@@ -0,0 +1,73 @@
+use crate::operator::{AutomationError, Selector, UIElement};
+
+/// The common trait that all platform-specific engines must implement
+pub trait AccessibilityEngine: Send + Sync {
+ /// Get the root UI element
+ fn get_root_element(&self) -> UIElement;
+
+ /// Get the currently focused element
+ fn get_focused_element(&self) -> Result<UIElement, AutomationError>;
+
+ /// Get all running applications
+ fn get_applications(&self) -> Result<Vec<UIElement>, AutomationError>;
+
+ /// Get application by name
+ fn get_application_by_name(&self, name: &str) -> Result<UIElement, AutomationError>;
+
+ /// Find elements using a selector
+ fn find_element(
+ &self,
+ selector: &Selector,
+ root: Option<&UIElement>,
+ ) -> Result<UIElement, AutomationError>;
+
+ /// Find all elements matching a selector
+ /// Default implementation returns an UnsupportedOperation error,
+ /// allowing platform-specific implementations to override as needed
+ fn find_elements(
+ &self,
+ selector: &Selector,
+ root: Option<&UIElement>,
+ ) -> Result<Vec<UIElement>, AutomationError>;
+}
+
+#[cfg(target_os = "linux")]
+mod linux;
+#[cfg(target_os = "macos")]
+mod macos;
+#[cfg(target_os = "windows")]
+mod windows;
+
+/// Create the appropriate engine for the current platform
+pub fn create_engine(
+ use_background_apps: bool,
+ activate_app: bool,
+) -> Result<Box<dyn AccessibilityEngine>, AutomationError> {
+ #[cfg(target_os = "macos")]
+ {
+ return Ok(Box::new(macos::MacOSEngine::new(
+ use_background_apps,
+ activate_app,
+ )?));
+ }
+ #[cfg(target_os = "windows")]
+ {
+ return Ok(Box::new(windows::WindowsEngine::new(
+ use_background_apps,
+ activate_app,
+ )?));
+ }
+ #[cfg(target_os = "linux")]
+ {
+ return Ok(Box::new(linux::LinuxEngine::new(
+ use_background_apps,
+ activate_app,
+ )?));
+ }
+ #[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
+ {
+ return Err(AutomationError::UnsupportedPlatform(
+ "Current platform is not supported".to_string(),
+ ));
+ }
+}
diff --git a/screenpipe-core/src/operator/platforms/windows.rs b/screenpipe-core/src/operator/platforms/windows.rs
new file mode 100644
index 000000000..f7bf91dbb
--- /dev/null
+++ b/screenpipe-core/src/operator/platforms/windows.rs
@@ -0,0 +1,197 @@
+use crate::operator::element::UIElementImpl;
+use crate::operator::platforms::AccessibilityEngine;
+use crate::operator::{AutomationError, Locator, Selector, UIElement, UIElementAttributes};
+use std::fmt::Debug;
+
+pub struct WindowsEngine;
+
+impl WindowsEngine {
+ pub fn new() -> Result<Self, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+}
+
+impl AccessibilityEngine for WindowsEngine {
+ fn get_root_element(&self) -> UIElement {
+ panic!("Windows implementation is not yet available")
+ }
+
+ fn get_element_by_id(&self, _id: &str) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_focused_element(&self) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_applications(&self) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_application_by_name(&self, _name: &str) -> Result<UIElement, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn find_elements(
+ &self,
+ _selector: &Selector,
+ _root: Option<&UIElement>,
+ ) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+}
+
+// Placeholder WindowsUIElement that implements UIElementImpl
+pub struct WindowsUIElement;
+
+impl Debug for WindowsUIElement {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("WindowsUIElement").finish()
+ }
+}
+
+impl UIElementImpl for WindowsUIElement {
+ fn object_id(&self) -> usize {
+ 0
+ }
+
+ fn id(&self) -> Option<String> {
+ None
+ }
+
+ fn role(&self) -> String {
+ "".to_string()
+ }
+
+ fn attributes(&self) -> UIElementAttributes {
+ UIElementAttributes {
+ role: "".to_string(),
+ label: None,
+ value: None,
+ description: None,
+ properties: std::collections::HashMap::new(),
+ }
+ }
+
+ fn children(&self) -> Result<Vec<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn parent(&self) -> Result<Option<UIElement>, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn bounds(&self) -> Result<(f64, f64, f64, f64), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn double_click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn right_click(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn hover(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn focus(&self) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn type_text(&self, _text: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn press_key(&self, _key: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn get_text(&self) -> Result<String, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn set_value(&self, _value: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_enabled(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_visible(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn is_focused(&self) -> Result<bool, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn perform_action(&self, _action: &str) -> Result<(), AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn as_any(&self) -> &dyn std::any::Any {
+ self
+ }
+
+ fn create_locator(&self, _selector: Selector) -> Result<Locator, AutomationError> {
+ Err(AutomationError::UnsupportedPlatform(
+ "Windows implementation is not yet available".to_string(),
+ ))
+ }
+
+ fn clone_box(&self) -> Box<dyn UIElementImpl> {
+ Box::new(WindowsUIElement)
+ }
+}
diff --git a/screenpipe-core/src/operator/selector.rs b/screenpipe-core/src/operator/selector.rs
new file mode 100644
index 000000000..569328278
--- /dev/null
+++ b/screenpipe-core/src/operator/selector.rs
@@ -0,0 +1,52 @@
+use std::collections::BTreeMap;
+
+/// Represents ways to locate a UI element
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum Selector {
+ /// Select by role and optional name
+ Role { role: String, name: Option<String> },
+ /// Select by accessibility ID
+ Id(String),
+ /// Select by name/label
+ Name(String),
+ /// Select by text content
+ Text(String),
+ /// Select using XPath-like query
+ Path(String),
+ /// Select by multiple attributes (key-value pairs)
+ Attributes(BTreeMap<String, String>),
+ /// Filter current elements by a predicate
+ Filter(usize), // Uses an ID to reference a filter predicate stored separately
+ /// Chain multiple selectors
+ Chain(Vec<Selector>),
+}
+
+impl From<&str> for Selector {
+ fn from(s: &str) -> Self {
+ // Make common UI roles like "window", "button", etc. default to Role selectors
+ // instead of Name selectors
+ match s {
+ "window" | "button" | "checkbox" | "menu" | "menuitem" | "menubar" | "textfield"
+ | "input" => Selector::Role {
+ role: s.to_string(),
+ name: None,
+ },
+ // starts with AX
+ _ if s.starts_with("AX") => Selector::Role {
+ role: s.to_string(),
+ name: None,
+ },
+ _ if s.contains(':') => {
+ let parts: Vec<&str> = s.splitn(2, ':').collect();
+ Selector::Role {
+ role: parts[0].to_string(),
+ name: Some(parts[1].to_string()),
+ }
+ }
+ _ if s.starts_with('#') => Selector::Id(s[1..].to_string()),
+ _ if s.starts_with('/') => Selector::Path(s.to_string()),
+ _ if s.starts_with("text:") => Selector::Text(s[5..].to_string()),
+ _ => Selector::Name(s.to_string()),
+ }
+ }
+}
diff --git a/screenpipe-core/src/operator/tests.rs b/screenpipe-core/src/operator/tests.rs
new file mode 100644
index 000000000..62d52ee08
--- /dev/null
+++ b/screenpipe-core/src/operator/tests.rs
@@ -0,0 +1,257 @@
+use tracing_subscriber::prelude::*;
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use tracing_subscriber::{filter::LevelFilter, fmt, EnvFilter};
+
+ #[cfg(target_os = "macos")]
+ mod macos_tests {
+ use serde_json::Value;
+
+ use crate::{Desktop, Selector};
+
+ use super::*;
+
+ // Setup tracing for tests
+ fn setup_tracing() {
+ let filter = EnvFilter::from_default_env()
+ .add_directive(LevelFilter::DEBUG.into())
+ .add_directive("operator=debug".parse().unwrap());
+
+ tracing_subscriber::registry()
+ .with(fmt::layer())
+ .with(filter)
+ .init();
+ }
+
+ #[test]
+ fn test_find_buttons_in_iphone_mirroring() {
+ setup_tracing();
+
+ // Create a desktop automation instance
+ let desktop = match Desktop::new(true, false) {
+ Ok(d) => {
+ println!("Successfully created Desktop automation");
+ d
+ }
+ Err(e) => {
+ println!("Failed to create Desktop automation: {:?}", e);
+ return;
+ }
+ };
+
+ let app = match desktop.application("Cursor") {
+ Ok(w) => w,
+ Err(e) => {
+ println!("Failed to find application: {:?}", e);
+ return;
+ }
+ };
+ println!("App: {:?}", app.attributes().label);
+
+ let windows = app.locator("window").unwrap().all().unwrap_or_default();
+ println!("Found {} windows", windows.len());
+
+ // Print the window hierarchy to understand the structure
+ println!("\n===== WINDOW HIERARCHY =====");
+ if let Ok(children) = app.children() {
+ println!("App has {} direct children", children.len());
+ for (i, child) in children.iter().enumerate() {
+ println!(
+ "Child #{}: role={}, label={:?}, description={:?}",
+ i,
+ child.role(),
+ child.attributes().label,
+ child.attributes().description
+ );
+
+ // Print the next level down to see buttons
+ if let Ok(grandchildren) = child.children() {
+ println!(" Has {} children", grandchildren.len());
+ for (j, grandchild) in grandchildren.iter().enumerate() {
+ println!(
+ " Grandchild #{}.{}: role={}, label={:?}, description={:?}",
+ i,
+ j,
+ grandchild.role(),
+ grandchild.attributes().label,
+ grandchild.attributes().description
+ );
+
+ // Try one more level
+ if let Ok(great_grandchildren) = grandchild.children() {
+ println!(" Has {} children", great_grandchildren.len());
+ for (k, ggc) in great_grandchildren.iter().take(5).enumerate() {
+ println!(
+ " Great-grandchild #{}.{}.{}: role={}, label={:?}",
+ i,
+ j,
+ k,
+ ggc.role(),
+ ggc.attributes().label
+ );
+ }
+ if great_grandchildren.len() > 5 {
+ println!(" ... and {} more", great_grandchildren.len() - 5);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Find buttons in the application window
+ println!("\n===== BUTTON SEARCH RESULTS =====");
+ let buttons = match app.locator("button") {
+ Ok(locator) => locator.all().unwrap_or_default(),
+ Err(_) => Vec::new(),
+ };
+ println!("Found {} buttons via locator API", buttons.len());
+
+ // Print details about each button by type
+ let mut ax_button_count = 0;
+ let mut ax_menu_item_count = 0;
+ let mut ax_menu_bar_item_count = 0;
+ let mut ax_static_text_count = 0;
+ let mut ax_image_count = 0;
+ let mut other_count = 0;
+
+ for (i, button) in buttons.iter().enumerate() {
+ let button_type = if let Some(props) = button.attributes().properties.get("AXRole")
+ {
+ let props_str = props.clone();
+ props_str.unwrap_or_default()
+ } else {
+ Value::String("unknown".to_string())
+ };
+
+ println!(
+ "Button #{}: type={}, role={}, label={:?}, description={:?}",
+ i,
+ button_type,
+ button.role(),
+ button.attributes().label,
+ button.attributes().description
+ );
+
+ // if description is "Rust" then click it
+ if button.attributes().description == Some("Rust".to_string()) {
+ match button.click() {
+ Ok(_) => println!("Clicked button: {:?}", button.attributes().label),
+ Err(e) => println!("Failed to click button: {:?}", e),
+ }
+ }
+
+ // Count by type
+ match button_type.as_str() {
+ Some("AXButton") => ax_button_count += 1,
+ Some("AXMenuItem") => ax_menu_item_count += 1,
+ Some("AXMenuBarItem") => ax_menu_bar_item_count += 1,
+ Some("AXStaticText") => ax_static_text_count += 1,
+ Some("AXImage") => ax_image_count += 1,
+ _ => other_count += 1,
+ }
+ }
+
+ // Print summary of button types
+ println!("\n===== BUTTON TYPE SUMMARY =====");
+ println!("AXButton: {}", ax_button_count);
+ println!("AXMenuItem: {}", ax_menu_item_count);
+ println!("AXMenuBarItem: {}", ax_menu_bar_item_count);
+ println!("AXStaticText: {}", ax_static_text_count);
+ println!("AXImage: {}", ax_image_count);
+ println!("Other: {}", other_count);
+ println!("Total: {}", buttons.len());
+
+ // Make sure we found at least some buttons
+ assert!(buttons.len() > 0, "No buttons found in iPhone Mirroring");
+
+ // Check that we found the standard menu bar items
+ assert_eq!(
+ ax_menu_bar_item_count, 6,
+ "Should find exactly 6 menu bar items"
+ );
+ }
+
+ #[test]
+ fn test_find_and_fill_text_inputs() {
+ setup_tracing();
+
+ // Create a desktop automation instance
+ let desktop = match Desktop::new(true, false) {
+ Ok(d) => {
+ println!("Successfully created Desktop automation");
+ d
+ }
+ Err(e) => {
+ println!("Failed to create Desktop automation: {:?}", e);
+ return;
+ }
+ };
+
+ let app = desktop.application("Arc").unwrap();
+
+ let children = app.children().unwrap();
+
+ println!("App children: {:?}", children.len());
+
+ for (i, child) in children.iter().enumerate() {
+ println!("App child #{}: {:?}", i, child.role());
+ }
+
+ let input = app.locator("window").unwrap().first().unwrap_or_default();
+ println!("found input: {:?}", input.is_some());
+ println!("found input: {:?}", input.unwrap().text(10).unwrap());
+ }
+
+ #[test]
+ fn test_find_and_fill_text_inputsv2() {
+ setup_tracing();
+
+ // Create a desktop automation instance
+ let desktop = match Desktop::new(true, true) {
+ Ok(d) => {
+ println!("Successfully created Desktop automation");
+ d
+ }
+ Err(e) => {
+ println!("Failed to create Desktop automation: {:?}", e);
+ return;
+ }
+ };
+
+ let app = desktop.application("Arc").unwrap();
+
+ let children = app.children().unwrap();
+
+ println!("App children: {:?}", children.len());
+
+ for (i, child) in children.iter().enumerate() {
+ println!("App child #{}: {:?}", i, child.role());
+ }
+
+ let buttons = app.locator("AXButton").unwrap().all().unwrap_or_default();
+ for b in buttons {
+ println!("b: {:?}", b.role());
+ println!("b: {:?}", b.attributes().label);
+ let text = b.text(4).unwrap_or_default();
+ println!("b: {:?}", text);
+ if text.contains("Click") {
+ println!("clicking");
+ b.type_text("foo");
+ b.focus().unwrap();
+ if let Err(e) = b.click() {
+ println!("failed to click: {:?}", e);
+ }
+ }
+ }
+ // input.focus().err().unwrap();
+ // let text = input.text(10).unwrap();
+ // println!("text: {:?}", text);
+
+ // let children = input.children().unwrap();
+ // println!("children: {:?}", children.len());
+ }
+ }
+}
diff --git a/screenpipe-core/src/operator/tree_search.rs b/screenpipe-core/src/operator/tree_search.rs
new file mode 100644
index 000000000..26d30f8b1
--- /dev/null
+++ b/screenpipe-core/src/operator/tree_search.rs
@@ -0,0 +1,215 @@
+/// TLDR: default TreeWalker does not traverse windows, so we need to traverse windows manually
+use accessibility::{AXAttribute, AXUIElement, AXUIElementAttributes, Error};
+use core_foundation::{array::CFArray, string::CFString};
+use std::{
+ cell::{Cell, RefCell},
+ thread,
+ time::{Duration, Instant},
+};
+use tracing::debug;
+
+pub trait TreeVisitor {
+ fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow;
+ fn exit_element(&self, element: &AXUIElement);
+}
+
+pub struct TreeWalkerWithWindows {
+ attr_children: AXAttribute<CFArray<AXUIElement>>,
+}
+
+#[derive(Copy, Clone, PartialEq, Eq)]
+pub enum TreeWalkerFlow {
+ Continue,
+ SkipSubtree,
+ Exit,
+}
+
+impl Default for TreeWalkerWithWindows {
+ fn default() -> Self {
+ Self {
+ attr_children: AXAttribute::children(),
+ }
+ }
+}
+
+impl TreeWalkerWithWindows {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ pub fn walk(&self, root: &AXUIElement, visitor: &dyn TreeVisitor) {
+ let _ = self.walk_one(root, visitor);
+ }
+
+ fn walk_one(&self, root: &AXUIElement, visitor: &dyn TreeVisitor) -> TreeWalkerFlow {
+ let mut flow = visitor.enter_element(root);
+
+ debug!(target: "operator", "Walking element: {:?}", root.role());
+
+ if flow == TreeWalkerFlow::Continue {
+ // First try to get windows (if this is an application element)
+ let windows_result = root.windows();
+ if let Ok(windows) = &windows_result {
+ for window in windows.iter() {
+ debug!(target: "operator", "Walking window: {:?}", window.title());
+ let window_flow = self.walk_one(&window, visitor);
+ if window_flow == TreeWalkerFlow::Exit {
+ flow = window_flow;
+ break;
+ }
+ }
+ }
+
+ // TODO avoid duplicate main window walking
+ // Try main window
+ if flow != TreeWalkerFlow::Exit {
+ if let Ok(main_window) = root.main_window() {
+ debug!(target: "operator", "Walking main window: {:?}", main_window.title());
+ let window_flow = self.walk_one(&main_window, visitor);
+ if window_flow == TreeWalkerFlow::Exit {
+ flow = window_flow;
+ }
+ }
+ }
+
+ // If we haven't exited yet, continue with regular children
+ if flow == TreeWalkerFlow::Continue {
+ if let Ok(children) = root.attribute(&self.attr_children) {
+ for child in children.into_iter() {
+ let child_flow = self.walk_one(&child, visitor);
+
+ if child_flow == TreeWalkerFlow::Exit {
+ flow = child_flow;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ visitor.exit_element(root);
+ flow
+ }
+}
+
+pub struct ElementFinderWithWindows {
+ root: AXUIElement,
+ implicit_wait: Option<Duration>,
+ predicate: Box<dyn Fn(&AXUIElement) -> bool>,
+ depth: Cell<usize>,
+ cached: RefCell<Option<AXUIElement>>,
+}
+
+impl ElementFinderWithWindows {
+ pub fn new<F>(root: &AXUIElement, predicate: F, implicit_wait: Option<Duration>) -> Self
+ where
+ F: 'static + Fn(&AXUIElement) -> bool,
+ {
+ Self {
+ root: root.clone(),
+ predicate: Box::new(predicate),
+ implicit_wait,
+ depth: Cell::new(0),
+ cached: RefCell::new(None),
+ }
+ }
+
+ pub fn find(&self) -> Result<AXUIElement, Error> {
+ if let Some(result) = &*self.cached.borrow() {
+ return Ok(result.clone());
+ }
+
+ let mut deadline = Instant::now();
+ let walker = TreeWalkerWithWindows::new();
+
+ if let Some(implicit_wait) = &self.implicit_wait {
+ deadline += *implicit_wait;
+ }
+
+ loop {
+ if let Some(result) = &*self.cached.borrow() {
+ return Ok(result.clone());
+ }
+
+ walker.walk(&self.root, self);
+ let now = Instant::now();
+
+ if now >= deadline {
+ return Err(Error::NotFound);
+ } else {
+ let time_left = deadline.saturating_duration_since(now);
+ thread::sleep(std::cmp::min(time_left, Duration::from_millis(250)));
+ }
+ }
+ }
+}
+
+const MAX_DEPTH: usize = 100;
+
+impl TreeVisitor for ElementFinderWithWindows {
+ fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow {
+ self.depth.set(self.depth.get() + 1);
+
+ if (self.predicate)(element) {
+ self.cached.replace(Some(element.clone()));
+ return TreeWalkerFlow::Exit;
+ }
+
+ if self.depth.get() > MAX_DEPTH {
+ TreeWalkerFlow::SkipSubtree
+ } else {
+ TreeWalkerFlow::Continue
+ }
+ }
+
+ fn exit_element(&self, _element: &AXUIElement) {
+ self.depth.set(self.depth.get() - 1)
+ }
+}
+
+pub struct ElementsCollectorWithWindows {
+ root: AXUIElement,
+ predicate: Box<dyn Fn(&AXUIElement) -> bool>,
+ depth: Cell<usize>,
+ matches: RefCell<Vec<AXUIElement>>,
+}
+
+impl ElementsCollectorWithWindows {
+ pub fn new<F>(root: &AXUIElement, predicate: F) -> Self
+ where
+ F: 'static + Fn(&AXUIElement) -> bool,
+ {
+ Self {
+ root: root.clone(),
+ predicate: Box::new(predicate),
+ depth: Cell::new(0),
+ matches: RefCell::new(Vec::new()),
+ }
+ }
+
+ pub fn find_all(&self) -> Vec<AXUIElement> {
+ let walker = TreeWalkerWithWindows::new();
+ walker.walk(&self.root, self);
+ self.matches.borrow().clone()
+ }
+}
+
+impl TreeVisitor for ElementsCollectorWithWindows {
+ fn enter_element(&self, element: &AXUIElement) -> TreeWalkerFlow {
+ self.depth.set(self.depth.get() + 1);
+
+ if (self.predicate)(element) {
+ self.matches.borrow_mut().push(element.clone());
+ }
+
+ if self.depth.get() > MAX_DEPTH {
+ TreeWalkerFlow::SkipSubtree
+ } else {
+ TreeWalkerFlow::Continue
+ }
+ }
+
+ fn exit_element(&self, _element: &AXUIElement) {
+ self.depth.set(self.depth.get() - 1)
+ }
+}
diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rs
index 020baa2f3..a4763b34c 100644
--- a/screenpipe-server/src/server.rs
+++ b/screenpipe-server/src/server.rs
@@ -11,10 +11,14 @@ use axum::{
};
use oasgen::{oasgen, OaSchema, Server};
+
+use screenpipe_core::Desktop;
+
use screenpipe_db::{
ContentType, DatabaseManager, FrameData, Order, SearchMatch, SearchResult, Speaker,
TagContentType,
};
+
use tokio_util::io::ReaderStream;
use tokio::fs::File;
@@ -1054,6 +1058,9 @@ impl SCServer {
.get("/speakers/similar", get_similar_speakers_handler)
.post("/experimental/frames/merge", merge_frames_handler)
.get("/experimental/validate/media", validate_media_handler)
+ .post("/experimental/operator", find_elements_handler)
+ .post("/experimental/operator/click", click_element_handler)
+ .post("/experimental/operator/type", type_text_handler)
.post("/audio/start", start_audio)
.post("/audio/stop", stop_audio)
.get("/semantic-search", semantic_search_handler)
@@ -3022,3 +3029,326 @@ pub struct RestartVisionDevicesResponse {
// restarted_devices,
// }))
// }
+
+// New structs for UI automation API
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ElementSelector {
+ app_name: String,
+ window_name: Option<String>,
+ locator: String,
+ index: Option<usize>,
+ text: Option<String>,
+ label: Option<String>,
+ description: Option<String>,
+ element_id: Option<String>,
+ use_background_apps: Option<bool>,
+ /// If true, the app will be activated before finding elements (this is useful to refresh the tree or clicking on elements)
+ activate_app: Option<bool>,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ClickElementRequest {
+ selector: ElementSelector,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct TypeTextRequest {
+ selector: ElementSelector,
+ text: String,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct FindElementsRequest {
+ selector: ElementSelector,
+ max_results: Option<usize>,
+ max_depth: Option<usize>,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ElementPosition {
+ x: i32,
+ y: i32,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ElementSize {
+ width: i32,
+ height: i32,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ElementInfo {
+ id: Option<String>,
+ role: String,
+ label: Option<String>,
+ description: Option<String>,
+ text: Option<String>,
+ position: Option<ElementPosition>,
+ size: Option<ElementSize>,
+ properties: serde_json::Value,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct FindElementsResponse {
+ element: ElementInfo,
+}
+
+#[derive(Debug, OaSchema, Deserialize, Serialize)]
+pub struct ActionResponse {
+ success: bool,
+ message: String,
+}
+
+// Handler functions for UI automation
+#[oasgen]
+async fn find_elements_handler(
+ State(_): State<Arc<AppState>>,
+ Json(request): Json<FindElementsRequest>,
+) -> Result<JsonResponse<FindElementsResponse>, (StatusCode, JsonResponse<Value>)> {
+ let desktop = match Desktop::new(
+ request.selector.use_background_apps.unwrap_or(false),
+ request.selector.activate_app.unwrap_or(false),
+ ) {
+ Ok(d) => d,
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to initialize desktop automation: {}", e)
+ })),
+ ));
+ }
+ };
+
+ let app = match desktop.application(&request.selector.app_name) {
+ Ok(app) => app,
+ Err(e) => {
+ return Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({
+ "error": format!("Application not found: {}", e)
+ })),
+ ));
+ }
+ };
+
+ debug!("app: {:?}", app.text(1).unwrap_or_default());
+
+ let element = match app.locator(request.selector.locator.as_str()) {
+ Ok(locator) => match locator.first() {
+ Ok(element) => element,
+ Err(_) => {
+ return Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({ "error": "No matching element found" })),
+ ));
+ }
+ },
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({ "error": format!("Failed to create locator: {}", e) })),
+ ));
+ }
+ };
+
+ match element {
+ Some(element) => {
+ debug!("element: {:?}", element);
+ // Convert to ElementInfo
+ let element_info = ElementInfo {
+ id: element.id(),
+ role: element.role(),
+ label: element.attributes().label,
+ description: element.attributes().description,
+ text: element.text(request.max_depth.unwrap_or(10)).ok(),
+ position: element.bounds().ok().map(|(x, y, _, _)| ElementPosition {
+ x: x as i32,
+ y: y as i32,
+ }),
+ size: element.bounds().ok().map(|(_, _, w, h)| ElementSize {
+ width: w as i32,
+ height: h as i32,
+ }),
+ properties: json!(element.attributes().properties),
+ };
+
+ Ok(JsonResponse(FindElementsResponse {
+ element: element_info,
+ }))
+ }
+ None => Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({ "error": "No matching element found" })),
+ )),
+ }
+}
+
+#[oasgen]
+async fn click_element_handler(
+ State(_): State<Arc<AppState>>,
+ Json(request): Json<ClickElementRequest>,
+) -> Result<JsonResponse<ActionResponse>, (StatusCode, JsonResponse<Value>)> {
+ let desktop = match Desktop::new(
+ request.selector.use_background_apps.unwrap_or(false),
+ request.selector.activate_app.unwrap_or(false),
+ ) {
+ Ok(d) => d,
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to initialize desktop automation: {}", e)
+ })),
+ ));
+ }
+ };
+
+ let app = match desktop.application(&request.selector.app_name) {
+ Ok(app) => app,
+ Err(e) => {
+ return Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({
+ "error": format!("Application not found: {}", e)
+ })),
+ ));
+ }
+ };
+
+ debug!("app: {:?}", app.text(1).unwrap_or_default());
+
+ // Find elements matching the selector
+ let element = match app.locator(request.selector.locator.as_str()) {
+ Ok(locator) => match locator.first() {
+ Ok(element) => element,
+ Err(e) => {
+ error!("Failed to find elements: {}", e);
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to find elements: {}", e)
+ })),
+ ));
+ }
+ },
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to create locator: {}", e)
+ })),
+ ));
+ }
+ };
+
+ debug!("element: {:?}", element);
+
+ match element {
+ Some(element) => match element.click() {
+ Ok(_) => Ok(JsonResponse(ActionResponse {
+ success: true,
+ message: format!("Clicked element with role: {}", element.role()),
+ })),
+ Err(e) => {
+ error!("Failed to click element: {}", e);
+ Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to click element: {}", e)
+ })),
+ ))
+ }
+ },
+ None => Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({
+ "error": "No matching element found"
+ })),
+ )),
+ }
+}
+
+#[oasgen]
+async fn type_text_handler(
+ State(_): State<Arc<AppState>>,
+ Json(request): Json<TypeTextRequest>,
+) -> Result<JsonResponse<ActionResponse>, (StatusCode, JsonResponse<Value>)> {
+ let desktop = match Desktop::new(
+ request.selector.use_background_apps.unwrap_or(false),
+ request.selector.activate_app.unwrap_or(false),
+ ) {
+ Ok(d) => d,
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to initialize desktop automation: {}", e)
+ })),
+ ));
+ }
+ };
+
+ let app = match desktop.application(&request.selector.app_name) {
+ Ok(app) => app,
+ Err(e) => {
+ return Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({
+ "error": format!("Application not found: {}", e)
+ })),
+ ));
+ }
+ };
+
+ debug!("app: {:?}", app);
+ // Find elements matching the selector
+ let element = match app.locator(request.selector.locator.as_str()) {
+ Ok(locator) => match locator.first() {
+ Ok(element) => element,
+ Err(e) => {
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to find elements: {}", e)
+ })),
+ ));
+ }
+ },
+ Err(e) => {
+ error!("Failed to create locator: {}", e);
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to create locator: {}", e)
+ })),
+ ));
+ }
+ };
+
+ debug!("element: {:?}", element);
+
+ match element {
+ Some(element) => match element.type_text(&request.text) {
+ Ok(_) => Ok(JsonResponse(ActionResponse {
+ success: true,
+ message: format!("Typed text into element with role: {}", element.role()),
+ })),
+ Err(e) => {
+ error!("Failed to type text: {}", e);
+ Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ JsonResponse(json!({
+ "error": format!("Failed to type text: {}", e)
+ })),
+ ))
+ }
+ },
+ None => Err((
+ StatusCode::NOT_FOUND,
+ JsonResponse(json!({
+ "error": "No matching element found"
+ })),
+ )),
+ }
+}
diff --git a/screenpipe-vision/Cargo.toml b/screenpipe-vision/Cargo.toml
index 6afa2fa5a..48fa23663 100644
--- a/screenpipe-vision/Cargo.toml
+++ b/screenpipe-vision/Cargo.toml
@@ -98,7 +98,7 @@ windows = { version = "0.58", features = [
libc = "=0.2.164"
cidre = { git = "https://github.com/yury/cidre.git" } # idk why cant pin version / rev
accessibility-sys = "0.1.3"
-core-foundation = "0.9.4"
+core-foundation = "=0.10.0"
url = "2.5.0"
[target.'cfg(target_os = "linux")'.dependencies]
diff --git a/screenpipe-vision/src/browser_utils/macos.rs b/screenpipe-vision/src/browser_utils/macos.rs
index 25e98f040..f90b13975 100644
--- a/screenpipe-vision/src/browser_utils/macos.rs
+++ b/screenpipe-vision/src/browser_utils/macos.rs
@@ -1,11 +1,12 @@
use accessibility_sys::{
kAXChildrenAttribute, kAXFocusedWindowAttribute, kAXRoleAttribute, kAXTextFieldRole,
- kAXValueAttribute, AXUIElementCopyAttributeValue, AXUIElementCreateApplication,
- AXUIElementRef,
+ kAXValueAttribute, AXUIElementCopyAttributeValue, AXUIElementCreateApplication, AXUIElementRef,
};
use anyhow::Result;
use core_foundation::{
- array::CFArray, base::{CFRelease, CFTypeRef, TCFType}, string::CFString
+ array::CFArray,
+ base::{CFRelease, CFTypeRef, TCFType},
+ string::CFString,
};
use url::Url;
@@ -41,12 +42,13 @@ impl MacOSUrlDetector {
if status == accessibility_sys::kAXErrorSuccess {
let _value_str = CFString::wrap_under_get_rule(value as _);
let url_str = _value_str.to_string();
- let url_to_parse = if !url_str.starts_with("http://") && !url_str.starts_with("https://") {
- format!("https://{}", url_str)
- } else {
- url_str
- };
-
+ let url_to_parse =
+ if !url_str.starts_with("http://") && !url_str.starts_with("https://") {
+ format!("https://{}", url_str)
+ } else {
+ url_str
+ };
+
if Url::parse(&url_to_parse).is_ok() {
return Some(element);
}
@@ -60,16 +62,17 @@ impl MacOSUrlDetector {
CFString::from_static_string(kAXChildrenAttribute).as_concrete_TypeRef(),
&mut children,
);
-
+
if status == accessibility_sys::kAXErrorSuccess {
- let _children_array = CFArray::<*const std::ffi::c_void>::wrap_under_get_rule(children as _);
+ let _children_array =
+ CFArray::<*const std::ffi::c_void>::wrap_under_get_rule(children as _);
for child in _children_array.iter() {
if let Some(found) = self.find_url_field(*child as AXUIElementRef) {
return Some(found);
}
}
}
-
+
None
}
@@ -78,7 +81,7 @@ impl MacOSUrlDetector {
.arg("-e")
.arg(script)
.output()?;
-
+
if output.status.success() {
let url = String::from_utf8(output.stdout)?.trim().to_string();
return Ok(Some(url));
@@ -89,14 +92,14 @@ impl MacOSUrlDetector {
fn get_url_via_accessibility(&self, process_id: i32) -> Result<Option<String>> {
unsafe {
let app_element = AXUIElementCreateApplication(process_id);
-
+
let mut focused_window: CFTypeRef = std::ptr::null_mut();
let status = AXUIElementCopyAttributeValue(
app_element,
CFString::from_static_string(kAXFocusedWindowAttribute).as_concrete_TypeRef(),
&mut focused_window,
);
-
+
if status != accessibility_sys::kAXErrorSuccess {
CFRelease(app_element as CFTypeRef);
return Ok(None);
@@ -118,7 +121,7 @@ impl MacOSUrlDetector {
CFString::from_static_string(kAXValueAttribute).as_concrete_TypeRef(),
&mut url_value,
);
-
+
let result = if status == accessibility_sys::kAXErrorSuccess {
let url_str = CFString::wrap_under_get_rule(url_value as _);
Ok(Some(url_str.to_string()))
@@ -129,7 +132,7 @@ impl MacOSUrlDetector {
CFRelease(url_value as CFTypeRef);
CFRelease(focused_window as CFTypeRef);
CFRelease(app_element as CFTypeRef);
-
+
result
}
}
@@ -144,4 +147,4 @@ impl BrowserUrlDetector for MacOSUrlDetector {
self.get_url_via_accessibility(process_id)
}
}
-}
\ No newline at end of file
+}