Scripting help: Add separator in menu

Hello, I’m afraid I am having trouble figuring out how to correctly add a menu item separator in a plugin:
image
How do I do this correctly? Using "name": "-" like in Vanilla doesn’t work, neither does an empty string (as you can see).

Is there any reference I can check for writing different menu items? Especially a fling-out menu would be a great help. Thanks!

For context, I have the following:

def conditionalContextMenus(self):

	if self.condition_met:
		
		conditionalMenus = [
			{"name": Glyphs.localize({"en": u"Show affected glyphs"}),
			 "action": self.openTabWithAffectedGlyphs_},
			# add a divider
			{"name": "", "disabled": True, "action": None}
			# add a submenu (fling-out)??
		]
		# Return list of context menu items
		return conditionalMenus

Try adding an item like so:

{ "menu": NSMenuItem.separatorItem() }

Thank you! That works perfectly. Do you have any guidance on how to add a submenu?

Same thing, but the NSMenuItem gets a submenu. See

Something like:

submenu = NSMenu.new()
# fill submenu
item = NSMenuItem.new()
item.setTitle_("Some Title")
item.setSubmenu_(submenu)
{ "menu": item }

Thanks! Got it to work.

May I ask how to add a submenu to the plugin’s menu this way?

I get the code above, but can’t figure out how to find that menu, since we define it as menuName in settings:

def settings(self):
	self.menuName = Glyphs.localize({
		'en': 'My Plugin’,
	})

Is this a filter plug-in where you would like to add a submenu to the menu item that activates the plug-in?

The self.menuName is only the default API that can be used by some plugins.

You need to get the main menu and from there drill down to the place you like to put your menu and add it yourself. I’ll send some sample code, later.

1 Like

Yes, a filter and I’d like to have a few options (apply, report, add the customParameter).

In that case it’s best to add a new menu item and add the submenu there. Having a submenu on the same menu item that opens the filter is fairly confusing and thus seldomly done.

I mean it doesn’t open a window, just these options. Is it safe to set self.menuName = None and add a new menu item instead of it? Feels hacky

The general plugin has a small sample: GlyphsSDK/Python Templates/General Plugin at Glyphs3 · schriftgestalt/GlyphsSDK · GitHub

But you need to dig a bit deeper:

newItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Some Name", None, "")
Glyphs.menu[PATH_MENU].submenu().itemWithTitle_("Other").append(newItem)

But this only works with the English localization. You can try:

newItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_("Some Name", None, "")
Glyphs.menu[PATH_MENU].submenu().itemArray().lastObject().append(newItem)

That break if some other plugin adds an item after the “Other” item.

1 Like

I have something for that, I will share the code tomorrow

# Create main menu item with submenu

    mainMenuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        "YourMenuName", None, ""
    )
  
    # Create submenu
    submenu = NSMenu.alloc().init()
  
    # Add submenu items
    subMenu1 = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        "YourFirstSubMenu", "NameOfYourCallback", ""
    )
    subMenu1.setTarget_(self)
    submenu.addItem_(subMenu1)
  
    # Add separator
    submenu.addItem_(NSMenuItem.separatorItem())
  
    # Add submenu items
    subMenu2 = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        "YourSecondSubMenu", "NameOfYourCallback", ""
    )
    subMenu2.setTarget_(self)
    submenu.addItem_(subMenu2)
  
    # Attach submenu to main menu item
    mainMenuItem.setSubmenu_(submenu)
  
    # Add to Glyphs menu
    Glyphs.menu[WINDOW_MENU].append(mainMenuItem)
1 Like

What is the best practice and convention if I need to separate the group of few items in context menu?
– Add separators above and below the group
– Add only one separator above, before the group

I’m thinking about the case when different plugins have context menu items with separators above and below, so separators from different plugins will make double line.