Skip to content

Instantly share code, notes, and snippets.

@wongjustin99
Created August 3, 2019 05:16
Show Gist options
  • Save wongjustin99/8bd37eb3d39c9ea37d4405a9a7865c86 to your computer and use it in GitHub Desktop.
Save wongjustin99/8bd37eb3d39c9ea37d4405a9a7865c86 to your computer and use it in GitHub Desktop.
Run AppleScript from URL in swift
// need to remove extenion in XCode 10.1, or else I get weird encoding errors that corrupt the script file even when using outside of XCode
let myAppleScript = Bundle.main.url(forResource: "say_hello", withExtension: nil)!
// unnecessary catch statements/need some cleanup
func runAppleScriptFromUrl(scriptUrl: URL) {
do {
var errorScriptLoad: NSDictionary?
if let scriptObject = NSAppleScript(contentsOf: scriptUrl, error: &errorScriptLoad) {
var error: NSDictionary?
if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError(
&error) {
print(output.stringValue ?? "No script output")
} else if (error != nil) {
print("error: \(error)")
}
} else {
print("couldnt load script:\(errorScriptLoad)")
}
} catch {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment