2

It's impossible for me to run an Apple Script from XCode. Here is how I try to make it work :

                let appleScript = NSAppleScript(source: script)
                var error: NSDictionary?
                if let outputString = appleScript?.executeAndReturnError(&error).stringValue {
                    print(outputString)
                } else if (error != nil) {
                    print("error: \(error!)")
                }`

I got this error in output :

error : {
    NSAppleScriptErrorAppName = Finder;
    NSAppleScriptErrorBriefMessage = "Not authorized to send Apple events to Finder.";
    NSAppleScriptErrorMessage = "Not authorized to send Apple events to Finder.";
    NSAppleScriptErrorNumber = "-1743"
    NSAppleScriptErrorRange = "NSRange: {63,1O}"

I've done everything described here and check every permission on my Entitlement file. "Apple Events" are set to "YES", but that is still not working.

I'm really stuck here. Any suggestion ? I'm on Monterey.

1 Answers1

0

I found a solution to this : putting the applescript.scpt file in the ressources of my app and call it with :

 AppKit.NSEvent.addGlobalMonitorForEvents(matching: .leftMouseDown) { event in
        let task = Process()
        if let path = Bundle.main.path(forResource: "getpathscript.scpt", ofType: nil) {
            task.launchPath = "/usr/bin/osascript"
            task.arguments = ["\(path)"]
            let pipe = Pipe()
            task.standardOutput = pipe
            task.launch()
            let data = pipe.fileHandleForReading.readDataToEndOfFile()
            filepath = (String(data: data, encoding: .utf8)?.trimmingCharacters(in: .newlines))!
        }
    }

Hope this helps.