I've never written macOS or Swift code before. I learned just enough to get this proof of concept together. It only makes the two-way generalization as in my question: Removable and/or ejectable media vs non-removable non-ejectable media. DMGs are lumped with USB sticks and SD cards. Optical and floppy disks surely are too. I have no idea if there's such thing as a storage type where only one of "removable" and "ejectable" is true but not both...
import Cocoa
import DiskArbitration
if let session = DASessionCreate(kCFAllocatorDefault) {
let mountedVolumeURLs = FileManager.default.mountedVolumeURLs(includingResourceValuesForKeys: nil)!
for volumeURL in mountedVolumeURLs {
if let disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, volumeURL as CFURL),
let bsdName = DADiskGetBSDName(disk) {
let bsdString = String(cString : bsdName)
print(volumeURL.path, bsdString)
if let descDict = DADiskCopyDescription(disk) as? [String: CFTypeRef] {
let removable : Bool, ejectable : Bool
if let val = descDict["DAMediaRemovable"] as? Bool {
removable = val
if let val = descDict["DAMediaEjectable"] as? Bool {
ejectable = val
var type = ""
type += removable || ejectable ? "USB stick, SD card, etc" : "hard drive, SSD, etc";
type += " ("
type += removable ? "" : "not "
type += "removable"
type += ", "
type += ejectable ? "" : "not "
type += "ejectable"
type += ")"
print(" ", type)
}
}
}
print("\n")
}
}
}