I have a non-sandboxed app and I'm attempting to run a few lines of AppleScript occasionally in a handler. The handler is running, but I get the following error regardless of my settings. I've tried opening new projects, using new app IDs, running from Xcode and launching the app directly — everything I can think of and I still can't get this AppleScript code to run. The debugger just prints the following error code each time the handler runs:
{
NSAppleScriptErrorAppName = "System Events";
NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to System Events.";
NSAppleScriptErrorMessage = "Not authorized to send Apple events to System Events.";
NSAppleScriptErrorNumber = "-1743";
NSAppleScriptErrorRange = "NSRange: {117, 9}";
}
Here is the handler that is getting called at the appropriate time.
func centerCursor(appName: String? = runningApp.localizedName) -> Void {
guard let appName = appName else { return }
let aScript = """
on getCenter(\(appName))
set midX to 0
set midY to 0
tell application "System Events"
tell application process \(appName)
set frontmost to true
delay 0.5
set currentWindow to window 1
set windowPositon to the position of currentWindow
set windowSize to the size of currentWindow
{"Position", windowPositon, "Size", windowSize}
set midX to (item 1 of windowPositon) + ((item 1 of windowSize) / 2)
set midY to (item 2 of windowPositon) + ((item 2 of windowSize) / 2)
{midX, midY}
end tell
end tell
return {midX, midY}
end getCenter
getCenter("\(appName)")
"""
if let script = NSAppleScript(source: aScript) {
var error: NSDictionary?
script.executeAndReturnError(&error)
if let err = error {
print(err)
}
}
}
And here are some screen grabs of my settings. Am I missing something obvious? Do I need a different certificate or profile to make this work? The app never shoes up in System Preferences