My CodeBox app for Mac OS X has got an embedded helper app named CodeBar. It is a small utility resided in the system status bar. In this blog post, I'm going to share an open-source project demonstrating how to implement a custom status icon with a popup window. I hope it will be useful for those who are new to Cocoa and want to make their own utility living in the status bar.
First, we have to allocate a new NSStatusItem to place it into the menu bar. This task is managed by the controller class named MenubarController. To make status item more flexible, we use a custom StatusItemView to display its contents in the menu bar. The demo project provides the simplest implementation that draws a Star icon in that view. You can have even more advanced icon by adding handling of drag & drop or any other feature supported by regular views.
Next, we need a customized window to display it as a popover. In the demo project, this window is a panel managed by the PanelController class. It loads the panel component from NIB and manages its live cycle. Also, it configures popover appearance by removing title bar and standard window background.
Finally, once we have menu bar icon and popover, it is time to connect them. The top level controller class ApplicationDelegate is watching for clicks in the menu bar and passes them to the panel controller. Then, after panel controller is closed, application delegate asks menu bar icon to remove a blue highlight.
Popup project on GitHub has got all source files that you need to build a demo app in Xcode 4.
P. S. In Lion, Apple is adding a new class for popovers like in iOS. So, after OS X 10.7 is released, you would better to rely on native Cocoa classes where it is possible. In other cases, the Popup project should still be usable.
Update: What a shame. It looks like implementation using NSPopover requires a lot of hacks. Please find more information here.