我正在使用macOS 10.13中的Automator应用程序创建NSUserAutomatorTask
一个.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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
工作流无法运行并显示错误:
没有名为"randomVariable"的变量
但是,如果我编辑工作流并添加一个变量以匹配代码中的一个,那么一切都很好.
我不再收到错误,工作流程正确执行:
这是一个问题,因为我想将多条信息作为变量传递给任何潜在的工作流程,并且每个工作流程单独决定处理每个所需的参数.
我不想要求每个工作流声明我的应用程序可选择提供的变量.
请注意,在我的示例工作流程中,从不使用变量.我不希望声明它的额外要求.
有没有办法避免每个工作流声明我的应用程序在执行工作流时传递的变量?
或者,有没有办法检查工作流声明了哪些变量?然后我只能传递工作流实际使用的那些.
在 Xcode 中运行我的 AppleScript 的代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Script" ofType:@"scpt"];
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[script executeAndReturnError:nil];
Run Code Online (Sandbox Code Playgroud)
在执行它之前,我想知道是否可以设置一些变量供它使用。换句话说,我想将变量从我的应用程序传递给 AppleScript。