Status Bar App
How to create a status bar app using Xcode and Objective-C on Mac

The following example has been tested using Xcode 5.1.1 on Mac OS X 10.9.3 . But it may likely works fine on other versions as well. Your comments or ideas on how to simplify this further are more than welcome!

See also the Swift version.

Create a new empty Project

From Xcode create a new OS X project of type "Cocoa Application":



Set up the MainMenu.xib interface

To start with you may remove the default Window object (you won't need it for this project):



Then you can add (ctrl+drag) a new Menu object to the application:



Please do not remove the default "Main Menu" object from the application, otherwise keyboard shortcuts as ⌘C (copy) and ⌘V (paste) won't work anymore throughout the application!

Set the description and the shortcut key of the last menu option to "Quit" and use ⌘Q as a shortcut key:



Change the header and implementation of the app delegate

Add a statusBarMenu outlet and a statusBarItem variable to the AppDelegate.h file:

@interface AppDelegate : NSObject {
   IBOutlet NSMenu *statusBarMenu;
   NSStatusItem * statusBarItem;
}
@end

Add the following code in the applicationDidFinishLaunching method of the AppDelegate.m file:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   // Insert code here to initialize your application
   statusBarItem = [[NSStatusBar systemStatusBar]
                     statusItemWithLength:NSVariableStatusItemLength];
   [statusBarItem setMenu:statusBarMenu];
   [statusBarItem setTitle:@"It's me!"];
   [statusBarItem setHighlightMode:YES];
}

Make all necessary connections

First, add the "terminate:" response to the "Quit" menu item. To do this, ctrl+drag from the menu item to the first responder and then choose option "terminate:" from the drop down list menu that will appear:





Then connect the outlet in the AppDelegate.h with the menu object. Simply ctrl+drag from the little circle next to the IBOutlet variable to the menu object:



Change info.plist

Set the application as an agent app, so that it won't appear in the Dock or in the Force Quit window. To do this add the key Application is agent (UIElement) to the application info.plist file and set it to YES:



Run the application

As you run the application from Xcode it should immediately show on the Mac status bar:



Tips

Use an icon, in place of text, to be shown on the status ba.
Add the following lines of code in the applicationDidFinishLaunching method of the AppDelegate.m file:

[statusBarItem setTitle:@""];
[statusBarItem setImage:[NSImage imageNamed:@"StatusIconNormal"]];
[statusBarItem setAlternateImage:[NSImage imageNamed:@"StatusIconHighlighted"]];


  • Xcode Project Download

Gaetano Causio © | Privacy Policy | Disclaimer