I have the following problem. There is a Cocoa app I have with UI, nib, etc. I need to be able to start the application in background or in foreground. In other words, I need NSApplicationActivationPolicyProhibited in the former and NSApplicationActivationPolicyRegular in the latter. Now...the decision of how to start depends on an argument pass to the startup of the app. So the code looks more or less like:
shouldBeBackground = // read this from argument - argv
if (shouldBeBackground) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
} else {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
// later on I do the makeKeyAndOrderFront:
}
Now the problem is with the Info.plist. If I put LSBackgroundOnly to 1 everything works as expected. But if I put it to 0 (or do not put the key), when the app starts it shows up the icon of the app in the dock for a couple of miliseconds and a little flash. I guess this is because the app is initialized with the default policy (starting to show the icon) and I switch it to background too late. This is also explained in: Cocoa - go to foreground/background programmatically
I know places WHERE to read the argument and do the previous IF even before I start cocoa or the app (for example, I can hook in MyOwnPrincipalClass +initialize), but even with that (even if I set the policy) it looks like the startup of Cocoa will set the default one. So it doesn't work.
The only solution I found is to put my IF at the beginning (before initialize cocoa) and depending on the argument, I write/modify the Info.plist (LSBackgroundOnly flag). This could work but it is a real hack. I would like to find a cleaner solution.
BTW, if I do the other way around: start up always as background and then make it foreground has a worst result since when going foreground the menubar of the app is not shown up automatically: you need to switch to another app and come back to make it appear....I searched in the internet and indeed it looks like a known bug/limitation.
BTW2: having 2 different binaries is not an option either.
Thanks for any help you can give me,