Keeping your installations running forever on OSX

August 27th, 2013

Recently I had to build a Kiosk style app for a client, implementing Kinect gesture tracking. I decided to use OpenNI for this, but being a little bit unstable, the app was crashing from time to time, and because I live in Bulgaria and the client is in Canada, it was really hard for me to debug the problem and also the crashes were rare and unpredictable. So I decided just to restart the app everytime it crashes.

At first I just made another app, which sole purpose was to “ping” the main app via OSC and if the app does not “pong” back, it will just run the main app again.

// Basic code to run the app
string theApp = "PATH_TO_APP";
string command = "/usr/bin/open \""+theApp+"\"";
system(command.c_str()); 

It worked ok, without actually crashing the main app. But here is the problem, when the app crashes, it will bring up the Crash Report pop-up and until someone dismiss it, you can not launch the app again. So the solution is to disable the Crash Report dialog, which you can do through Terminal with the following commands:

defaults write com.apple.CrashReporter DialogType none
sudo defaults write com.apple.CrashReporter DialogType none

To be honest, I am not exactly sure, if you should run this as root or just as your user, but just in case, you could do both. Have in mind that this will take effect after couple of minutes.

Ok after fixing the Crash Report issue, one more thing we must do is to keep the app on top of everything. If you are using the app in fullscreen mode, you could sort this out with just running the app in full screen on startup, but in my case the app was running in window mode, so in order to keep it on top, we must use the power of Apple Script. I could not find a different solution, NSWindow setLevel: did not work for some reason.

// I found this on Openframeworks Forum: http://forum.openframeworks.cc/index.php?topic=4764.0
// so the full credit goes to "stephanschulz"

string myAppName = "AppName";
char myScript[255];
sprintf(myScript, "osascript -e 'tell app \"System Events\"' -e 'set theApps to every process whose name contains \"%s\"' -e 'set theApp to item 1 of theApps' -e 'set frontmost of theApp to true' -e 'end tell'",myAppName.c_str());
std::system(myScript);

Run the above function every 5 minutes, so you could be sure, if any system pop-ups appears, the app will go on top of them. At last you could use the ofSetWindowPosition(0,0) (if you are using Openframeworks) to place the app’s window at a constant position everytime it starts.

If anybody has some other methods to keep their apps running, please comment below or drop me a line!

Leave a Reply

Your email address will not be published. Required fields are marked *