Launching an app synchronously

When NetNewsWire sends a news item to a weblog editor app, it sends an Apple event to that app. But first it needs to make sure the app is running, so that it can receive the Apple event.

When I first added this feature, I had some weird thing where NetNewsWire launched the app then waited for a notification that the app had launched—and then it would send the Apple event. It took a surprising amount of code.

I decided that it was okay to launch the weblog editor app synchronously. Since -[NSWorkspace launchApplication:] is async, I needed something else. Turns out it was totally easy. Given f as the path to the app:

LSLaunchURLSpec launchSpec;
launchSpec.appURL = (CFURLRef)[NSURL fileURLWithPath:f];
launchSpec.itemURLs = nil;
launchSpec.passThruParams = nil;
launchSpec.launchFlags = kLSLaunchAndDisplayErrors;
launchSpec.asyncRefCon = nil;
return LSOpenFromURLSpec(&launchSpec, nil);

Anyway—so if you’re writing a newsreader or browser and need to launch a weblog editor before sending it the post-to-weblog Apple event, you can use the above code to launch the weblog editor. (Or use it for launching any app, of course.)

19 Dec 2006