Here is a quick demo of how to integrate AppleScript synchronous and asynchronous execution into a SwiftUI application.
The AppleScriptRunner
class has an observable property, state
, which reports the execution state of the associated AppleScript script. To use AppleScriptRunner
, first create an @ObservedObject
variable in your SwiftUI view:
@ObservedObject var script: AppleScriptRunner("""
property counter: 0
set counter to counter + 1
return counter
""")
To run the script, AppleScriptRunner provides two functions: executeSync
(run script on the main thread) and executeAsync
(run the script in a background thread). You can call one of these functions from a SwiftUI button:
Button("Run") {
script.executeAsync()
}
Then, in your view you can declare content depending on the script’s state:
switch script.state {
case .running:
// indicate the script is running...
Text("Running...")
case .complete(let result):
// Show the result of the script...
Text("\(result)") // result is an NSAppleEventDescriptor
.foregroundColor(.green)
case .error(let error):
// Show the runtime error
Text(error.message)
.foregroundColor(.red)
case .idle:
// The script has never been run...
Group() {} // empty ...
}
For the purposes of this demo the AppleScriptRunner
class is simplistic in that it only accepts AppleScript source code through its constructor. Obvious improvements include loading compiled scripts, changing the script, etc.