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