Re: Aperture Edit Plugin sandbox NSTask workaround?
Re: Aperture Edit Plugin sandbox NSTask workaround?
- Subject: Re: Aperture Edit Plugin sandbox NSTask workaround?
- From: Rainer Standke <email@hidden>
- Date: Sun, 26 May 2013 11:52:03 -0700
I am not all that experienced with any of this myself... but I wouldn't have expected this to work, because you are doing this while running in Aperture, and thus inside Aperture's sandbox, right? But hey, what works works!
I have a sandboxed app in development that is set to use a user script. Here is off the top of my head how that's supposed to work: A sandboxed app has access to folder where the user can place a script that's specific to the sandboxed app. But since the user has to install / drag the script there manually, from outside the app, that is taken to be enough of a manifest user intent that the script gets to run outside the sandbox. Communication with the user script happens via passed parameters, and returned values. I plan to tell the users inside my sandboxed app to download and run an installer that would install the script for them. The nifty thing is that the script can be a unix executable, much like any command line tool. This means I even get to write this in Objective C...
Just thought I'd share... (I got much of this from a WWDC video from last year, the one about scripting.)
Rainer
On May 17, 2013, at 14:25, Paul Miller <email@hidden> wrote:
> 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
_______________________________________________
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