The Shape of Everything
A website mostly about Mac stuff, written by August "Gus" Mueller
» Acorn
» Retrobatch
» Mastodon
» Micro.blog
» Instagram
» Github
» Maybe Pizza?
» Archives
» Feed
» Micro feed
June 30, 2008
(This post is from my old, old, super old site. My views have changed over the years, hopefully my writing has improved, and there is now more than a handful of folks reading my site. Enjoy.)

A little while ago I shared a snip of code I put in VoodooPad development builds, which beeps at me when I launch a second copy. I run into this problem because I use VoodooPad all day long, and - well, since I'm VoodooPad's creator, sometimes I'll accidently get multiple copies running which I don't want.

Anyway.

I've got a better version now (and I don't know why I didn't do this the first time either); use AppleScript to quit the old version and then continue on the app's merry way. I put this in my main.m file, before NSApplicationMain():

#ifdef DEBUG

NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; NSArray *launchedApplications = [workspace launchedApplications]; for (NSDictionary *junk in launchedApplications) {   if ([@"com.flyingmeat.VoodooPad_Pro" isEqualToString:[junk objectForKey:@"NSApplicationBundleIdentifier"]]) {     NSLog(@"It's already running you moron");     system("/usr/bin/osascript -e 'tell app "VoodooPad Pro" to quit'");     sleep(1.5f); // let the quit actually happen.   } }

#endif

Yes yes yes, using system() is bad. I know. It's only ever run on my desktop though.