Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Cocoa application with MenuBar Icon. How To:?

Tags:

xcode

macos

cocoa

I'm trying to make a Cocoa application that's pretty simple. I have three windows with three messages on them, that's all there is to it. What I'm trying to do is this:

The user runs the app, the app icon appears in the menu bar and that's all that happens, no menu and no dock icon

Then, the user can click the MenuBar Icon and have a dropdown list and choose from the three available messages.

I know it's useless, but this is literally my first application and I can't figure out how to get NSStatusItem to work properly...

I've looked around and found some tutorials but I can't seem to follow any of them...any help?

like image 455
Zrb0529 Avatar asked Jan 21 '11 06:01

Zrb0529


1 Answers

  1. Regarding the "no dock icon" - add boolean LSUIElement entry to Info.plist file and set it to true. This won't show application in app switcher UI (cmd+tab) as well.
  2. Adding menu bar icon is as much as looking into NSStatusBar and NSStatusItem documentation and using example code there:

.

// this one is taken from apple documentation
- (void)activateStatusMenu {
    NSStatusBar *bar = [NSStatusBar systemStatusBar];

    theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
    [theItem retain];

    [theItem setTitle: NSLocalizedString(@"Tablet",@"")];
    [theItem setHighlightMode:YES];
    [theItem setMenu:theMenu];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [self activateStatusMenu];
}

Update Since ARC does not allow retain calls in the code, I managed to solve the issue by creating theItem as __strong instance variable of the class where the item is being created.

like image 122
Eimantas Avatar answered Oct 24 '22 13:10

Eimantas