简体   繁体   English

以编程方式创建OS X“服务”菜单

[英]Programmatically creating the OS X “Services” menu

I'm working on a cross platform app that doesn't use NIB files and trying to figure out how to create the standard OS X "Services" menu (a submenu of the application menu in most applications). 我正在开发一个不使用NIB文件的跨平台应用程序,并试图弄清楚如何创建标准OS X“服务”菜单(大多数应用程序中应用程序菜单的子菜单)。

Looking at the nib file for standard Cocoa app, the services menu is defined like this: 查看标准Cocoa应用程序的nib文件,服务菜单的定义如下:

<menuItem title="Services" id="NMo-om-nkz">
    <modifierMask key="keyEquivalentModifierMask"/>
    <menu key="submenu" title="Services" systemMenu="services" id="hz9-B4-Xy5"/>
</menuItem>

Obviously the bit that makes it work is systemMenu="services" but I can't see how to programmitically create a NSMenu item like this - there's no "systemMenu" property on NSMenu. 显然,使它起作用的是systemMenu="services"但是我看不到如何以编程方式创建这样的NSMenu项-NSMenu上没有“ systemMenu”属性。

What magic is happening here? 这里发生了什么魔术?

You find the Services menu on NSApplication. 您可以在NSApplication上找到“服务”菜单。

-[NSApplication servicesMenu]

See documentation . 请参阅文档

For future reference, based on @catlan's answer here's some code... 供将来参考,基于@catlan的答案,这里有一些代码...

// Create the services menu
NSApp.servicesMenu = [[NSMenu alloc] init];

// Create menu item for it
NSMenuItem* servicesItem = [[NSMenuItem alloc] init];
servicesItem.title = @"Services";
servicesItem.submenu = NSApp.servicesMenu;

// Add it to the app menu
NSMenu* appMenu = [[NSApp mainMenu] itemAtIndex:0].submenu;
[appMenu addItem:servicesItem];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM