Setting NSUserAutomatorTask variables without requiring Automator Workflows to declare that variable

可紊 提交于 2019-12-01 17:05:02

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.}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!