Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you execute an Applescript script from a Swift Application

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.)

like image 824
codingguy3000 Avatar asked Aug 06 '14 14:08

codingguy3000


People also ask

How do I run an Apple Script?

In the Script Editor app on your Mac, click the Run button in the toolbar, or press Command-R, to execute the commands in your script.

What is Nsapplescript?

An object that provides the ability to load, compile, and execute scripts.


1 Answers

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)")     } } 
like image 52
zekel Avatar answered Sep 22 '22 13:09

zekel