gpt4 book ai didi

macos - 设置 NSUserAutomatorTask 变量,而不需要 Automator 工作流声明该变量

转载 作者:行者123 更新时间:2023-12-03 16:09:47 25 4
gpt4 key购买 nike

我正在使用 NSUserAutomatorTask 启动通过 macOS 10.13 中的 Automator 应用创建的 .workflow 文件。

我通过 variables 属性将变量传递到工作流程:

https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099-variables

父应用程序已沙盒化。该脚本位于 .applicationScriptsDirectory 中,并且在未设置变量或从应用程序设置相同变量并在​​工作流中声明时成功运行。

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

工作流无法运行并出现错误:

No variable named "randomVariable"

但是,如果我编辑工作流程并添加一个变量以匹配代码中设置的变量,则一切都很好。

Add var to workflow

我不再收到错误,并且工作流程正确执行:

Execute workflow

这是一个问题,因为我想将多条信息作为变量传递给任何潜在的工作流程,并让每个工作流程单独决定处理每个所需的参数。

我不想要求每个工作流程都声明我的应用可选提供的变量。

请注意,在我的示例工作流程中,从未使用过该变量。我不希望有额外的声明要求。

有什么方法可以避免每个工作流程声明我的应用程序在执行工作流程时传递的变量吗?

或者,有没有办法检查工作流已声明哪些变量?然后我只能传递工作流程实际使用的那些。

最佳答案

AMWorkFlow 方法 setValue(_:forVariableWithName:)valueForVariable(withName:) 都可以安全地确定该变量是否在工作流程文件。

因此,在 NSUserAutomatorTask 旁边构建一个 AMWorkFlow。仅设置脚本正在使用的变量,如 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 在沙盒中根本不执行,因此您必须使用 NSUserAutomatorTask 来实际运行工作流。

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

关于macos - 设置 NSUserAutomatorTask 变量,而不需要 Automator 工作流声明该变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52507620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com