在NSUserAutomatorTask中设置变量

dav*_*idb 5 macos objective-c automator

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

AMWorkflow *task = [[AMWorkflow alloc] initWithContentsOfURL:scriptURL error:nil];
[task setValue:@"myValue" forVariableWithName:@"myVar"];
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法使用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]);
}];
Run Code Online (Sandbox Code Playgroud)

我在另一个答案(使用带有沙盒应用程序的AMWorkflow)中读到,忽略了NSUserAutomatorTask的"executeWithInput:"提供的值.变量是否也可能?

mar*_*nte 2

当你第一次在 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]);
}];
Run Code Online (Sandbox Code Playgroud)

该工作流程很简单,已经有一个名为“存储”的变量和一个从变量获取输入的列表中的选择。

在此输入图像描述

代码运行时的工作流程。

在此输入图像描述