Re: Relaunching App Programmatically - Terminal Window Shows up
Re: Relaunching App Programmatically - Terminal Window Shows up
- Subject: Re: Relaunching App Programmatically - Terminal Window Shows up
- From: Michael Ash <email@hidden>
- Date: Fri, 6 Feb 2009 10:30:13 -0500
On Fri, Feb 6, 2009 at 8:06 AM, email@hidden
<email@hidden> wrote:
> You don't need a helper app for this.
>
> - (void) relaunch
> {
> int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
> NSString *myPath = [NSString stringWithFormat:@"%s",
> [[[NSBundle mainBundle] executablePath] fileSystemRepresentation]];
> [NSTask launchedTaskWithLaunchPath:myPath arguments:[NSArray
> arrayWithObject:[NSString stringWithFormat:@"%d",
> processIdentifier]]];
> [NSApp terminate:self];
> }
If the user has added your application to the Dock permanently, this
will create a new instance of the application with a second icon, then
quit the first, without merging them. The result: one icon for a
non-running application, and a second icon for a running application.
Not good.
My preferred approach is to create a very small tool like this:
int main(int argc, char **argv) {
char dummy;
read(STDIN_FILENO, &dummy, 1);
CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL,
argv[1], strlen(argv[1]), 0);
LSOpenCFURLRef(url, NULL);
return 0;
}
You can then launch it from the parent application, passing the path
to your .app as the only parameter, and giving it an NSPipe for
standard input. When you quit, the pipe closes and the read()
unblocks. It then relaunches your application using LaunchServices,
and you're back in business.
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