From 743ddb62eeceecb519026ec086552d3a42a18887 Mon Sep 17 00:00:00 2001 From: Iewnfod Date: Tue, 26 Mar 2024 10:15:57 +0800 Subject: [PATCH] fix(macos): accessibility permission request --- src-tauri/Cargo.toml | 2 ++ src-tauri/src/config.rs | 17 +++-------------- src-tauri/src/open.rs | 29 ++++++++++++++++++++--------- src-tauri/tauri.macos.conf.json | 5 ++++- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 76ce577..d83704e 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -32,6 +32,8 @@ auto-launch = "0.5.0" lazy_static = "1.4.0" env_logger = "0.11.2" log = "0.4.21" +home = "0.5.9" +macos-accessibility-client = "0.0.1" [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index 171282f..29f948d 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -1,22 +1,11 @@ use auto_launch::AutoLaunch; +use home::home_dir; use log::debug; use std::env::current_exe; -use std::path::PathBuf; -use std::{fs::create_dir_all, path::Path}; +use std::fs::create_dir_all; use crate::constants::*; -fn get_user_home() -> PathBuf { - let user = users::get_user_by_uid(users::get_current_uid()).unwrap(); - if cfg!(target_os = "macos") { - Path::new("/Users").join(user.name()) - } else if cfg!(target_os = "linux") { - Path::new("/home").join(user.name()) - } else { - Path::new(user.name()).to_path_buf() - } -} - fn get_auto_launcher() -> AutoLaunch { let mut exe_path = current_exe().unwrap(); while exe_path.is_symlink() { @@ -45,7 +34,7 @@ fn remove_startup() -> bool { } fn generate_config_path() { - let home = get_user_home(); + let home = home_dir().unwrap(); let mut config_path = home.join(".config").join(APP_ID); if cfg!(target_os = "macos") { config_path = home diff --git a/src-tauri/src/open.rs b/src-tauri/src/open.rs index e8fa69f..6808317 100644 --- a/src-tauri/src/open.rs +++ b/src-tauri/src/open.rs @@ -22,18 +22,29 @@ fn sub_sub_open(target_path: &String) { #[cfg(target_os = "macos")] fn run_show_window_apple_script(app_name: &String) -> bool { - let apple_script_1 = format!("tell application \"{}\" to activate", app_name); - let apple_script_2 = format!("tell application \"System Events\" to click UI element \"{}\" of list 1 of application process \"Dock\"", app_name); + // check accessibility + use macos_accessibility_client::accessibility; - let mut command1 = Command::new("osascript"); - command1.arg("-e").arg(&apple_script_1); - let result1 = command1.output().unwrap(); + if !accessibility::application_is_trusted_with_prompt() { + return false; + } + + let apple_scripts = vec![ + format!("tell application \"{}\" to activate", app_name), + format!("tell application \"System Events\" to click UI element \"{}\" of list 1 of application process \"Dock\"", app_name) + ]; + + for apple_script in apple_scripts { + let mut command = Command::new("osascript"); + command.arg("-e").arg(&apple_script); + let result = command.output().unwrap(); - let mut command2 = Command::new("osascript"); - command2.arg("-e").arg(&apple_script_2); - let result2 = command2.output().unwrap(); + if !result.status.success() { + return false; + } + } - result1.status.success() && result2.status.success() + true } #[cfg(target_os = "macos")] diff --git a/src-tauri/tauri.macos.conf.json b/src-tauri/tauri.macos.conf.json index b316316..ad76ffd 100644 --- a/src-tauri/tauri.macos.conf.json +++ b/src-tauri/tauri.macos.conf.json @@ -5,7 +5,10 @@ "iconAsTemplate": true }, "bundle": { - "targets": "all" + "targets": "all", + "macOS": { + "minimumSystemVersion": "10.13" + } } } }