我正在将一些代码从 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 的变量和一个从变量中获取输入的列表。
代码运行时的工作流。
关于objective-c - 在 NSUserAutomatorTask 中设置变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111994/