I am getting an error when trying to set a tag color Blue to a file using setResourceValue:
var error: NSError?
let listofTags = NSWorkspace.sharedWorkspace().fileLabels
let theURL:NSURL = NSURL.fileURLWithPath("/Volumes/234567_fr.tif")!
var Tag: AnyObject = NSWorkspace.sharedWorkspace().fileLabels[4] // Tag = "Blue"
theURL.setResourceValue(Tag, forKey: NSURLTagNamesKey, error: &error)
println(error) // Error Domain=NSOSStatusErrorDomain Code=-8050 "The operation couldn’t be completed. (OSStatus error -8050.)
Any idea? Thank you for your help
Solution:
1 - The first argument for setResourceValue
has to be an NSArray
2 - Shocking, but... the color name has to be the localized one!
This example fixes your error 8050 but doesn't actually set a color tag if your system language isn't English:
var error: NSError?
let theURL:NSURL = NSURL(fileURLWithPath: "/Users/me/tests/z.png")!
let tag: AnyObject = NSWorkspace.sharedWorkspace().fileLabels[4] // "Blue" tag
let arr = NSArray(object: tag)
theURL.setResourceValue(arr, forKey: NSURLTagNamesKey, error: &error)
On my system (French), this doesn't set an actual blue color label tag, only a text tag containing the word "Blue".
To set the proper color tag, you have to give the localized color name literally:
var error: NSError?
let theURL:NSURL = NSURL(fileURLWithPath: "/Users/me/tests/z.png")!
let arr = NSArray(object: "Bleu") // "Blue" translated to French
theURL.setResourceValue(arr, forKey: NSURLTagNamesKey, error: &error)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With