osx - Read & Write file tag in cocoa app OS X with swift or obj-c -
is there way read/write file tags without shell commands? tried nsfilemanager
, cgimagesource
classes. no luck far.
an nsurl
object has resource key nsurltagnameskey
. value array of strings.
this swift example reads tags, adds tag foo
, write tags back.
let url = nsurl(fileurlwithpath: "/path/to/file.ext") var resource : anyobject? { try url.getresourcevalue(&resource, forkey: nsurltagnameskey) var tags : [string] if resource == nil { tags = [string]() } else { tags = resource as! [string] } print(tags) tags += ["foo"] try url.setresourcevalue(tags, forkey: nsurltagnameskey) } catch let error nserror { print(error) }
the swift 3+ version bit different. in url
tagnames
property get-only it's necessary bridge cast url
foundation nsurl
var url = url(fileurlwithpath: "/path/to/file.ext") { let resourcevalues = try url.resourcevalues(forkeys: [.tagnameskey]) var tags : [string] if let tagnames = resourcevalues.tagnames { tags = tagnames } else { tags = [string]() } tags += ["foo"] try (url nsurl).setresourcevalue(tags, forkey: .tagnameskey) } catch { print(error) }
Comments
Post a Comment