1

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

Entitlements file Hardened Runtime Option Target Build Settings Project Build Settings Info.plist - NSAppleEventsUsageDescription System Preferences - Privacy

se_puede_dev
  • 585
  • 7
  • 16
  • I was running into the same issue and ended up solving it after changing my `Info.plist` file as you show above, running it again, and then (after triggering the applescript again), following the prompts to give permissions in System Settings, under Privacy and Security>Accessibility (not Automation). Hope this helps someone else! – Chris McElroy Jul 08 '23 at 16:45
  • (I added a fuller description of what i did here: https://stackoverflow.com/a/76644078/8222178) – Chris McElroy Jul 08 '23 at 17:08

0 Answers0