Creating a Mac OS X status bar application is really straightforward. However, below you will find a condensed step-by-step tutorial to avoid unnecessary mistakes.
- Create a new Xcode Cocoa Application project
- Go to
Info.plist
file and add the key Application is agent (UIElement) and set it toYES
<key>LSUIElement<key> <true/>
- Add two new properties to your app delegate header file. These two properties will contain the main status item displayed in the status bar and its associated
NSMenu
menu:@property (strong, nonatomic) IBOutlet NSMenu *statusMenu; @property (strong, nonatomic) NSStatusItem *statusItem;
- Open MainMenu.xib and add new
NSMenu
Control. Connect theNSMenu
control with yourstatusMenu
outlet in your app delegate. Finally delete the window control and save the nib file. - Go to applicationDidFinishLaunching: or awakeFromNib method of your app delegate.
- Add the following code to create your status item, set the title (which will be displayed in the status bar), add the status menu to your status item and activate the highlighting mode.
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; [self.statusItem setMenu:self.statusMenu]; [self.statusItem setTitle:@"My App"]; [self.statusItem setHighlightMode:YES];
- Run the application and you should see My App on your status bar. By activating the status item you will see the three default menu items too.
Leave a Reply