Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting variables in a NSUserAutomatorTask

I'm migrating some code from AMWorkflow to NSUserAutomatorTask so that ultimately I can sandbox my app. I'd like to be able to set the value of existing variables within the workflow as was possible in AMWorkflow with:

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

However I don't seem to be able to get something similar working with NSUserAutomatorTask. The only documentation I can find (the class reference) says supply the variables as an 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've read in another answer ( Using AMWorkflow with sandboxed app ) that the value supplied with "executeWithInput:" for NSUserAutomatorTask is ignored. Is it possible the variables are too?

like image 712
davidb Avatar asked Oct 22 '22 20:10

davidb


2 Answers

I did try this when you first posted in 10.8.3 and could not get it to work. I tried various things with no luck.

I am now on 10.8.4 and it now seems to work without any real changes to your basic 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 a simple one that that already has a Variable called Storage and a choose from list that gets its input from the variable.

enter image description here

The workflow in action when the code is run.

enter image description here

like image 185
markhunte Avatar answered Oct 24 '22 15:10

markhunte


This may help - not tried it yet but looking for 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 to set for the named variable.

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

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

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

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

- (id)valueForVariableWithName:(NSString *)variableName

Parameters variableName The variable name.

Return Value The value for the variable. Returns nil if no variable is found with the specified name.

Availability Available in OS X v10.5 and later. See Also – setValue:forVariableWithName: Declared In AMWorkflow.h writeToURL:error

like image 29
Tim Avatar answered Oct 24 '22 17:10

Tim