objective-c - 在 NSUserAutomatorTask 中设置变量

标签 objective-c macos automator

我正在将一些代码从 AMWorkflow 迁移到 NSUserAutomatorTask,以便最终我可以将我的应用程序沙盒化。我希望能够像在 AMWorkflow 中那样在工作流中设置现有变量的值:

AMWorkflow *task = [[AMWorkflow alloc] initWithContentsOfURL:scriptURL error:nil];
[task setValue:@"myValue" forVariableWithName:@"myVar"];

但是我似乎无法使用 NSUserAutomatorTask 获得类似的东西。 我能找到的唯一文档(类引用)说将变量作为 NSDictionary 提供。

所以我正在尝试类似的方法:

NSUserAutomatorTask * task = [[NSUserAutomatorTask alloc] initWithURL:workflow error:nil];

task.variables = [NSDictionary dictionaryWithObject:@"myValue" forKey:@"myVar"];
[task executeWithInput:nil completionHandler:^(id result, NSError *error){
    if(error)
        NSLog(@"Error while executing workflow %@", [error localizedDescription]);
}];

我在另一个答案 ( Using AMWorkflow with sandboxed app ) 中读到,“executeWithInput:”为 NSUserAutomatorTask 提供的值被忽略。有没有可能变量也是?

最佳答案

当您在 10.8.3 中首次发布时,我确实尝试过此操作,但无法正常工作。我尝试了各种方法,但都没有成功。

我现在使用的是 10.8.4,它现在似乎可以正常工作,而无需对您的基本代码进行任何实际更改。

NSUserAutomatorTask * task = [[NSUserAutomatorTask alloc] initWithURL:[NSURL  URLWithString:@"file:///Users/UserName/Desktop/folderActionTest/test.workflow"] error:nil];

NSDictionary* taskDict = [NSDictionary dictionaryWithObject:@"Test item" forKey:@"Storage"];
task.variables=taskDict;
[task executeWithInput:nil completionHandler:^(id result, NSError *error){
    if(error)
        NSLog(@"Error while executing workflow %@", [error localizedDescription]);
}];

工作流很简单,它已经有一个名为 Storage 的变量和一个从变量中获取输入的列表。

enter image description here

代码运行时的工作流。

enter image description here

关于objective-c - 在 NSUserAutomatorTask 中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111994/

相关文章:

ios - UITextField强制重绘

objective-c - UISplitViewController - 通过 Storyboard segues 的多个详细 View Controller

macos - Mac代码签名: Bypass the Keychain Access private key prompt from Terminal

macos - AppleScript 访问网络文件夹

ios - 如何从我的 iphone 连接到我的 mac os 上的本地主机

ios - UILabel 自动换行

objective-c - 在 NSWindowController 上按下 NSButton 时 EXC_BAD_ACCESS

macos - Java 7 是否只能在 x64 位 MAC OS X 上运行?

bash - Automator bash shell 脚本问题

pdf - 如何使用 AppleScript 和 Acrobat Pro 自动从 PDF 中提取页面?