Menu Bar app being launched by Dock Icon (Mac OS X) - macos

I'm working on a Menu Bar app and I've added the LSUIElement so the icon won't appear in the dock when the app is in use (And also to remove the File, Edit, View etc at the left of the menu bar).
The problem is that I want the app to be launched by the Icon and by adding the LSUIElement the only way to luanch the app is thru xcode.
I've seen that in the Mac app Caffiene it launches by the icon but it doesn't appear in the dock that it's running etc which is excatly what I want/need.
So I was wondering if you guys know how to do this.
Thank you in advance!

If you want to have a dock icon but not show the app as running in the Dock, simply create two applications:
Status Menu Application
This is the actual application that sets up the menu and contains the application's logic. It has LSUIElement set.
Launcher Application
This application has a dock icon. It contains the status menu application in its bundle. Its sole job is to launch the status menu application and then quit itself.

There is a really simple way to do this without creating a separate launcher app. Build your application, which has a status item, as an app bundle. Then, add an entry to the application's Info.plist file
Key: NSUIElement
Value: Number, 1 or Boolean True
NSUIElement tells OSX if the app should display in the Dock or not.

Related

macOS / Qt: Disabling the "Quit" menu item on my application's dock menu

I've writing a macOS application with Qt. This application is a launch agent, meaning that it's launched by launchd and always running in the background. Normally the application only has a menu bar icon, and it doesn't have any open windows or a Dock icon. (i.e. the shared NSApplication instance's activationPolicy property is set to a value of NSApplicationActivationPolicyAccessory.)
However, there are a few menu items available in its menu bar item that open some windows, and when those windows are open the app switches to not being background-only any longer, so it will have a Dock icon and menu bar. (i.e. activationPolicy is changed to NSApplicationActivationPolicyRegular.) With there being a Dock icon, that means it's possible for the user to right-click it and open its menu, and that menu has its default menu item for quitting the application.
Since the app is a launch agent, though, and is intended to always be running, quitting it just causes launchd to relaunch the app. I'd like to disable or remove this menu item if possible, or otherwise prevent the user from being able to quit the app in this manner.
Is there any way to do that? If there's a way to accomplish this purely using Qt's functions that would be great, but if not it's fine for me to use macOS specific functions too.
I should add that because this is a Qt application, I can't use the same method as outlined here because I don't have access to the application's delegate. I would need to use another approach. (For example, it may be possible to swizzle methods on Qt's application delegate, though if there's a cleaner way to accomplish this than I'd much rather do that.)
After doing some more research, I've found that it's not possible to remove the "Quit" menu item from an application's Dock menu, or any of the other standard menu items there as they are created and handled by the Dock itself.
It is possible to stop an application from quitting when the user quits the application via the Dock. In a Qt application the method is to subclass QApplication and override its bool event(QEvent *) method. The overridden method should check for events of type QEvent::Close, call the ignore() method on the event, and then return true. Note though that this will stop the application from quitting via all other conventional methods as well.
edit: It is also possible to tell when the app is being quit via the Dock, at least when using Apple's native API. See: macOS: Is there any way to know when the user has tried to quit an application via its Dock icon?
By using Objective-C method swizzling it's possible to override the applicationShouldTerminate: method of Qt's application delegate and prevent a Qt app from being quit by the dock.

Keep status bar app from showing icon in the dock

I created a status bar app for the mac a few weeks ago. However, whenever I run the app, the dock icon shows up alone with the item in the status bar. Is there a way to keep the app from appearing in the dock?
I can't write comments but to what #Michael Dautermann wrote I'd suggest using the LSUIElement key.
You've said that it's a status bar app, so it has a UI.
LSUIElement (String - OS X) specifies whether the app runs as an agent app. If this key is set to “1”, Launch Services runs the app as an agent app. Agent apps do not appear in the Dock or in the Force Quit window. Although they typically run as background apps, they can come to the foreground to present a user interface if desired. A click on a window belonging to an agent app brings that app forward to handle events.
The Dock and loginwindow are two apps that run as agent apps.
Despite what the docs say it is a String so you can also set this as bool.
Yes, set the LSBackgroundOnly key in your app's "Info.plist" file.

Display NSPopover above dock icon

How can I display an NSPopover above my app's dock icon?
There are two ways to customise the dock menu, based on if your application is running or not. See the Apple documentation for customising it when its open here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW1
To summarise, you basically provide a menu in the main application's XIB or storyboard which contains the extra menu options that you want to add to the dock menu.
To customise your Dock icon's menu when the application is not running, see the guide here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/CreatingaDockTilePlug-in/CreatingaDockTilePlug-in.html
Basically, you need to create a plugin which you drop into your application's resource folder. This works very like the original method, but executes inside Finder's dock process.

How to create a helper application (LSUIElement) that also has a (removable) dock icon

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?
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];
}
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.

How can I write an autostarting dock app for the Mac?

I have an application I'd like to build that starts when you start the mac and will appear in the dock.
I have some experience with Objective-C and iPhone dev but none with Mac dev, I'm also a PC user normally so I'm not au fait with the norms of Mac usage. Any guidance here is appreciated.
How do you get an application to autostart?
*edit Can you run an app in the status bar? (with the clock etc) or do apps only run in the dock?
To write a program in the Status Bar check out this great tutorial.
To get an application to auto start on login, look for information on launch daemon or just make your application installer to place an entry on /Library/StartupItems.
About the dock: usually, user controls what's on his/her dock. When your UI application is running, it will usually be in the dock. If you want the icon to stick there permanently, you will need the user dragging the icon from the Finder to the dock, or, right-click on the dock icon while is running in select Options->Keep In Dock menu option.

Resources