5

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?

Community
  • 1
  • 1
davidb
  • 357
  • 2
  • 9
  • I added a similar question here. I want to set variables, but only if they are supplied in the workflow itself. Using `NSUserAutomatorTask` the workflow will not run if the variables are not set in the workflow. https://stackoverflow.com/questions/52507620/setting-nsuserautomatortask-variables-without-requiring-automator-workflows-to-d – pkamb Sep 26 '18 at 01:57

2 Answers2

2

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
markhunte
  • 6,805
  • 2
  • 25
  • 44
1

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

pkamb
  • 33,281
  • 23
  • 160
  • 191
Tim
  • 11
  • 1
  • 1
    That's all good stuff (and in fact works) but my problem is finding a way to do the same within the confines of a sandboxed app which restricts me to NSUserAutomatorTask – davidb Mar 07 '13 at 17:52