I’m trying to create a Rust program that will ask for accessibility permission on macOS.
If the permission is granted (through Settings -> Privacy & Security -> Accessibility), then it works and returns true from AXIsProcessTrustedWithOptions
.
But if the permission is not allowed, instead of prompting to ask for the permission, the program crashes with a segfault.
It seems the settings dictionary is created incorrectly, but I’m not sure why.
I created it by following AXIsProcessTrustedWithOptions Apple’s docs and tested it on macOS 14.4.1 (Apple silicon).
use accessibility_sys::AXIsProcessTrustedWithOptions;
use core_foundation_sys::base::{CFRelease, TCFTypeRef};
use core_foundation_sys::dictionary::{CFDictionaryAddValue, CFDictionaryCreateMutable};
use core_foundation_sys::number::kCFBooleanTrue;
use std::ffi::CString;
use std::ptr;
fn main() {
let mut is_allowed = false;
unsafe {
let options =
CFDictionaryCreateMutable(ptr::null_mut(), 1, std::ptr::null(), std::ptr::null());
let key = CString::new("AXTrustedCheckOptionPrompt").unwrap();
let value = kCFBooleanTrue;
CFDictionaryAddValue(options, key.as_ptr().as_void_ptr(), value.as_void_ptr());
is_allowed = AXIsProcessTrustedWithOptions(options);
CFRelease(options as *const _);
}
println!("Accessibility permission enabled: {}", is_allowed);
}