swift - List all mounted Volumes in a Menu Bar Submenu (programmatically only) -
i'm working on "menu bar only" project , need list mounted volumes in submenu in menubar app. figured out how print() mounted volumes need submenu (without .xib or .storyboard work) "listvolumes func"
func listvolumes(sender: nsmenuitem) { let keys = [nsurlvolumenamekey, nsurlvolumeisremovablekey, nsurlvolumeisejectablekey] let paths = nsfilemanager().mountedvolumeurlsincludingresourcevaluesforkeys(keys, options: []) if let urls = paths { url in urls { if let components = url.pathcomponents components.count > 1 && components[1] == "volumes" { print(url) } } } }
an below code "menu bar app"
let statusitem = nsstatusbar.systemstatusbar().statusitemwithlength(-2) func applicationdidfinishlaunching(anotification: nsnotification) { if let button = statusitem.button { button.image = nsimage(named: "statusbarbuttonimage") } let menu = nsmenu() menu.additem(nsmenuitem(title: "volumes", action: selector("volumes:"), keyequivalent: "")) menu.additem(nsmenuitem.separatoritem()) menu.additem(nsmenuitem(title: "help", action: selector("help:"), keyequivalent: "")) menu.additem(nsmenuitem.separatoritem()) menu.additem(nsmenuitem(title: "quit", action: selector("terminate:"), keyequivalent: "q")) statusitem.menu = menu }
so question is, how can create submenu contains mounted drives (programmatically only)
you have set submenu volumes
menu item example
let volumesmenuitem = nsmenuitem(title: "volumes", action: selector("volumes:"), keyequivalent: "") menu.additem(volumesmenuitem) let volumesmenu = nsmenu(title: "volumes") volumesmenuitem.submenu = volumesmenu
i recommend return array of names listvolumes()
function, can add menu items volumesmenu
assuming volumes
contains names
for volumename in volumes { volumesmenu.additem(nsmenuitem(title: volumename, action: selector("selectvolume"), keyequivalent: "")) }
Comments
Post a Comment