开发者

How to create a Menubar application for Mac

开发者 https://www.devze.com 2023-01-10 18:46 出处:网络
EDIT: This is a nice ready-made menubar application here (github source) by this answer. I was wondering how to make a menubar application, what are the requirements for that to do so?

EDIT: This is a nice ready-made menubar application here (github source) by this answer.


I was wondering how to make a menubar application, what are the requirements for that to do so?

I saw a si开发者_StackOverflowmple application for the menubar was to open links using your browser, I want to create something similar to that.

How to create a Menubar application for Mac

This is the application I like to make similar.


NSStatusItem is what you are looking for. Also add LSUIElement with string value of 1 to your Info.plist to hide it from Dock.


I've found Codebox's Popup to be a great starting point. It is ripe for forking on Github.

How to create a Menubar application for Mac

Though it works nicely, they do note on their site...

P. S. In Lion, Apple is adding a new class for popovers like in iOS. So, after OS X 10.7 is released, you would better to rely on native Cocoa classes where it is possible. In other cases, the Popup project should still be usable.


BitBar is an application on GitHub that can "Put anything in your Mac OS X menu bar".

It runs shell or other executable scripts (which it calls Plugins - see the many examples in the plugins repo) and displays the results in the menu bar. You can write your own plugin and have it run simply by adding it to the 'Plugins folder'. As well as displaying information, it can also run pre-defined bash scripts interactively from the plugin menus you define.

Since I first posted this answer it's popularity has exploded (52 contributors currently) and there is now even a distributable version with which you can package your own plugins.

A very simple (non-interactive) example to show live Bitcoin price:

How to create a Menubar application for Mac


As Apple added NSStatusBarButton property to NSStatusItem in Yosemite, we can implement menubar app a lot simpler. I just created a sample project on github.

https://github.com/taichino/PopupTest


FlyCut is another nice open source application that does this. (MIT licensed.) Very handy too, I use it several times a day.

Here's some code that seems like it may be relevant:

    // Flycut/AppController.h
    IBOutlet NSMenu *jcMenu;

    // Flycut/AppController.m
    statusItem = [[[NSStatusBar systemStatusBar]
            statusItemWithLength:NSVariableStatusItemLength] retain];
    [statusItem setHighlightMode:YES];

    if ( [[DBUserDefaults standardUserDefaults] integerForKey:@"menuIcon"] == 1 ) {
        [statusItem setTitle:[NSString stringWithFormat:@"%C",0x2704]]; 
    } else if ( [[DBUserDefaults standardUserDefaults] integerForKey:@"menuIcon"] == 2 ) {
        [statusItem setTitle:[NSString stringWithFormat:@"%C",0x2702]]; 
    } else {
        [statusItem setImage:[NSImage imageNamed:@"com.generalarcade.flycut.16.png"]];
    }
    [statusItem setMenu:jcMenu];
    [statusItem setEnabled:YES];


Mail Notifr is another open source Menubar app. It helped me a bunch, especially when I needed to figure out how to implement the open on login. Also available on the App Store.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号