设置NSUserAutomatorTask变量而无需Automator Workflow声明该变量 [英] Setting NSUserAutomatorTask variables without requiring Automator Workflows to declare that variable

查看:136
本文介绍了设置NSUserAutomatorTask变量而无需Automator Workflow声明该变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSUserAutomatorTask启动.workflow文件,该文件是通过macOS 10.13中的Automator应用程序创建的.

I'm using NSUserAutomatorTask to launch a .workflow file, created via the Automator app in macOS 10.13.

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

I'm passing variables to the workflow via the variables property:

https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099变量

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

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:

没有名为"randomVariable"的变量

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.

推荐答案

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

The AMWorkFlow methods setValue(_:forVariableWithName:) and valueForVariable(withName:) both safely determine if that variable is set in the workflow file.

因此,在NSUserAutomatorTask旁边构造一个AMWorkFlow.如AMWorkFlow所示,仅设置脚本正在使用的变量:

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根本不在沙箱中执行,因此必须使用NSUserAutomatorTask才能真正运行工作流程.

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在运行此工作流程时遇到错误:

装有沙盒的应用程序不能使用Automator.framework来运行工作流.

Error Domain = com.apple.Automator代码= 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来运行工作流.}

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 Workflow声明该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆