设置 NSUserAutomatorTask 变量而不需要 Automator Workflows 来声明该变量 [英] Setting NSUserAutomatorTask variables without requiring Automator Workflows to declare that variable
问题描述
我正在使用 NSUserAutomatorTask
来启动 .workflow
文件,该文件是通过 macOS 10.13 中的 Automator 应用程序创建的.
我通过 variables
属性将变量传递给工作流:
我不再收到错误,并且工作流程正确执行:
这是一个问题,因为我想将多条信息作为变量传递给任何潜在的工作流程,并让每个工作流程单独决定处理每个所需的参数.
我不想要求每个工作流都声明我的应用将可选提供的变量.
请注意,在我的示例工作流程中,从未使用过该变量.我不希望声明它的附加要求.
有什么办法可以避免每个工作流都声明我的应用在执行工作流时传递的变量?
或者,有没有办法检查工作流声明了哪些变量?然后我只能传递工作流实际使用的那些.
AMWorkFlow
方法 setValue(_:forVariableWithName:)
和 valueForVariable(withName:)
两者都可以安全地确定是否在工作流文件中设置了该变量.
因此,在您的 NSUserAutomatorTask
旁边构建一个 AMWorkFlow
.仅设置脚本正在使用的变量,如 AMWorkFlow
所示:
if let automatorTask = try?NSUserAutomatorTask(网址:网址){如果让 varChecker = 试试?AMWorkflow(contentsOf: url) {automatorTask.variables = POSSIBLE_VARIABLES.filter {return varChecker.setValue($0.value, forVariableWithName: $0.key)//-或者-//返回 varChecker.valueForVariable(withName: $0.key) != nil}}automatorTask.execute(withInput: nil, completionHandler: nil)}
AMWorkFlow
在 Sandbox 中根本不执行,因此您必须使用 NSUserAutomatorTask
来实际运行工作流.
做{尝试 AMWorkflow.run(at: url, withInput: nil)} 抓住让错误{打印(错误)}
<块引用>
Automator 在运行此工作流时遇到错误:
沙盒应用程序无法使用 Automator.framework 运行工作流.
Error Domain=com.apple.Automator Code=0 Automator 在运行此工作流时遇到错误:沙盒应用程序无法使用 Automator.framework 运行工作流."";UserInfo={NSUnderlyingError=0x604000e498a0 {Error Domain=com.apple.Automator Code=0 沙盒应用程序不能使用 Automator.framework 运行工作流.";UserInfo={NSLocalizedDescription=沙盒应用程序不能使用 Automator.framework 运行工作流.}},NSLocalizedDescription=Automator 在运行此工作流时遇到错误:沙盒应用程序不能使用 Automator.framework 运行工作流.",NSLocalizedFailureReason=沙盒应用程序可以不要使用 Automator.framework 来运行工作流.}
I'm using NSUserAutomatorTask
to launch a .workflow
file, created via the Automator app in macOS 10.13.
I'm passing variables to the workflow via the variables
property:
https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099-variables
The parent app is sandboxed. The script is located in the .applicationScriptsDirectory
and runs successfully when variables are not set, or when the same variables are set from the app and declared in the workflow.
if let workflow = try? NSUserAutomatorTask(url: url) {
workflow.variables = ["randomVariable": "value"] // NOTE
workflow.execute(withInput: nil) { (value, error) in
if let error = error {
print(error) // No variable named "randomVariable"
}
}
}
The workflow fails to run with the error:
No variable named "randomVariable"
However, if I edit the workflow and add a Variable to match the one set in code, everything is fine.
I no longer get the error, and the workflow executes correctly:
This is an issue because I want to pass multiple pieces of information as variables to any potential workflow, and for each workflow to individually decide to handle each needed parameter.
I do not want to require that every workflow declare the variables that my app will optionally provide.
Note that in my sample workflow the variable is never used. I do not want the additional requirement that it be declared.
Is there any way to avoid each workflow declaring the variables that my app passes when executing the workflow?
Or, is there a way to inspect which variables a workflow has declared? I could then only pass the ones actually used by the workflow.
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.}
这篇关于设置 NSUserAutomatorTask 变量而不需要 Automator Workflows 来声明该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!