FROM : Michael Ash
DATE : Mon Mar 03 23:22:58 2008
On Mon, Mar 3, 2008 at 2:43 PM, Steven Degutis <<email_removed>> 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
DATE : Mon Mar 03 23:22:58 2008
On Mon, Mar 3, 2008 at 2:43 PM, Steven Degutis <<email_removed>> 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
| Related mails | Author | Date |
|---|---|---|
| Mattias Arrelid | Mar 3, 17:24 | |
| Nir Soffer | Mar 3, 18:14 | |
| Mattias Arrelid | Mar 3, 18:18 | |
| Jean-Daniel Dupas | Mar 3, 18:29 | |
| John Stiles | Mar 3, 18:38 | |
| Steven Degutis | Mar 3, 20:43 | |
| Michael Ash | Mar 3, 23:22 | |
| Steven Degutis | Mar 4, 02:48 | |
| Joe Ranieri | Mar 4, 03:27 | |
| Mattias Arrelid | Mar 4, 12:10 | |
| Michael Ash | Mar 4, 17:11 |






Cocoa mail archive

