Relaunch app

How would you tell your app to quit and relaunch itself?


you need an external helper script or app to do this

Actually you can do this directly within the app.

I use a shell script to first kill, then open my application. What the following code does is use NSBundle to get both the bundle’s path (…/ and the executable’s path (…/

it then uses the executable path to get your application’s PID (process id), and uses that PID in creating the shell script to kill the app and open your application (using the bundle path).

Careful when/if you copy and paste, then compile in the code. The “\n” should change to a line break when compiled, so that the shell script is two lines, each a separate command.

Works for me everytime.

Killing a process in the shell is not good programming habit.

Anyway I don’t understand the purpose of relaunching an app externally.
It’s better to write a reset method

Can you explain why its not a good programming habit? And if there’s a non-kill method that can be shell or applescripted to quit an application?

I use the internal kill-relaunch because my application has a preference to switch LSUIelements, changing from a menubar only app (no dock icon) to a dock based application; after changing the LSUIelement the application needs to be relaunched to take affect. There are other applications have the exact same behavior, so there must be a way.

Whether or not killing an application is good programming practice, this works and hasn’t caused an error or issue yet.

IT IS important to note two things about this usage:

  1. If you Build and Run your application in XCode, this method won’t work while it is running through XCode. You have to build it, and manually open your application (double-click its icon in the finder) and then the method works.

  2. The application that I use this code for is not document based, and does not have to worry about saving and closing out data/document before quiting. If yours does, be careful to save and close your documents before running this code.

The AppleScript quit and the Cocoa [NSApp terminate:self ] command terminates an application controlled, that means, preferences and other housekeeping things are saved. There is no guarantee that killall does it, too. In case of a menulet that might not matter, but you should always write code as reliable as possible.

AFAIK to change the background status of an app it’s sufficient to set the value in info.plist and then update the modification date of the bundle, for example with this Cocoa method

+ (void)updateBundleModificationDate { [[NSFileManager defaultManager] changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath:[[NSBundle mainBundle] bundlePath]]; }