jsherk
jsherk

Reputation: 6472

How to create a menu bar item that will launch an app?

Ok, I have created a little utility with AppleScript and used Automator to turn it into an application (MyApp.app). I would like to create a Menu Bar item (menulet, menu extra) that simply launches MyApp.app when you click on it.

I understand I need to create some kind of .menu file that goes in /System/Library/CoreServices/Menu Extras folder.

I have Xcode setup, but not really sure where I need to start. All my googling has turned up is how to add and remove existing menulets already in the Menu Extras folder.

Any hints, tips, tutorials or code appreciated!

EDIT: I do NOT want to run the AppleScript from the little Script Menu Bar item... I want a separate menu bar item that will launch the application.

EDIT: I am talking about the Menu Bar up in the top right corner, not the Dock.

EDIT: I want to be able to create a shortcut or a quick launch button to an application, so that one click on the icon in the menu bar will launch the application. I do not want or need any drop down menus associated with the menu bar item ... one-click = launch application.

Upvotes: 1

Views: 6715

Answers (2)

Dr.Kameleon
Dr.Kameleon

Reputation: 22810

Step-by-step :

  1. Set Application is agent (UIElement) to YES in your in info.plist file
  2. Get UKLoginItemRegistry from Uli Kusterer's site, if you want the menu to show up at LogOn (optional; but that's the way you'll most likely want to do it)
  3. Implement an NSMenu (with the items you need) in your XIB and connect it as an outlet (statusMenu) to your main App Delegate
  4. Set the Status Bar Menu in your AppDelegate.m file, like this :

- (BOOL)isAppSetToRunAtLogon {
    int ret = [UKLoginItemRegistry indexForLoginItemWithPath:[[NSBundle mainBundle] bundlePath]];
    return (ret >= 0);
}

- (void) runAtLogon
{
    [UKLoginItemRegistry addLoginItemWithPath:[[NSBundle mainBundle] bundlePath] hideIt: NO];
}

- (void) removeFromLogon
{
    [UKLoginItemRegistry removeLoginItemWithPath:[[NSBundle mainBundle] bundlePath]];
}

-(void)awakeFromNib {

    if (![self isAppSetToRunAtLogon])
    {
        [self runAtLogon];
    }

    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
    [statusItem setMenu:statusMenu];

    NSImage* statusImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"YOUR_STATUS_MENU_ICON" ofType:@"icns"]];

    [statusImage setSize:NSMakeSize(20, 20)];

    [statusItem setImage:[statusImage retain]];

    [statusItem setHighlightMode:YES];
}

And... that's it! :-)

Upvotes: 2

user866649
user866649

Reputation:

You are looking for a status item - see Status Bar Programming Topics. An older (Xcode 3, but there isn't much to it) tutorial can be found here.

Upvotes: 1

Related Questions