Can you execute an Applescript script from a Swift Application

前端 未结 6 1574
野的像风
野的像风 2020-12-01 01:25

I have a simple AppleScript that sends an email. How can I call it from within a Swift application?

(I wasn\'t able to find the answer via Google.)

6条回答
  •  执笔经年
    2020-12-01 02:12

    As Kamaros suggests, you can call NSApplescript directly without having to launch a separate process via NSTask (as CRGreen suggests.)

    Swift Code

    let myAppleScript = "..."
    var error: NSDictionary?
    if let scriptObject = NSAppleScript(source: myAppleScript) {
        if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
                                                                           &error) {
            print(output.stringValue)
        } else if (error != nil) {
            print("error: \(error)")
        }
    }
    

提交回复
热议问题