Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cua-driver): open Finder folder URLs reliably
Francesco Bonacci13057dc6c8c0
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/apps/mod.rs b/libs/cua-driver/rust/crates/platform-macos/src/apps/mod.rsindex 09324dbbdd..b83e632f37 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/apps/mod.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/apps/mod.rs@@ -4,7 +4,12 @@ pub mod nsworkspace;use anyhow::Context;use serde::{Deserialize, Serialize};-use std::process::Command;+use std::{+ ffi::c_void,+ process::Command,+ sync::mpsc::{self, SyncSender},+ time::Duration,+};#[derive(Debug, Clone, Serialize, Deserialize)]pub struct AppInfo {@@ -137,6 +142,14 @@ pub fn launch_with_urls_by_bundle(env: &std::collections::HashMap<String, String>,creates_new_instance: bool,) -> anyhow::Result<i32> {+ if additional_args.is_empty()+ && env.is_empty()+ && !creates_new_instance+ && finder_folder_handoff(bundle_id, urls)+ {+ return open_finder_folders(urls);+ }+// Pass the bundle id directly — see `launch_app` rationale above// (Cryptex-installed apps).//@@ -178,6 +191,15 @@ pub fn launch_with_urls_by_name(let located = locate_by_name(name).ok_or_else(|| anyhow::anyhow!("Could not locate app with name '{name}'"))?;let (app_ref, bid) = located.app_ref_and_bundle_id();+ if additional_args.is_empty()+ && env.is_empty()+ && !creates_new_instance+ && bid+ .as_deref()+ .is_some_and(|bundle_id| finder_folder_handoff(bundle_id, urls))+ {+ return open_finder_folders(urls);+ }// See `launch_with_urls_by_bundle` — skip `oapp` AppleEvent on// the URL-handoff path.let cfg = nsworkspace::OpenConfig {@@ -196,6 +218,80 @@ pub fn launch_with_urls_by_name(Ok(pid)}+pub(crate) fn finder_folder_handoff(bundle_id: &str, urls: &[String]) -> bool {+ bundle_id == "com.apple.finder"+ && !urls.is_empty()+ && urls.iter().all(|url| std::path::Path::new(url).is_dir())+}++fn open_finder_folders(urls: &[String]) -> anyhow::Result<i32> {+ const MAIN_QUEUE_TIMEOUT: Duration = Duration::from_secs(5);++ if objc2_foundation::MainThreadMarker::new().is_some() {+ select_finder_folders(urls)?;+ } else {+ let (tx, rx) = mpsc::sync_channel(1);+ let request = Box::new(FinderFolderRequest {+ folders: urls.to_vec(),+ tx,+ });+ unsafe {+ let main_queue = &raw const _dispatch_main_q as *const c_void;+ dispatch_async_f(+ main_queue,+ Box::into_raw(request) as *mut c_void,+ open_finder_folders_on_main,+ );+ }+ rx.recv_timeout(MAIN_QUEUE_TIMEOUT)+ .context("Timed out waiting for Finder folder handoff on the AppKit main queue")?+ .map_err(anyhow::Error::msg)?;+ }++ list_running_apps()+ .into_iter()+ .find(|app| app.bundle_id.as_deref() == Some("com.apple.finder") && app.pid > 0)+ .map(|app| app.pid)+ .ok_or_else(|| {+ anyhow::anyhow!("Finder accepted the folder open request but is not running")+ })+}++fn select_finder_folders(urls: &[String]) -> anyhow::Result<()> {+ use objc2_app_kit::NSWorkspace;+ use objc2_foundation::NSString;++ let workspace = unsafe { NSWorkspace::sharedWorkspace() };+ for folder in urls {+ let folder = NSString::from_str(folder);+ if !unsafe { workspace.selectFile_inFileViewerRootedAtPath(None, &folder) } {+ anyhow::bail!("Finder refused to open folder: {folder}");+ }+ }+ Ok(())+}++struct FinderFolderRequest {+ folders: Vec<String>,+ tx: SyncSender<Result<(), String>>,+}++#[link(name = "dispatch", kind = "dylib")]+extern "C" {+ static _dispatch_main_q: u8;+ fn dispatch_async_f(+ queue: *const c_void,+ context: *mut c_void,+ work: unsafe extern "C" fn(*mut c_void),+ );+}++unsafe extern "C" fn open_finder_folders_on_main(context: *mut c_void) {+ let request = unsafe { Box::from_raw(context.cast::<FinderFolderRequest>()) };+ let result = select_finder_folders(&request.folders).map_err(|error| error.to_string());+ let _ = request.tx.send(result);+}+// ── Bundle resolution ────────────────────────────────────────────────────────/// What `locate_by_name` resolved a display name into.@@ -620,7 +716,19 @@ pub fn format_app_list(apps: &[AppInfo]) -> String {#[cfg(test)]mod tests {- use super::unix_secs_to_rfc3339;+ use super::{finder_folder_handoff, unix_secs_to_rfc3339};++ #[test]+ fn finder_folder_handoff_is_narrowly_selected() {+ let folder = std::env::temp_dir().to_string_lossy().into_owned();++ assert!(finder_folder_handoff("com.apple.finder", &[folder.clone()]));+ assert!(!finder_folder_handoff("com.apple.TextEdit", &[folder]));+ assert!(!finder_folder_handoff(+ "com.apple.finder",+ &["https://example.com".to_owned()]+ ));+ }#[test]fn rfc3339_epoch() {diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/launch_app.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/launch_app.rsindex a08d6ba03d..6085da9947 100644--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/launch_app.rs+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/launch_app.rs@@ -91,7 +91,11 @@ impl Tool for LaunchAppTool {let name = args.opt_str("name");let mut response_bundle_id = bundle_id.clone();let response_requested_name = name.clone();- let urls: Vec<String> = args.str_array("urls");+ let urls: Vec<String> = args+ .str_array("urls")+ .into_iter()+ .map(normalize_launch_url)+ .collect();if args.get("cdp_debugging_port").is_some() {return ToolResult::error("cdp_debugging_port moved to browser_prepare so DevTools is never enabled on an unproven user profile",@@ -187,15 +191,26 @@ impl Tool for LaunchAppTool {// frontmost — handles the intra-`open()` synchronous activation// that fired before we could arm with the real pid.let prior_frontmost = crate::apps::frontmost_pid();-- let wildcard_lease = prior_frontmost.map(|prior| {- crate::focus_steal::FocusStealPreventer::begin_suppression(- None,- prior,- "LaunchAppTool.pre",- )+ let finder_folder_handoff = response_bundle_id.as_deref().is_some_and(|bundle_id| {+ additional_arguments.is_empty()+ && env.is_empty()+ && !creates_new_instance+ && crate::apps::finder_folder_handoff(bundle_id, &urls)});+ // Finder's synchronous folder-open selector must be allowed to activate+ // long enough to perform the request. Use the ordinary targeted+ // post-launch guard to restore the prior foreground app immediately.+ let wildcard_lease = prior_frontmost+ .filter(|_| !finder_folder_handoff)+ .map(|prior| {+ crate::focus_steal::FocusStealPreventer::begin_suppression(+ None,+ prior,+ "LaunchAppTool.pre",+ )+ });+// Predicate captured BEFORE moving inputs into spawn_blocking.// Same condition that selects the `openURLs:withApplicationAtURL:`// chain over the simpler `openApplicationAtURL:` path. Used after@@ -635,6 +650,12 @@ fn preflight_file_urls(urls: &[String]) -> Option<ToolResult> {None}+fn normalize_launch_url(raw: String) -> String {+ local_file_target(&raw)+ .map(|path| path.to_string_lossy().into_owned())+ .unwrap_or(raw)+}+fn local_file_target(raw: &str) -> Option<PathBuf> {if raw.is_empty() {return Some(PathBuf::from(raw));@@ -698,7 +719,8 @@ fn hex_value(byte: u8) -> Option<u8> {mod tests {use super::{contains_remote_debugging_flag, is_cua_driver_bundle_id, local_file_target,- preflight_file_urls, response_identity, structured_launch_failure, LaunchAppTool,+ normalize_launch_url, preflight_file_urls, response_identity, structured_launch_failure,+ LaunchAppTool,};use cua_driver_core::tool::Tool;use serde_json::json;@@ -716,6 +738,20 @@ mod tests {);}+ #[test]+ fn launch_url_normalization_expands_home_relative_paths() {+ let home = PathBuf::from(std::env::var_os("HOME").expect("HOME must be set for macOS"));++ assert_eq!(+ PathBuf::from(normalize_launch_url("~/Desktop/BenchInbox".to_owned())),+ home.join("Desktop/BenchInbox")+ );+ assert_eq!(+ normalize_launch_url("https://example.com".to_owned()),+ "https://example.com"+ );+ }+#[test]fn local_file_target_skips_remote_and_custom_schemes() {assert_eq!(local_file_target("https://example.com"), None);