3

I want to create logical groups by separating out the menu items in my sub menu.

Similar to the open with sub menu in Mac.

How can I achieve this.

NSMenuItem Separator Item creates a blank space, I require the visual to be the same as in the image attached. enter image description here

Sandeep T D S
  • 481
  • 3
  • 15
  • The lines are Separator Items. How do you create the menu? – Willeke Mar 01 '17 at 17:23
  • `NSMenu* mainMenu = [[NSMenu alloc]init];` `NSMenuItem *mainItem = [[NSMenuItem alloc]init];` `[mainItem setTitle:NSLocalizedString(@"My Menu", nil)];` `[mainItem setImage:[NSApp applicationIconImage]];` `[mainMenu addItem:[self createOpenInWebMenuItem]];` `[mainItem setSubmenu:mainMenu];` `NSMenu *mainMenu = [[NSMenu alloc] initWithTitle:@""];` `[mainMenu addItem:mainItem];` `return mainMenu;` – Sandeep T D S Mar 03 '17 at 12:07
  • `NSMenuItem *openInWebMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"OPEN_IN_WEB", nil) action:@selector(` `handleMenuItemSelection:) keyEquivalent:@""];` `openInWebMenu.tag = MenuItemOpenInWeb;` `[openInWebMenu setObservationInfo:(__bridge void * _Nullable)([Util getSingleSelectedItemFilePath])];` `return openInWebMenu;` – Sandeep T D S Mar 03 '17 at 12:10
  • Can confirm that the same code that produce a separator in an App menu, shows as a blank space in the context (right click) Finder menu (Finder sync extension). – bauerMusic Sep 23 '20 at 10:43

1 Answers1

4

I guess is, you are searching for Submenu Item. Find it in the Interface Builder by searching for submenu.

enter image description here

After inserting in the desired menu you find a new entry called Item. Just edit these and or fill in other menu items as you like. This creates a menu structure as you asked for!

enter image description here

  • Edit *

I provided you a solution for your question. According to the Apple Docs Context menus are located in the views in which they belong. Call the method from the viewController `viewDidLoad. the selector method create as you need.

* Edit * Assign the created menu to the main menu. I did this in the viewDidLoad method. if you don't like to have it on the main menu on the top, just skip the two lines after the mainmenu comment. BTW the index is the position in which the menu get inserted in the mainmenu.

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] setMenu:[self contextMenu]];

    NSMenuItem *subMenu = [[NSMenuItem alloc] init];
    subMenu.submenu = [self contextMenu];

    // Add the menu in mainmenu as well
    NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
    [mainMenu insertItem:subMenu atIndex:3];
   }

-(NSMenu *)contextMenu {

    NSMenu *contextMenu = [[NSMenu alloc] initWithTitle:@"Contextmenu"];

    NSMenuItem *subMenuItem = [[NSMenuItem alloc] init];
    [subMenuItem setEnabled:YES];
    [subMenuItem setTarget:self];
    [subMenuItem setEnabled:YES];

    NSMenu *subMenu = [[NSMenu alloc] initWithTitle:@"Submenu"];
    subMenuItem.submenu = subMenu;
    [subMenuItem setTitle:[subMenu title]];

   // Creates the menu entries.
    NSMenuItem *menuItem1 = [[NSMenuItem alloc] initWithTitle:@"otherMenu" action:@selector(subMenuAction:) keyEquivalent:@""];
    NSMenuItem *menuItem2 = [[NSMenuItem alloc] initWithTitle:@"anotherMenu" action:@selector(subMenuAction:) keyEquivalent:@""];

 // Creates the separator.
   NSMenuItem *separator = [NSMenuItem separatorItem];

  [subMenu addItem:menuItem1];
  [subMenu addItem:separator];
  [subMenu addItem:menuItem2];


  [contextMenu addItem:subMenuItem];

  return contextMenu;

 }

- (void)subMenuAction:(id)sender {

}

I tested it and it works! Here the screenshot.enter image description here

Hope it helps. And when it does, use the green answered menu :-)

voltae
  • 582
  • 4
  • 11
  • Can we do this programmatically by using objective-c? I am creating a Cusotm Context Menu as a Finder Extension and so do not have an interface builder for the same. Also, my motive is to create a thin horizontal separator between menu items like the one you can see in your second screenshot between Open and Close in the second Menu. Thanks for the support. Cheers! – Sandeep T D S Mar 02 '17 at 05:51
  • Here is a complete solution for you. – voltae Mar 02 '17 at 09:44
  • I had tried using NS Separator Item, but this doesn't create a horizontal line but creates an empty space. I tried your code as well, but to the same result. Refer the images: [https://drive.google.com/drive/folders/0B4JsxIX3-8pNZW1GaEZFYnU4RkE?usp=sharing] – Sandeep T D S Mar 03 '17 at 11:49
  • The code is as shown above, NSSeparatorItem, i did not find in the docs. You can use [subMenu addItem:[NSMenuItem separatorItem]]; I tested the method and it works well on 10.11 – voltae Mar 03 '17 at 17:18
  • 1
    Hi @voltae, I still haven't been able to find a solution. I created a new project and used your code but still see a blank entry. – Sandeep T D S May 23 '17 at 10:10
  • http://www.cocoabuilder.com/archive/cocoa/126682-nsmenuitem-separatoritem-separator-showing-line-programatically.html – Sandeep T D S May 23 '17 at 10:10
  • I still am unable to create logical groups for the app context menu. Can you try creating a finder-sync extension and use this code and check if logical groups are created for you? – Sandeep T D S May 25 '17 at 11:12
  • 1
    Note: I Forgot to mention, this works for the main app, ( A Context Menu for the main app), but creates a blank entry in the finder sync extension. – Sandeep T D S May 25 '17 at 11:16
  • Ahh. Please see the [official Documentation](https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/Finder.html). – voltae May 25 '17 at 14:03
  • Yeah, sorry for not mentioning it earlier. The Documentation doesn't provide any specific information, hence have reached out to Apple directly. Thank you for your support. – Sandeep T D S May 26 '17 at 10:14