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:
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:
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.