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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -