macos 如何为 Mac 创建菜单栏应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3409985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a Menubar application for Mac
提问by MacMac
EDIT: This is a nice ready-made menubar application here(github source) by this answer.
编辑:这是一个很好的现成的菜单栏的应用程序在这里(GitHub的源)通过这个答案。
I was wondering how to make a menubar application, what are the requirements for that to do so?
我想知道如何制作菜单栏应用程序,这样做有什么要求?
I saw a simple application for the menubar was to open links using your browser, I want to create something similar to that.
我看到一个简单的菜单栏应用程序是使用浏览器打开链接,我想创建类似的东西。
This is the application I like to make similar.
这是我喜欢制作的类似应用程序。
采纳答案by SteamTrout
NSStatusItemis what you are looking for. Also add LSUIElement with string value of 1 to your Info.plist to hide it from Dock.
NSStatusItem就是你要找的东西。还将字符串值为 1 的 LSUIElement 添加到您的 Info.plist 以将其从 Dock 中隐藏。
回答by Alex Gray
I've found Codebox's Popupto be a great starting point. It is ripe for forking on Github.
我发现Codebox 的 Popup是一个很好的起点。在 Github 上分叉的时机已经成熟。
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.
PS 在 Lion 中,Apple 正在为弹出框添加一个新类,就像在 iOS 中一样。因此,在 OS X 10.7 发布后,您最好尽可能依赖本机 Cocoa 类。在其他情况下,Popup 项目应该仍然可用。
回答by MatzFan
BitBaris an application on GitHubthat can "Put anything in your Mac OS X menu bar".
BitBar是GitHub 上的一个应用程序,可以“在 Mac OS X 菜单栏中放置任何内容”。
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.
它运行 shell 或其他可执行脚本(它称为插件 - 请参阅插件存储库中的许多示例)并在菜单栏中显示结果。您可以编写自己的插件,只需将其添加到“插件文件夹”即可运行。除了显示信息外,它还可以从您定义的插件菜单中以交互方式运行预定义的 bash 脚本。
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.
自从我第一次发布这个答案以来,它的受欢迎程度已经激增(目前有 52 个贡献者),现在甚至还有一个可分发版本,您可以使用它打包自己的插件。
A very simple (non-interactive) example to show live Bitcoin price:
一个非常简单的(非交互式)示例来显示实时比特币价格:
回答by taichino
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.
由于 Apple在 Yosemite 中添加了NSStatusBarButton
属性NSStatusItem
,我们可以更简单地实现菜单栏应用程序。我刚刚在 github 上创建了一个示例项目。
回答by funroll
FlyCutis another nice open source application that does this. (MIT licensed.) Very handy too, I use it several times a day.
FlyCut是另一个很好的开源应用程序。(MIT 许可。)也非常方便,我每天使用它几次。
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];
回答by lindon fox
Mail Notifris 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.
Mail Notifr是另一个开源菜单栏应用程序。它帮助了我很多,尤其是当我需要弄清楚如何实现登录时打开时。也可在App Store 上获得。