简体   繁体   中英

Trying to integrate apple script in Swift mac os App to get the gui elements of an application on macOS

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM