Cocoa menubar programmatically with NSMenu, how to get standard items "Services", "Hide App", "Hide Others", "Show All" etc

时间:2018-08-22 13:56:04

标签: cocoa macos-carbon nsmenu

In decarbonizing I have now come to creating a menubar programmatically using NSMenu.

Carbon seems to be so nice to add standard items to the application menu: Services, Hide app, Hide Others, Show All, Quit app, and they are even added using the user’s Preferred Language setting in System Preferences’s Language & Region, so in German Hide Others is added as Andere ausblenden.

I only had to add the About and Preferences items.

However, it seems that in Cocoa I have to add these standard items myself, but how can I find out what Hide Others is called in the user’s Preferred Language ?

And what is the Cocoa equivalent of CreateStandardWindowMenu ?

1 个答案:

答案 0 :(得分:1)

如果您使Xcode创建一个新的Cocoa应用程序项目,它将创建一个MainMenu.xib,您可以对其进行检查。看起来“隐藏其他”,“退出”等只是分配了特定操作的菜单项,但是“服务”似乎具有一些特殊的魔力。即使您不想从笔尖显示整个菜单栏,也可以从笔尖加载它,然后挑选某些菜单项并将其移至另一个菜单,对其进行重命名或其他操作。尽管我不确定我是否了解您要完成的工作。即使您不想本地化,也可以使用笔尖。

要解决修改后的问题的最后一部分,NSApplication对象具有windowsMenu属性,即,您可以创建菜单并说出[NSApp setWindowsMenu: myMenu]。但是我不知道这是否会添加诸如“将所有东西放到最前面”之类的内容,还是您必须自己创建这些内容。