1

我正在尝试从沙盒 AppKit 应用程序执行 Automator 工作流程。

最小示例 + github repo

   NSOpenPanel * panel = [NSOpenPanel openPanel];
   [panel setAllowsMultipleSelection:NO];
   [panel setCanChooseFiles:YES];
   [panel setCanChooseDirectories:NO];
   [panel setAllowedFileTypes:[NSArray arrayWithObject: @"com.apple.automator-workflow"]];
    NSInteger result = [panel runModal];

    if (result == NSFileHandlingPanelOKButton) {
      NSURL * workflow = [[panel URLs]objectAtIndex:0];
      NSLog(@"selected url %@", workflow);
      NSError * error = nil;
      [AMWorkflow runWorkflowAtURL:workflow withInput:[NSArray arrayWithObject:workflow] error:&error];
      if(error) {
          NSLog(@"Error while executing workflow %@", [error localizedDescription]);
      }        
    }

根据我目前对 AMWorkflow API 的理解,我假设它使用 Mach IPC 在单独的 Automator Runner 进程中执行工作流。

这就是我向我的应用程序添加以下权利的原因:

   <key>com.apple.security.temporary-exception.mach-lookup.global-name</key>
   <array>
     <string>com.apple.AutomatorRunner</string>
     <string>com.apple.Automator</string>
   </array>

但显然 Automator Runner 尝试连接回调用应用程序,但失败并显示以下错误消息:

Automator Runner(2717) deny mach-lookup  /Users/pbrc/Library/Developer/Xcode/DerivedData/AMWorkflowCaller-arjgkslqihljquelyvybmpsnljrn/Build/Products/Debug/AMWorkf

0   libsystem_kernel.dylib          0x00007fff96ce9686 mach_msg_trap + 10
1   liblaunch.dylib                 0x00007fff8db637c4
2   liblaunch.dylib                 0x00007fff8db624d9 bootstrap_look_up3 + 69
3   liblaunch.dylib                 0x00007fff8db62609 bootstrap_look_up2 + 40
4   Foundation                      0x00007fff8f4acffe -[NSMachBootstrapServer portForName:options:] + 102
5   Foundation                      0x00007fff8f4b84cb +[NSConnection connectionWithRegisteredName:host:usingNameServer:] + 30
6   Automator Runner                0x0000000100001a51 -[AMRunnerDelegate processArguments] + 487

有任何想法吗?

4

1 回答 1

2

简单的答案是 AMWorkflow API 在沙盒应用程序中不起作用。最近引入了另一种 API,它适用于沙盒应用程序:

NSUserAutomatorTask executeWithInput:completionHandler:

使用此 API,您可以执行位于应用程序脚本文件夹中的自动化脚本:

/Users/USERNAME/Library/Application Scripts/BUNDLENAME.APPNAME

有一个重要的警告:尽管方法的“输入”参数没有输入将传递到 10.8.3 12D75 之前的 Mac OS 版本上的自动化工作流程(这是/曾经是一个错误):

NSUserAutomatorTask * task = [[NSUserAutomatorTask alloc] initWithURL:workflow error:&error];
if(error) {
    NSLog(@"Error while creating script task %@", [error localizedDescription]);
}

[task executeWithInput: @"this will never reach your workflow" completionHandler:^(id result, NSError *error){
    if(error)
        NSLog(@"Error while executing workflow %@", [error localizedDescription]);
}];
于 2012-11-11T08:50:14.257 回答