Easy way to replicate Scripts menu bar?



  • I'd like to partition my Scripts folder, and have another application menu bar item alongside Extensions, Scripts, Window etc. with another menu corresponding to a directory on my HD. I've got the bare bones of a custom menu ready, but not sure if there's a stock function for linking the menu structure to a local directory.

    TIA!
    Ryan


  • admin

    hello @ryan,

    here’s a function to create a new menu item for a folder structure with .py files:

    from AppKit import NSApp, NSMenu, NSMenuItem
    from lib.UI.fileBrowser import RFPathItem
    
    def addMenu(name, path):
        '''Creates a new menu item in RoboFont’s main application menu.'''
    
        # create a new menu
        menu = NSMenu.alloc().initWithTitle_(name)
    
        # create a path item that will build the menu and connect all the callbacks
        pathItem = RFPathItem(path, ['.py'], isRoot=True)
        pathItem.getMenu(title=name, parentMenu=menu)
    
        # get the main menu
        menubar = NSApp().mainMenu()
    
        # search if the menu item already exists
        newItem = menubar.itemWithTitle_(name)
        if not newItem:
            # if not, create one and append it before `Help`
            newItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(name, '', '')
            menubar.insertItem_atIndex_(newItem, menubar.numberOfItems()-1)
    
        # set the menu as submenu
        newItem.setSubmenu_(menu)
        
    addMenu('My Menu', '/path/to/scripts/folder')
    

    you can set it as a start-up script, so the menu will always be there when you open RoboFont.

    (will be added to the docs soon… thanks!)



  • Thanks, @gferreira! This is exactly what I was looking for. Do you know of any resources that explain how to work with AppKit? Apple’s documentation feels less accessible/thorough to me, but I could be missing something.

    Thanks again,
    Ryan


  • admin

    • for the AppKit Apple docs opt for the objc version and replace : by _ for the python version.
    • use print(dir(nsObject)) to find all the methods and attributes

  • admin

    the preferred way is to use mojo.UI.MenuBuilder

    from mojo.UI import MenuBuilder
    
    def callback(sender):
        pass
    
    builder = MenuBuilder([("test", callback), ("test 2", callback)])
    
    menu = builder.getMenu()
    print(menu)
    
    name = "My Menu"
    
    # get the menu bar
    menubar = NSApp().mainMenu()
    
    newItem = menubar.itemWithTitle_(name)
    if not newItem:
        # if not, create one and append it before `Help`
        newItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(name, '', '')
        menubar.insertItem_atIndex_(newItem, menubar.numberOfItems()-1)
    
    # set the menu as submenu
    newItem.setSubmenu_(menu)