Skip to content

Commit 1db9b7d

Browse files
committed
feat: add 'apple-events' status query
1 parent f5cf0f2 commit 1db9b7d

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ If you're using macOS 12.3 or newer, you'll need to ensure you have Python insta
5757

5858
### `permissions.getAuthStatus(type)`
5959

60-
- `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `bluetooth`, `calendar`, `camera`, `contacts`, `full-disk-access`, `input-monitoring`, `location`, `microphone`,`photos`, `reminders`, `screen`, or `speech-recognition`.
60+
- `type` String - The type of system component to which you are requesting access. Can be one of `accessibility`, `apple-events`, `bluetooth`, `calendar`, `camera`, `contacts`, `full-disk-access`, `input-monitoring`, `location`, `microphone`,`photos`, `reminders`, `screen`, or `speech-recognition`.
6161

6262
Returns `String` - Can be one of `not determined`, `denied`, `authorized`, or `restricted`.
6363

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const permissions = require('bindings')('permissions.node')
33
function getAuthStatus(type) {
44
const validTypes = [
55
'accessibility',
6+
'apple-events',
67
'bluetooth',
78
'calendar',
89
'camera',

permissions.mm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ bool HasOpenSystemPreferencesDialog() {
237237
return kAuthorized;
238238
}
239239

240+
// Returns a status indicating whether the user has authorized Apple Events.
241+
std::string AppleEventsAuthStatus(Napi::Env env) {
242+
if (@available(macOS 10.14, *)) {
243+
AEDesc target_app = {typeNull, NULL};
244+
OSStatus status = AECreateDesc(typeApplicationBundleID, "com.apple.finder",
245+
strlen("com.apple.finder"), &target_app);
246+
if (status != noErr) {
247+
std::string err_msg = "Failed to query for Apple Events access";
248+
Napi::Error::New(env, err_msg).ThrowAsJavaScriptException();
249+
return kNotDetermined;
250+
}
251+
252+
status = AEDeterminePermissionToAutomateTarget(&target_app, kCoreEventClass,
253+
kAEOpenDocuments, false);
254+
255+
AEDisposeDesc(&target_app);
256+
257+
// User prompt has not yet been shown.
258+
if (status == errAEEventWouldRequireUserConsent) {
259+
return kNotDetermined;
260+
}
261+
262+
return status == noErr ? kAuthorized : kDenied;
263+
}
264+
265+
return kAuthorized;
266+
}
267+
240268
// Returns a status indicating whether the user has authorized Apple Music
241269
// Library access.
242270
std::string MusicLibraryAuthStatus() {
@@ -452,6 +480,8 @@ bool HasOpenSystemPreferencesDialog() {
452480
auth_status = MusicLibraryAuthStatus();
453481
} else if (type == "input-monitoring") {
454482
auth_status = InputMonitoringAuthStatus();
483+
} else if (type == "apple-events") {
484+
auth_status = AppleEventsAuthStatus(env);
455485
}
456486

457487
return Napi::Value::From(env, auth_status);

0 commit comments

Comments
 (0)