Swift 3: Set Finder label color

前端 未结 4 1965
夕颜
夕颜 2020-12-09 00:52

I\'m trying to set the colored labels shown by the finder. The only function I know is setResourceValue. But this needs localized names!

I could image my mother lang

4条回答
  •  清歌不尽
    2020-12-09 01:14

    I got it working without having to know the color name, thanks to the new URLResourceValues() struct and the tag numbers.

    Knowing that each of these tag numbers represents a tag color:

    0 None
    1 Grey
    2 Green
    3 Purple
    4 Blue
    5 Yellow
    6 Red
    7 Orange

    Make a URL of your file:

    var url = URL(fileURLWithPath: pathToYourFile)
    

    It has to be a var because we are going to mutate it.

    Create a new URLResourceValues instance (also needs to be a variable):

    var rv = URLResourceValues()
    

    Set the label number like this:

    rv.labelNumber = 2 // green
    

    Finally, write the tag to the file:

    do {
        try url.setResourceValues(rv)
    } catch {
        print(error.localizedDescription)
    }
    

    In our example we've set the number tag to 2 so now this file is labeled with green color.

提交回复
热议问题