Re: Aperture Edit Plugin sandbox NSTask workaround?
On 5/16/2013 10:42 AM, Paul Miller wrote: My Aperture plugin uses NSTask to launch a UI application, passing the image to edit on the command-line, then waits for the application to exit. This has worked forever, until Aperture was sandboxed. Here goes (error checking removed for brevity): // initial conditions NSString *editedPath = ... <path to edit file> NSString *cmdName = <path to my UI app> NSString *appBundleID = <bundle ID of my UI app> // get the timestamp for the editedPath NSError *error = nil; NSDate *startDate = [attrs fileModificationDate]; // launch application using Apple events int result = -1; NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; [workspace openFile:editedPath withApplication:cmdName]; // sleep for a bit to wait for the app to start up sleep(1); // now look for it based on its bundle ID (which I get from Info.plist) NSString *appBundleID = [[bundle infoDictionary] objectForKey:@"commandID"]; NSRunningApplication *app = [runningApps objectAtIndex:0]; // poll until it quits while (!app.terminated) { sleep(1); // spin event loop NSEvent *event; do { [NSApp sendEvent: event]; } while (event != nil); } // check to see if the file has changed NSDate *endDate = [attrs fileModificationDate]; if ([startDate isEqualTo:endDate]) [_editManager cancelEditSession]; else [_editManager endEditSession]; _______________________________________________ Do not post admin requests to the list. They will be ignored. Pro-apps-dev mailing list (Pro-apps-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/pro-apps-dev/site_archiver%40lists.a... This email sent to site_archiver@lists.apple.com After two days of hair pulling and suicidal thoughts, I wanted to share with everyone my workaround for this problem. Instead of (the possibly much more "elegant") going with XPC services, I implemented a hair-brained workaround involving NSWorkspace, NSRunningApplication, a little "polite" polling, some NSDate checks on the file being edited, and my signed (but unsandoxed UI app). NSTask and waitpid don't work in the sandbox, so I had to spin until the NSRunningApplication quit. NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:editedPath error:&error]; NSArray *runningApps = [NSRunningApplication runningApplicationsWithBundleIdentifier:appBundleID]; event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSModalPanelRunLoopMode dequeue:YES]; attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:editedPath error:&error];
participants (1)
-
Paul Miller