26

I'm trying to create a cascade submenu for a Finder Sync extension in Swift/Cocoa. I have the following code:

override func menuForMenuKind(menuKind: FIMenuKind) -> NSMenu! {
    let m = NSMenu(title: "")
    let mi1 = NSMenuItem(title: "item1", action: nil, keyEquivalent: "")
    let mi11 = NSMenuItem(title: "item11", action: nil, keyEquivalent: "")

    let m2 = NSMenu(title: "")
    let mi2 = NSMenuItem(title: "item2", action: nil, keyEquivalent: "")
    m2.addItem(mi2)

    m.addItem(mi1)
    m.addItem(mi11)
    m.setSubmenu(m2, forItem: mi1)
    return m
}

So what I'm trying to achieve is:

item1->
       item2 
item11

So what I actually get is a flat item1 and item11 menu list.

Any hints?


I filed a bug with Apple (#18810635), got a response that it's a duplicate of (#18531883) which is still open.

Posted the copy of the filing at OpenRadar http://openradar.appspot.com/radar?id=5772557445758976 , tweeted to a developer advocate.

If anyone knows the fate of #18531883 - this is core raison d'être for Finder Sync Extensions?

Greg
  • 9,068
  • 6
  • 49
  • 91
Krzysztof Rosiński
  • 1,488
  • 1
  • 11
  • 24

3 Answers3

4

I've upgraded the project to Swift 2.0, and finally made a working submenu. Posting the solution:

override func menuForMenuKind(menuKind: FIMenuKind) -> NSMenu! {
    let main = NSMenu()
    let submenu = NSMenu()
    let mainDropdown = NSMenuItem(title: "Some option group", action: nil, keyEquivalent: "")
    main.addItem(mainDropdown)
    m.setSubmenu(submenu, forItem: mainDropdown)


    submenu.addItem(NSMenuItem(title: "Option 1", action: nil, keyEquivalent: ""))
    submenu.addItem(NSMenuItem(title: "Option 2", action: nil, keyEquivalent: ""))
    return main
}

This will only work on Mac OS 10.11+, 10.10.5 still has the bug being unable to generate a submenu. So a good appraoch is generating a flat menu for < 10.11, and a cascade starting from el capitan.

Krzysztof Rosiński
  • 1,488
  • 1
  • 11
  • 24
  • Hi, do you know how to add a menu item like the *Move to Dropbox* menu item [here](http://i.imgur.com/C9Fzbif.png)? In the docs it is mentioned that you need to implement the same `menuForMenuKind:` method passing the menu kind to it as an argument. Do you know from hwere I should be calling that method with the parameter? – Isuru Dec 11 '15 at 11:08
  • You don't explicitly call it, menuForMenuKind: should be implemented for FIFinderSync interface. When You register the directories, FinderSync will call this method each time a user opens a files context menu. – Krzysztof Rosiński Dec 11 '15 at 14:49
  • Oh I see. What do you mean by registering directories? Can you please tell me how to do that? Or point me to a resource you used? Info about Finder Sync is scarce. – Isuru Dec 11 '15 at 14:57
  • A Hello World example is available as a template in Xcode. File->New->Target, choose Application Extensions, pick the Finder Sync Extension. Good luck – Krzysztof Rosiński Dec 11 '15 at 16:19
  • I was already playing around with the boilerplate project but still having the issue. I'll post a separate question then. Thanks. – Isuru Dec 11 '15 at 18:17
0
func constructMenu() {
        let main = NSMenu()
        let submenu = NSMenu()
        let mainDropdown = NSMenuItem(title: "Some option group", action: nil, keyEquivalent: "")
        main.addItem(mainDropdown)
        main.setSubmenu(submenu, for: mainDropdown)


        submenu.addItem(NSMenuItem(title: "Option 1", action: nil, keyEquivalent: ""))
        submenu.addItem(NSMenuItem(title: "Option 2", action: nil, keyEquivalent: ""))




       // myList.setSubmenu(mylist2, for: myList)


        statusItem.menu = main
    }
0

Finder Sync Extensions do not support submenus in versions before macOS 10.11.

This is stated in the code docs when you examine the FIFinderSyncProtocol header:

Specific menu item properties are used: title, action, image, and enabled. Starting in 10.11: tag, state, and indentationLevel also work, and submenus are allowed.

optional func menu(for menu: FIMenuKind) -> NSMenu?
pkamb
  • 33,281
  • 23
  • 160
  • 191