Skip to content

Commit

Permalink
fix(macos): accessibility permission request
Browse files Browse the repository at this point in the history
  • Loading branch information
iewnfod committed Mar 26, 2024
1 parent 02442a1 commit 743ddb6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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
Expand Down
29 changes: 20 additions & 9 deletions src-tauri/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
5 changes: 4 additions & 1 deletion src-tauri/tauri.macos.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"iconAsTemplate": true
},
"bundle": {
"targets": "all"
"targets": "all",
"macOS": {
"minimumSystemVersion": "10.13"
}
}
}
}

0 comments on commit 743ddb6

Please sign in to comment.