0

I'm trying to build a macOS Swift app that retrieves UI elements from another app using accessibility. I'm having permission issue despite checking 'AppleEvents' in Hardened Runtime. I also added 'NSAppleEventsUsageDescription' in the plist, but I still get permission issue. I am using this code to test it:

import SwiftUI
import Cocoa
import ApplicationServices

struct ContentView: View {
    @State private var message: String = "Checking accessibility permissions..."

    var body: some View {
        VStack {
            Text("Accessibility Test")
                .font(.largeTitle)
                .padding()

            Text(message)
                .padding()
                .multilineTextAlignment(.center)
        }
        .padding()
        .onAppear {
            checkAccessibilityPermissions()
        }
    }

    func checkAccessibilityPermissions() {
        DispatchQueue.global(qos: .background).async {
            let systemPreferencesApp = NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.systempreferences").first

            guard let systemPreferencesApp = systemPreferencesApp else {
                DispatchQueue.main.async {
                    self.message = "System Preferences is not running."
                }
                return
            }

            let pid = systemPreferencesApp.processIdentifier
            let axApp = AXUIElementCreateApplication(pid)

            var attributeValue: CFTypeRef?
            let result = AXUIElementCopyAttributeValue(axApp, kAXFocusedWindowAttribute as CFString, &attributeValue)

            DispatchQueue.main.async {
                if result == .success {
                    self.message = "Accessibility permissions are set correctly."
                } else {
                    self.message = "Accessibility permissions are not set correctly. Error code: \(result.rawValue)"
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
2
  • Accessibility API is not permitted in sandbox.
    – soundflix
    Commented May 24, 2024 at 22:11
  • 1
    Turning it off worked, thank you!!
    – HNT
    Commented May 24, 2024 at 22:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.