You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Swift在macOS程序中读写文件「显示简介」的注释字段

问题

开发基于Swift的macOS应用,需在桌面生成文件,并通过Swift以字符串形式读写文件「显示简介」窗口中的注释字段,要求不使用AppleScript或第三方库。

目前已能通过扩展属性读取注释,但调用设置函数后,「显示简介」窗口的注释并未更新,尝试的代码如下:

let shortcutURL = "path to file"
let newComment = "I am a new comment"

var temp: String
var data: Data

do {
    try data = FileManager.default.extendedAttribute("com.apple.metadata:kMDItemFinderComment", atPath: shortcutURL, traverseLink: true)
    try temp = PropertyListSerialization.propertyList(from: data as Data, format: nil) as! String
    print("Data: \(temp)")
} catch {
    print("Error: \(error)")
}

do {
    try FileManager.default.setExtendedAttribute("com.apple.metadata:kMDItemFinderComment",
                                                 value: newComment.data(using: .utf8), atPath: shortcutURL,
                                                 traverseLink: true, mode: XAAnyMode)
    print("Comment updated successfully")
} catch {
    print("Error: \(error)")
}
解决方案

问题出在扩展属性com.apple.metadata:kMDItemFinderComment存储的是plist格式数据,而非直接的UTF-8字符串。你读取时用了PropertyListSerialization解析,但设置时直接把字符串转成了Data,格式不匹配,导致Finder无法识别。

正确的读写实现

以下是修正后的代码,包含完整的读写逻辑,以及设置后通知Spotlight更新元数据(确保Finder立刻刷新注释):

import Foundation
import CoreServices

// 读取文件注释
func getFinderComment(forPath path: String) throws -> String? {
    let data = try FileManager.default.extendedAttribute("com.apple.metadata:kMDItemFinderComment", atPath: path, traverseLink: true)
    guard let comment = try PropertyListSerialization.propertyList(from: data, format: nil) as? String else {
        return nil
    }
    return comment
}

// 设置文件注释
func setFinderComment(_ comment: String, forPath path: String) throws {
    // 将字符串包装成plist格式的Data
    let plistData = try PropertyListSerialization.data(fromPropertyList: comment, format: .binary, options: 0)
    try FileManager.default.setExtendedAttribute("com.apple.metadata:kMDItemFinderComment",
                                                 value: plistData,
                                                 atPath: path,
                                                 traverseLink: true,
                                                 mode: XAAnyMode)
    
    // 通知Spotlight更新文件元数据,确保Finder立刻刷新
    let url = URL(fileURLWithPath: path)
    MDItemUpdate(url as CFURL)
}

// 使用示例
let filePath = "path to your file"
let newComment = "I am a new comment"

// 读取注释
do {
    if let comment = try getFinderComment(forPath: filePath) {
        print("当前注释: \(comment)")
    } else {
        print("无注释")
    }
} catch {
    print("读取错误: \(error)")
}

// 设置注释
do {
    try setFinderComment(newComment, forPath: filePath)
    print("注释更新成功")
} catch {
    print("设置错误: \(error)")
}

关键说明

  1. plist格式要求:kMDItemFinderComment的扩展属性必须是二进制或XML格式的plist数据,直接存储字符串Data会导致Finder无法解析,所以设置时必须用PropertyListSerialization把字符串转成plist格式的Data。
  2. 元数据更新通知:设置扩展属性后,Finder可能不会立刻刷新显示,调用MDItemUpdate(来自CoreServices框架)可以通知Spotlight更新文件元数据,让「显示简介」窗口的注释即时更新。
  3. 类型安全优化:读取时增加了类型判断,避免强制解包导致崩溃,提升代码稳定性。

内容的提问来源于stack exchange,提问作者yash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:53:14