macos_accessibility_client/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![allow(non_snake_case)]

/// Raw C-bindings for some of Apple's accessibility API. See [Apple's documentation](https://developer.apple.com/documentation/applicationservices/axuielement_h?language=objc#overview)
pub mod raw {
	use core_foundation_sys::dictionary::CFDictionaryRef;
	use core_foundation_sys::base::Boolean;
	use core_foundation::string::CFStringRef;

	extern {
		pub static kAXTrustedCheckOptionPrompt: CFStringRef;

		pub fn AXIsProcessTrusted() -> Boolean;
		pub fn AXIsProcessTrustedWithOptions(theDict: CFDictionaryRef) -> Boolean;

	}
}

/// Wrapped bindings to some of Apple's accessibility client API.
pub mod accessibility {
	use core_foundation::string::CFString;
	use core_foundation::dictionary::CFDictionary;
	use core_foundation::boolean::CFBoolean;
	use core_foundation::base::TCFType;

	use crate::raw::{AXIsProcessTrusted, kAXTrustedCheckOptionPrompt, AXIsProcessTrustedWithOptions};

	/// Checks whether or not this application is a trusted accessibility client.
	pub fn application_is_trusted() -> bool {
		unsafe {
			return AXIsProcessTrusted() != 0;
		}
	}

	/// Same as [application_is_trusted], but also shows the user a prompt asking
	/// them to allow accessibility API access if it hasn't already been given.
	pub fn application_is_trusted_with_prompt() -> bool {
		unsafe {
			let option_prompt = CFString::wrap_under_get_rule(kAXTrustedCheckOptionPrompt);
			let dict: CFDictionary<CFString, CFBoolean> = CFDictionary::from_CFType_pairs(&[
				(option_prompt, CFBoolean::true_value()),
			]);
			return AXIsProcessTrustedWithOptions(dict.as_concrete_TypeRef()) != 0
		}
	}
}

#[cfg(test)]
mod tests {
	use crate::accessibility::{application_is_trusted, application_is_trusted_with_prompt};

	#[test]
	fn test_application_is_trusted() {
		assert_eq!(false, application_is_trusted());
	}

	#[test]
    fn test_application_is_trusted_with_prompt() {
		assert_eq!(false, application_is_trusted_with_prompt());
    }
}