如何使用Swift在MacOS上检查文件是否有红色标签标签在Finder中?

huangapple go评论178阅读模式
英文:

How can I check if a file has a red tag label in Finder using Swift on MacOS?

问题

我正在为 MacOs 创建我的第一个项目。
我必须处理文件夹中的文件。我的目标是检查这些文件,并查看哪些元素通过 Finder 应用了红色标签。
我真的疯了,因为代码无法识别具有红色标签的图像。

这是我编写的函数。

这是 ViewController 中的代码。

文件信息

英文:

I am creating my first project for MacOs.
I have to work on files in a folder. My goal is to check these files and see which elements have a red tag applied via Finder.
I am literally going crazy because the code cannot recognise the images with the red tag.

This is the function I have written.

  1. func isFileTaggedRed(at fileURL: URL) -> Bool {
  2. do {
  3. let resourceValues = try fileURL.resourceValues(forKeys: [.labelColorKey])
  4. if let labelColor = resourceValues.labelColor, labelColor == .red {
  5. return true
  6. }
  7. } catch {
  8. print("Error retrieving resource values for file: \(error)")
  9. }
  10. return false
  11. }

This is the code in the ViewController

  1. for imageUrl in imageUrls {
  2. if isFileTaggedRed(at: imageUrl) {
  3. print("Skipping red tagged image: \(imageUrl.lastPathComponent)")
  4. continue
  5. }else{
  6. print("not tagged")
  7. }
  8. }

file info

答案1

得分: 2

我不知道是否有更简单的方法来完成您所需的操作,但您可以获取文件的tagNames并检查是否包含"Red"

  1. extension URL {
  2. var isFileTaggedRed: Bool {
  3. tagNames.contains("Red")
  4. }
  5. var tagNames: [String] {
  6. (try? resourceValues(forKeys: [.tagNamesKey]))?.tagNames ?? []
  7. }
  8. }

用法:

  1. import Cocoa
  2. class ViewController: NSViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. let folderUrl = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
  6. .appendingPathComponent("untitled folder", isDirectory: true)
  7. let files = try! FileManager.default.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil)
  8. if let fileUrl = files.first {
  9. print("isTaggedRed:", fileUrl.isFileTaggedRed)
  10. }
  11. }
  12. }
英文:

I don't know if there is an easier way to accomplish what you need but you can get the fileUrl tagNames and check if it contains "Red"


  1. extension URL {
  2. var isFileTaggedRed: Bool {
  3. tagNames.contains("Red")
  4. }
  5. var tagNames: [String] {
  6. (try? resourceValues(forKeys: [.tagNamesKey]))?.tagNames ?? []
  7. }
  8. }

Usage:

  1. import Cocoa
  2. class ViewController: NSViewController {
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. let folderUrl = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
  6. .appendingPathComponent("untitled folder", isDirectory: true)
  7. let files = try! FileManager.default.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil)
  8. if let fileUrl = files.first {
  9. print("isTaggedRed:", fileUrl.isFileTaggedRed)
  10. }
  11. }
  12. }

答案2

得分: 1

I haven't used labelColorKey before. It looks like the labelColor you get back is an NSColor. Colors are tricky in a color-managed system like MacOS. The color may be a shade of red but not the exact color you want. I would suggest logging the RGB values of your returned colors with NSColor.getRed(_:green:blue:alpha:). You're probably getting back a color that's close to, but not exactly equal to .red.

You might need to add code that checks to see if the RGB values are within a "wiggle room" range of .red (say each component is within .01 of the target color.)

Edit:

I just tried it, and when I added a red label to a file, I got back an NSColor that logs as NSCalibratedRGBColorSpace 0.980484 0.382818 0.347662 1

That will look red, but not fully saturated red.

Note that NSColor.red logs as sRGB IEC61966-2.1 colorspace 1 0 0 1 (100% red and no green or blue.)

Furthermore, when I look at a file with a red label, it does look like a slightly washed out red, not pure red.

Edit #2:

The code I used (in a command line tool) is as follows:

  1. func fileColor(at fileURL: URL) -> NSColor? {
  2. do {
  3. let resourceValues = try fileURL.resourceValues(forKeys: [.labelColorKey])
  4. if let labelColor = resourceValues.labelColor {
  5. return labelColor
  6. }
  7. } catch {
  8. print("Error retrieving resource values for file: \(error)")
  9. return nil
  10. }
  11. return nil
  12. }
  13. let path = "~/Documents/RedFile.rtf"
  14. let fixedPath = NSString(string: path).expandingTildeInPath
  15. let fileURL = URL(filePath: fixedPath)
  16. if let fileColor = fileColor(at: fileURL) {
  17. print("File color returned is \(fileColor)")
  18. } else {
  19. print("Could not load file.")
  20. }

(Note: I didn't translate the code, as requested.)

英文:

I haven't used labelColorKey before. It looks like the labelColor you get back is an NSColor. Colors are tricky in a color-managed system like MacOS. The color may be a shade of red but not the exact color you want. I would suggest logging the RGB values of your returned colors with NSColor.getRed(_:green:blue:alpha:). You're probably getting back a color that's close to, but not exactly equal to .red.

You might need to add code that checks to see if the RGB values are within a "wiggle room" range of .red (say each component is within .01 of the target color.)

Edit:

I just tried it, and when I added a red label to a file, I got back an NSColor that logs as NSCalibratedRGBColorSpace 0.980484 0.382818 0.347662 1

That will look red, but not fully saturated red.

Note that NSColor.red logs as sRGB IEC61966-2.1 colorspace 1 0 0 1 (100% red and no green or blue.)

Furthermore, when I look at a file with a red label, it does look like a slightly washed out red, not pure red.

Edit #2:

The code I used (in a command line tool) is as follows:

  1. func fileColor(at fileURL: URL) -> NSColor? {
  2. do {
  3. let resourceValues = try fileURL.resourceValues(forKeys: [.labelColorKey])
  4. if let labelColor = resourceValues.labelColor {
  5. return labelColor
  6. }
  7. } catch {
  8. print("Error retrieving resource values for file: \(error)")
  9. return nil
  10. }
  11. return nil
  12. }
  13. let path = "~/Documents/RedFile.rtf"
  14. let fixedPath = NSString(string:path).expandingTildeInPath
  15. let fileURL = URL(filePath: fixedPath)
  16. if let fileColor = fileColor(at: fileURL) {
  17. print("File color returned is \(fileColor)")
  18. } else {
  19. print("Could not load file.")
  20. }

huangapple
  • 本文由 发表于 2023年5月29日 22:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定