简体   繁体   中英

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).

Looking at the nib file for standard Cocoa app, the services menu is defined like this:

<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.

What magic is happening here?

You find the Services menu on NSApplication.

-[NSApplication servicesMenu]

See documentation .

For future reference, based on @catlan's answer here's some code...

// 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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