Created
August 3, 2019 05:16
-
-
Save wongjustin99/8bd37eb3d39c9ea37d4405a9a7865c86 to your computer and use it in GitHub Desktop.
Run AppleScript from URL in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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