Skip to content

Instantly share code, notes, and snippets.

@ghall89
Created March 12, 2025 00:42
Show Gist options
  • Select an option

  • Save ghall89/ad65bd2b923cae374688fe73d3e639ae to your computer and use it in GitHub Desktop.

Select an option

Save ghall89/ad65bd2b923cae374688fe73d3e639ae to your computer and use it in GitHub Desktop.
import AppleScriptObjC
import Foundation
enum AppleScriptError: Error {
case scriptCompilationFailed
case executionFailed(String)
}
func runAppleScript(script: String) throws -> String? {
var result: String?
var error: NSDictionary?
guard let appleScript = NSAppleScript(source: script) else {
throw AppleScriptError.scriptCompilationFailed
}
result = appleScript.executeAndReturnError(&error).stringValue
if let error = error {
throw AppleScriptError.executionFailed(error.description)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment