Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setResourceValue NSURLTagNamesKey error

Tags:

macos

swift

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

like image 831
Francois in UK Avatar asked Oct 20 '22 11:10

Francois in UK


1 Answers

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)
like image 168
Eric Aya Avatar answered Oct 22 '22 21:10

Eric Aya