Unable to set submenu for NSMenuItem (FinderSync extension)
Asked Answered
H

3

25

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?

Hadria answered 14/10, 2014 at 11:54 Comment(5)
Could you post your log (from Console)? I'm experiencing the exact same issue..Chandigarh
hi! I used your question to build my submenu items and I managed to make it work.Atoll
#Nuno Gonzales - have you tried using it in FinderSync extension or just a regular submenu?Chandigarh
I'm having the same problem. Kind of annoying...Chopin
Anybody have an update on this issue? I tried to find the status of 18531883 on OpenRadar, but it doesn't seem to exist in their database.Weisshorn
H
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.

Hadria answered 21/9, 2015 at 6:45 Comment(5)
Hi, do you know how to add a menu item like the Move to Dropbox menu item here? 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?Pippin
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.Moriarty
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.Pippin
A Hello World example is available as a template in Xcode. File->New->Target, choose Application Extensions, pick the Finder Sync Extension. Good luckMoriarty
I was already playing around with the boilerplate project but still having the issue. I'll post a separate question then. Thanks.Pippin
P
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
    }
Phycomycete answered 28/3, 2019 at 16:56 Comment(0)
S
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?
Seem answered 15/4, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.