7

I've submitted a helper application (using LSUIElement)to the Mac App Store. I was under the false impression that the App Store install process would put a dock icon for helper apps.

How can I create a dock icon that the user could remove, while the status bar app runs independently (like the popular app Caffeine)? Do I need to create a non-LSUIElement app that loads the LSUIElement app, or is there a better way?

akaru
  • 6,299
  • 9
  • 63
  • 102
  • See my comment to similar SO question: https://stackoverflow.com/a/68057340/1418981. Tested on macOS 11. – Vlad Jun 20 '21 at 15:27

2 Answers2

24

Instead of using LSUIElement, use NSApplication's setActivationPolicy: method. By default, the application will have a dock icon, but by changing the activation policy to NSApplicationActivationPolicyAccessory, you get the same effect as LSUIElement while being able to change it programatically (the documentation for NSApplicationActivationPolicyAccessory says it is equivalent to LSUIElement=1).

- (void)applicationDidFinishLaunching:(NSApplication *)app {
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"HideDockIcon"])
        [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • Would that allow the user to remove the icon from the dock while the app is running? – akaru Mar 22 '11 at 04:05
  • Yes, you just need to call setActivationPolicy: whenever you want to change it. – ughoavgfhw Mar 22 '11 at 17:49
  • 7
    -1: doc says "Currently, `NSApplicationActivationPolicyNone` and `NSApplicationActivationPolicyAccessory` may be changed to `NSApplicationActivationPolicyRegular`, but other modifications are not supported." – Richard Jun 15 '11 at 12:58
  • 8
    Richard is correct, you can't programmatically set the policy to NSApplicationActivationPolicyAccessory, but you can go the other way with it: set LSUIElement to 1 in the .plist, and then (optionally) set the policy to NSApplicationActivationPolicyRegular to show a dock icon. – zpasternack Jan 24 '12 at 04:21
  • `NSApplicationActivationPolicyAccessory` did not work for me but `NSApplicationActivationPolicyProhibited` did. – Albert Feb 09 '12 at 23:56
  • The header files say diffrently now: "In OS X 10.9, any policy may be set;" The docs still have @Richards comment. – Klaas Sep 26 '16 at 12:47
1

Apparently I was misinformed by my app reviewer (two of them actually). The dock icon is created for you by the install process. Pressing the issue, I was able to get the app through the review process.

akaru
  • 6,299
  • 9
  • 63
  • 102
  • This now behaves differently in Lion, where no icon is created in the Dock, only in LaunchPad. – akaru Aug 17 '11 at 22:13