SetResourceValue NSURLTagNamesKey error

I get an error when trying to set the color of the Blue tag 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.

+5
source share
1 answer

Decision:

1 - The first argument to setResourceValue must be NSArray

2 - Shocking, but ... the color name must be localized!

In this example, error 8050 is fixed, but the color mark is not actually set if your system language is not 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 does not set the actual label of the blue label, but only a text tag containing the word "blue".

To set the correct color tag, you must specify 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) 
+3
source

All Articles