Can you execute an Applescript script from a Swift Application

前端 未结 6 1575
野的像风
野的像风 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:10

    For anyone who is getting the warning below for Swift 4, for the line while creating an NSAppleEventDescriptor from zekel's answer

    Non-optional expression of type 'NSAppleEventDescriptor' used in a check for optionals

    You can get rid of it with this edited short version:

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

    However, you may have also realized; with this method, system logs this message to console everytime you run the script:

    AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference.

    Apparently it is a declared bug by an Apple staff developer, and is said to be 'just' a harmless log spam and is scheduled to be removed on future OS updates, as you can see in this very long apple developer forum post and SO question below:

    AppleEvents: received mach msg which wasn't complex type as expected in getMemoryReference

    Thanks Apple, for those bazillions of junk console logs thrown around.

提交回复
热议问题