I\'m using NSUserAutomatorTask to launch a .workflow file, created via the Automator app in macOS 10.13.
I\'m passing variables to the work
The AMWorkFlow methods setValue(_:forVariableWithName:) and valueForVariable(withName:) both safely determine if that variable is set in the workflow file.
So, construct an AMWorkFlow alongside your NSUserAutomatorTask. Only set the variables that the script is using, as indicated by the AMWorkFlow:
if let automatorTask = try? NSUserAutomatorTask(url: url) {
if let varChecker = try? AMWorkflow(contentsOf: url) {
automatorTask.variables = POSSIBLE_VARIABLES.filter {
return varChecker.setValue($0.value, forVariableWithName: $0.key)
// -or- //
return varChecker.valueForVariable(withName: $0.key) != nil
}
}
automatorTask.execute(withInput: nil, completionHandler: nil)
}
AMWorkFlow does not execute at all in the Sandbox, so you must use NSUserAutomatorTask to actually run the workflow.
do {
try AMWorkflow.run(at: url, withInput: nil)
} catch let error {
print(error)
}
Automator encountered an error running this workflow:
Sandboxed applications can not use Automator.framework to run workflows.
Error Domain=com.apple.Automator Code=0 "Automator encountered an error running this workflow: “Sandboxed applications can not use Automator.framework to run workflows.”" UserInfo={NSUnderlyingError=0x604000e498a0 {Error Domain=com.apple.Automator Code=0 "Sandboxed applications can not use Automator.framework to run workflows." UserInfo={NSLocalizedDescription=Sandboxed applications can not use Automator.framework to run workflows.}}, NSLocalizedDescription=Automator encountered an error running this workflow: “Sandboxed applications can not use Automator.framework to run workflows.”, NSLocalizedFailureReason=Sandboxed applications can not use Automator.framework to run workflows.}