How to listen for Privacy & Security > Automation > System Events status changes in MacOS Swift

My project (an non-sandbox app) was written in Swift on MacOS and it can execute the Apple script successfully.

I would like to listen for Privacy & Security > Automation > System Events status changes when the user changes it in System Settings to disable or enable my app feature in MacOS.

My app can receive Accessibility changes through this notification "com.apple.accessibility.api"

Are there any system notifications for my app to receive Automation > System Events status changes?

Thank you!

Post not yet marked as solved Up vote post of lgminh Down vote post of lgminh
307 views

Replies

My app can receive Accessibility changes through this notification com.apple.accessibility.api

In general, notifications are only considered API if there’s a symbolic constant for them in the headers. That’s not the case here, so I recommend that you not rely on this.

Are there any system notifications for my app to receive Automation > System Events status changes?

I’m not aware of any notification for this.

Probably your best option (for both of these) is to refresh your UI when the user activates your app. That’ll work pretty well because the user has to switch out to System Settings to change these values.

For Apple events, you can use AEDeterminePermissionToAutomateTarget to determine the current state.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for your response!

I tried using AEDeterminePermissionToAutomateTarget but got no response on Xcode Version 15.2 (15C500b) and MacOS 13.5.

I think my app has a similar issue to this thread https://developer.apple.com/forums/thread/666528.

I would like to check permission before running Apple Script from my app so I use my Bundle.main.bundleIdentifier in determinePermission() function.

Here is my code, check() function and request() function does not return anything :

protocol AppAccess {
    typealias Status = AppAccessStatus
    func check() -> Status
    func request() -> Status
}

enum AppAccessStatus {
    case granted, denied, requiresConsent, notRunning, unknown(Int)
}

class AppAccessImpl: AppAccess {

    func check() -> Status {
        guard #available(OSX 10.14, *) else {
            return .granted
        }
        return determinePermission(ask: false)
    }

    func request() -> Status {
        guard #available(OSX 10.14, *) else {
            return .granted
        }
        return determinePermission(ask: true)
    }

    @available(OSX 10.14, *)
    private func determinePermission(ask: Bool) -> Status {
        let errAEEventWouldRequireUserConsent = OSStatus(-1744)
        if var addressDesc = NSAppleEventDescriptor(bundleIdentifier: Bundle.main.bundleIdentifier ?? "").aeDesc?.pointee {
            let appleScriptPermission = AEDeterminePermissionToAutomateTarget(&addressDesc, typeWildCard, typeWildCard, ask)
            AEDisposeDesc(&addressDesc)
            switch appleScriptPermission {
            case noErr: return .granted
            case OSStatus(errAEEventNotPermitted): return .denied
            case errAEEventWouldRequireUserConsent: return .requiresConsent
            case OSStatus(procNotFound):
                return .notRunning
            default: return .unknown(Int(appleScriptPermission))
            }
        }
        return .unknown(-999)
    }
}

Can you help to take a look? Thanks a lot!