0

Trying to integrate apple script in Swift mac os App and getting the following error NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to System Events.";

Following is the script

        activate application "Calendar"
        delay 0.1
        tell application "System Events"
            tell front window of application process "Calendar"
                set uiElems to entire contents
            end tell
        end tell
"""

and Following is the entire code

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: NSWindow!


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Create the window and set the content view. 
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
        let myAppleScript = """
        activate application "Calendar"
        delay 0.1
        tell application "System Events"
            tell front window of application process "Calendar"
                set uiElems to entire contents
            end tell
        end tell
"""
        var error: NSDictionary?
        if let scriptObject = NSAppleScript(source: myAppleScript) {
            if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
                                                                               &error) {
                print(output)
            } else if (error != nil) {
                print("error: \(error)")
            }
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

I tried 1)adding Xcode, calendar in accessibility 2)added entry to plist

Has anyone faced this issue

3
  • You need to ask permission to the user
    – Leo Dabus
    Commented Jul 1, 2020 at 1:37
  • Make sure to authorize your App to send AppleScript events inside your macOS -> System Preferences > Security & Privacy > Automation
    – Leo Dabus
    Commented Jul 1, 2020 at 1:43
  • stackoverflow.com/a/55876631/341994
    – matt
    Commented Jul 1, 2020 at 4:48

1 Answer 1

6

In order for an app to communicate with other apps via Apple Events, you need a few things configured in your Xcode project:

  1. A general entitlement for Apple Events
  2. A specific entitlement for the app you want to script, either using the "temporary-exception" if it's an older app or a specific access entitlement for a newer app that supports it
  3. An Info.plist entry that causes the system to prompt for scripting permission for your app

Here's an example of an entitlements file entry for Apple Events plus a temporary-exception for an application (by its bundle ID):

<key>com.apple.security.automation.apple-events</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events</key>
<string>com.apple.QuickTimePlayerX</string>

Here's an example of the Info.plist entry that is needed:

<key>NSAppleEventsUsageDescription</key>
<string>Please give ScreenRecordingDetector access to Quicktime Player via Apple Script.</string>

Some relevant documentation:

QA1888 Sandboxing and Automation in OS X

App Sandbox Temporary Exception Entitlements

3
  • Also, if Hardened Runtime is enabled in Signing & Capabilities, you must check the “Apple Events” option or outgoing events will be blocked.
    – foo
    Commented Jul 1, 2020 at 6:02
  • Yes I believe enabling that in the Xcode Signing & Capabilities results in the apple-events entitlement file entry mentioned in my answer.
    – Corbell
    Commented Jul 1, 2020 at 6:05
  • This answer is a good review of the requirements to use Apple Events in an application. It helped me determine the missing pieces of my Objective-C application in scripting the Calendar. Commented Oct 16, 2020 at 18:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.