osx - Get mounted volumes list with Swift? -


does know how list of removable volumes mounted swift?

i've tried this, return list of files , subfolders of external drivers:

let filemanager:nsfilemanager = nsfilemanager()         let files = filemanager.enumeratoratpath("/volumes")           while let file = files?.nextobject() {             println(file)             menu.additem(nsmenuitem(title: file as! string, action: selector(""), keyequivalent: ""))         } 

this prints list of mounted volumes:

let filemanager = nsfilemanager() let keys = [nsurlvolumenamekey, nsurlvolumeisremovablekey, nsurlvolumeisejectablekey] let paths = filemanager.mountedvolumeurlsincludingresourcevaluesforkeys(keys, options: nil) if let urls = paths as? [nsurl] {     url in urls {         println(url)     } } 

you can of course filter paths inside "volumes" directory:

let filemanager = nsfilemanager() let keys = [nsurlvolumenamekey, nsurlvolumeisremovablekey, nsurlvolumeisejectablekey] let paths = filemanager.mountedvolumeurlsincludingresourcevaluesforkeys(keys, options: nil) if let urls = paths as? [nsurl] {     url in urls {         if url.relativepath?.pathcomponents.count > 1 {             if url.relativepath?.pathcomponents[1] == "volumes" {                 println(url)             }         }     } } 

and swift 2 there's 2 differences: pass [] instead of nil filemanager's options, , there's no need cast array of nsurls:

let filemanager = nsfilemanager() let keys = [nsurlvolumenamekey, nsurlvolumeisremovablekey, nsurlvolumeisejectablekey] let paths = filemanager.mountedvolumeurlsincludingresourcevaluesforkeys(keys, options: []) if let urls = paths {     url in urls {         if url.relativepath?.pathcomponents.count > 1 {             if url.relativepath?.pathcomponents[1] == "volumes" {                 print(url)             }         }     } } 

update swift 2.1

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)         }     } } 

update swift 3

let keys: [urlresourcekey] = [.volumenamekey, .volumeisremovablekey, .volumeisejectablekey] let paths = filemanager().mountedvolumeurls(includingresourcevaluesforkeys: keys, options: []) if let urls = paths {     url in urls {         let components = url.pathcomponents         if components.count > 1            && components[1] == "volumes"         {             print(url)         }     } } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -