2013-01-01 71 views
4

我正在將一些代碼從AMWorkflow遷移到NSUserAutomatorTask,以便最終可以對我的應用程序進行沙盒處理。我希望能夠在工作流程中設置現有變量的值作爲AMWorkflow是可能的:在NSUserAutomatorTask中設置變量

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被忽略。變量是否也可能?

回答

1

我沒有嘗試這個當你第一次張貼在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]); 
}]; 

}

工作流是一個簡單的一個已經有一個名爲存儲變量,然後從列表中,從變量獲取輸入一個選擇。

enter image description here

工作流中的動作時的代碼運行。

enter image description here

+0

感謝很多的範圍內是相同的 - 我會重新測試在10.8.4 – davidb

+0

你說得對 - 它現在「正常工作」。 – davidb

0

這可能會幫助 - 沒有嘗試過,但尋找相同的答案

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

的setValue:forVariableWithName: 設置與指定名稱的工作流程變量的值。

  • (BOOL)的setValue:(id)的值forVariableWithName:(的NSString *)VARIABLENAME 參數 值 的價值爲命名的變量設置。

variableName 要爲其設置值的變量的名稱。

返回值 如果找到variableName並設置其值,則爲YES;否則不。

討論 如果未找到variableName指定的變量,則此方法不執行任何操作。

可用性 可在OS X v10.5及更高版本中使用。 聲明於 AMWorkflow.h valueForVariableWithName: 返回具有指定名稱的工作流變量的值。

  • (ID)valueForVariableWithName:(的NSString *)VARIABLENAME 參數 VARIABLENAME 變量名。

返回值 變量的值。如果找不到具有指定名稱的變量,則返回nil。

可用性 可在OS X v10.5及更高版本中使用。 也 見 - 的setValue:forVariableWithName: 宣佈 AMWorkflow.h writeToURL:錯誤

+0

這是所有好東西(事實上的作品),但我的問題是找到一個方式做沙盒應用程序,它限制了我NSUserAutomatorTask – davidb