/ Published in: Objective C
The application that needs to be restarted must fork 'relaunch' with two arguments, then terminate. In Cocoa, the fork is easily achieved with a NSTask. The first argument must be the path to the application to relaunch. The second argument must be the process identifier of the terminating application. In Cocoa, you can get it with [[NSProcessInfo processInfo] processIdentifier], which is equivalent to getpid().
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// gcc -Wall -arch i386 -arch ppc -mmacosx-version-min=10.4 -Os -framework AppKit -o relaunch relaunch.m #import <AppKit/AppKit.h> { const char *executablePath; pid_t parentProcessId; } - (void) relaunch; @end @implementation TerminationListener - (id) initWithExecutablePath:(const char *)execPath parentProcessId:(pid_t)ppid { self = [super init]; if (self != nil) { executablePath = execPath; parentProcessId = ppid; // This adds the input source required by the run loop [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationDidTerminate:) name:NSWorkspaceDidTerminateApplicationNotification object:nil]; if (getppid() == 1) { // ppid is launchd (1) => parent terminated already [self relaunch]; } } return self; } { if (parentProcessId == [[[notification userInfo] valueForKey:@"NSApplicationProcessIdentifier"] intValue]) { // parent just terminated [self relaunch]; } } - (void) relaunch { } @end int main (int argc, const char * argv[]) { if (argc != 3) return EXIT_FAILURE; [[[TerminationListener alloc] initWithExecutablePath:argv[1] parentProcessId:atoi(argv[2])] autorelease]; [pool release]; return EXIT_SUCCESS; }