NSWorkspace launches in foreground
Okay, just in time to make another submission to Happy Apple Bug Friday (Bug #4506684): I have an application which consists of a GUI and a background daemon embedded in its bundle. Whenever you change the settings, it relaunches the daemon so it picks up its settings.
My first attempt used [[NSWorkspace sharedWorkspace] launchApplication: @"path/to.app"];. This worked fine except that every time the helper was launched, my app's front window was deactivated. Okay, so I guessed that it was launched like a GUI application and tried the showIcon: NO autolaunch: NO-variant. I tried autolaunch:YES as well, because the docs said something about daemons. No dice.
I finally resorted to LSOpenCFURLRef( (CFURLRef) [NSURL fileURLWithPath: @"path/to.app"], NULL ); and lived happily ever after.
Apparently, NSWorkspace launches all applications, even NSBGOnly and NSUIElement applications, as GUI apps, but the OS then can't activate them. What happens is that your app stays active, but its frontmost window loses focus. Very annoying to the user. To launch a daemon, you have to use Launch Services.
|Uli Kusterer writes:|
Because it was a background daemon that has a GUI, and NSTask doesn't correctly launch those (at least on some MacOS X releases I had to be compatible with).