I am .net programer for several years. I was looking for something new, new challenges. Becase of this (and of course willingness to try new platform) I decided to buy my first mac and started realize my ideas.
First application i have installed was XCode and the party has began ;)
Ok, let’s back to the main topic – how to add your own icon or text to the system status bar, which is Mac OSX equivalent of MS Windows’ tray bar or tray menu.
We want to create icon with menu options: start, stop and quit. Additionally we want that after click start menu item icon was replaced by time counter (like on the image below).


status bar menu with open menu items


So, let’s create new class with following header:

@interface SystemTray : NSObject {
	@private NSStatusItem *_systemTray;
	@private NSTimer *_timer;	

	@private NSDate *_startDate;
	@private NSTimeInterval _timeInterval;
}

- (IBAction)updateTime:(id)sender;

@end

NSStatusItem – this is the basic you need to add tray’s item
NSTimer – you need it if you would like to change content of your tray’s item in the given period of time.


@implementation SystemTray

- (void)quitApp:(id)sender {
	[NSApp terminate: sender];
}

- (void)start:(id)sender {

	_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
	…
	[_timer fire];
}

- (void)stop:(id)sender {
	_timeInterval = [_startDate timeIntervalSinceNow];
	[_timer invalidate];
	[_timer release];
}

- (IBAction)updateTime:(id)sender {
	...
	//refresh time count in NSStatusBar
	[_systemTray setTitle:[NSString stringWithFormat:@"%d:%d:%d", hours, minutes, seconds]];
}

- (void) createMyTrayBar  {
	NSZone *menuZone = [NSMenu menuZone];
	NSMenu *menu = [[NSMenu allocWithZone:menuZone] init];
	NSMenuItem *menuItem;

	menuItem = [menu addItemWithTitle:@"Start" action:@selector(start:) keyEquivalent:@""];
	[menuItem setTarget:self];

	menuItem = [menu addItemWithTitle:@"Stop" action:@selector(stop:) keyEquivalent:@""];
	[menuItem setTarget:self];

	[menu addItem:[NSMenuItem separatorItem]];

	menuItem = [menu addItemWithTitle:@"Quit" action:@selector(quitApp:) keyEquivalent:@""];
	[menuItem setTarget:self];

	_systemTray = [[[NSStatusBar systemStatusBar]
					statusItemWithLength:NSVariableStatusItemLength] retain];

	[_systemTray setMenu:menu];
	[_systemTray setHighlightMode:YES];
	[_systemTray setToolTip:@"Work time counter"];
	[_systemTray setTitle:@"☁☁"];

	[menu release];
}

- (void) applicationDidFinishLaunching:(NSNotification *) notification {

	//initialize your NSStatusBar
	[self createMyTrayBar];
}

-(void)dealloc
{
	if (_timer != NULL) {
		[_timer release];
	}

    [_systemTray release];
	[super dealloc];
}

@end

If you use NSSquareStatusItemLength for statusItemWithLength your status bar wan’t dynamically expand (the width will be fixed). In the example with time counter i use NSVariableStatusItemLength.