Setting variables in NSUserAutomatorTask

I am porting some code from AMWorkflow to NSUserAutomatorTask so that in the end I can isolate my application. I would like to be able to set the value of existing variables in a workflow, as was possible in AMWorkflow, with:

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

However, I can't seem to get something like this while working with NSUserAutomatorTask. The only documentation I can find (class reference) indicates that the variables are NSDictionary.

So, I'm trying something like:

 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]); }]; 

I read in another answer ( Using AMWorkflow with sandboxed software ) that the value specified in "executeWithInput:" for NSUserAutomatorTask is ignored. Is it possible that variables too?

+4
source share
2 answers

I tried this when you first published in 10.8.3 and couldn't make it work. I tried different things with no luck.

Now I'm on 10.8.4, and now it works without any real changes in your base code.

  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]); }]; 

}

The workflow is simple, which already has a variable called Storage and a choice from the list that gets its input from the variable.

enter image description here

Workflow in action when running code.

enter image description here

+1
source

It may help - have not tried it yet, but looking for the same answer

https://developer.apple.com/library/mac/#documentation/AppleApplications/Reference/AMWorkflow_class/Reference/Reference.html#//apple_ref/occ/cl/AMWorkflow

SetValue: forVariableWithName: Sets the value of the workflow variable with the specified name.

  • (BOOL) setValue: (id) value forVariableWithName: (NSString *) variableName parameters value The value for the specified named variable.

variableName The name of the variable to set the value for.

Return value YES if variableName was found and its value is set; otherwise NO.

Discussion This method does nothing if the variable specified by variable Name is not found.

Availability Available in OS X v10.5 and later. Declared in AMWorkflow.h valueForVariableWithName: Returns the value of a workflow variable with the specified name.

  • (id) valueForVariableWithName: (NSString *) variableName parameters nameVariable The name of the variable.

Return Value The value of the variable. Returns nil if the variable is not found with the specified name.

Availability Available in OS X v10.5 and later. See also - setValue: forVariableWithName: Announced in AMWorkflow.h writeToURL: error

0
source

All Articles