Create a StatusBar App

A StatusBar App is what on the right side of the menu bar, it doesn't contain a main window.

It has been months since I decided to learn cocoa development, but in vain. Unlike python, the documentation of cocoa tortures me a lot.

I am still new to cocoa development. And this post will be part of my learning series. I didn't mean to teach you anything, on the contrary, it is a track of my learing. But it may be a little helpful to you.

StatusBar App

A StatusBar App is what on the right side of the menu bar, it doesn't contain a main window. For example, the volumn control is a StatusBar App.

Demo Time

It's the time to create a StatusBar App now.

  1. Open your Xcode (I am on Xcode 4.3.3)

  2. Create a cocoa application project

    create a project
  3. Name the project StatusBarApp

  4. Run for testing

Now you will get an App with window. However, our app is a StatusBar App, it has no window.

The final app should be like:

application

Menu

We will create the menu first.

  1. Drag a menu to the interface builder

    drag a menu
  2. Edit the menu item

    edit menu item
  3. Decorate the menu with seprator

    decorate with seprator
  4. Connect the menu to your code outlet, and name it statusMenu

    connect menu outlet

StatusBar

Create the statusBar property in your AppDelegate.h file:

@property (strong, nonatomic) NSStatusItem *statusBar;

Synthesize it in the AppDelegate.m file:

@synthesize statusBar = _statusBar;

Initialize the statusBar:

- (void) awakeFromNib {
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.statusBar.title = @"G";
// you can also set an image
//self.statusBar.image =
self.statusBar.menu = self.statusMenu;
self.statusBar.highlightMode = YES;
}

awakeFromNib is earlier than applicationDidFinishLaunching in the lifecycle.

App Go

Let's test this application.

  1. Run you app now. You will see a G in the menu bar.
  2. Delete the useless window in your MainMenu.xib, and run your app again.

It works! But it doesn't work the right way. It is on the dock, it shows the menu on the left.

Fix it in StatusBarApp-Info.plist, add a row:

Application is agent (UIElement)  = YES
connect menu outlet

Run your application again, it won't be on the dock, it won't show the menu.

Do More

But you can't quit the application, that could be annoying. We did have a Quit item on the application, but it won't work right now.

Fix it:

quit

Reference: