Re: Aperture Edit Plugin sandbox NSTask workaround?
Re: Aperture Edit Plugin sandbox NSTask workaround?
- Subject: Re: Aperture Edit Plugin sandbox NSTask workaround?
- From: Paul Miller <email@hidden>
- Date: Fri, 17 May 2013 16:25:31 -0500
- Organization: FXTECH
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.
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.
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;
NSDictionary *attrs = [[NSFileManager defaultManager]
attributesOfItemAtPath:editedPath error:&error];
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"];
NSArray *runningApps = [NSRunningApplication
runningApplicationsWithBundleIdentifier:appBundleID];
NSRunningApplication *app = [runningApps objectAtIndex:0];
// poll until it quits
while (!app.terminated)
{
sleep(1);
// spin event loop
NSEvent *event;
do
{
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:nil inMode:NSModalPanelRunLoopMode dequeue:YES];
[NSApp sendEvent: event];
}
while (event != nil);
}
// check to see if the file has changed
attrs = [[NSFileManager defaultManager]
attributesOfItemAtPath:editedPath error:&error];
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 (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden