Re: Relaunching an application
Re: Relaunching an application
- Subject: Re: Relaunching an application
- From: "Michael Ash" <email@hidden>
- Date: Mon, 3 Mar 2008 17:22:58 -0500
On Mon, Mar 3, 2008 at 2:43 PM, Steven Degutis <email@hidden> wrote:
> Perhaps a small application like relaunch.app could be imbedded into
> your relaunchable application, where all it pretty much does is serve
> as a buffer by relaunching your application reliably (through Cocoa
> methods) after your app calls it the same way you mentioned above.
>
> [[NSWorkspace sharedWorkspace] launchApplication:relauncherExecutablePath];
> [NSApp terminate:self];
>
> Then the relaunch.app program uses a method which calls itself
> continuously (like a loop that wont make your app unresponsive), which
> does a check to see if the original app is still running using
> NSWorkspace's -launchedApplications, and only after it sees your app
> end, then it will run the app again and terminate itself. This ensures
> that your app opens only after it ends first. And doing this would be
> quite easy, using two methods, -awakeFromNib and your custom method
> -hasAppEnded. The former would launch the second one immediately, and
> do nothing else, and the second one would do the check, and at the
> end, if it is still running (hasnt quit and run the new app) it would
> launch itself with [self performSelector:@selector(hasAppEnded)
> withObject:nil afterDelay:1.0]; or something. This is what I would do
> anyway. 1.0 might be too long or too short for a real application,
> though.
The best way is to write a little program like this (error checking
removed for brevity, typed in mail client, caveat emptor):
int main(int argc, char **argv)
{
char dummy;
read(STDIN_FILENO, &dummy, 1);
[NSAutoreleasePool new];
NSURL *url = [NSURL fileURLWithPath:[NSString
stringWithUTF8String:argv[1]]];
LSOpenCFURLRef((CFURLRef)url, NULL);
return 0;
}
Then invoke it using an NSTask in the main app:
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:pathToHelper];
[task setArguments:[NSArray arrayWithObject:[[NSBundle mainBundle]
bundlePath]]];
[task setStandardInput:[NSPipe pipe]];
[task launch];
And then quit the app. The subtask will block in the read waiting for
data on the pipe which will never come. When your app terminates, the
write end of the pipe closes, unblocking the read in the subtask. The
subtask then relaunches your app using the URL you gave it as a
parameter. It's fast, easy to use, and requires no polling or messy
shell scripts.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden