【问题标题】:Setting the Finder label from Cocoa从 Cocoa 设置 Finder 标签
【发布时间】:2026-01-20 05:55:02
【问题描述】:

我想在 Cocoa 中以编程方式设置 Finder 标签(也就是 Finder 中项目的颜色)。我可以毫无问题地阅读 URL u 的 Finder 标签

MDItemRef itemRef = MDItemCreateWithURL(NULL, (CFURLRef)u);
CFStringRef s = MDItemCopyAttribute(itemRef, kMDItemFSLabel);
label = [(NSString *)s intValue];

此 API 不提供任何用于写入元数据的机制,所以我的猜测是解决方案完全在别处。有什么提示吗?

编辑:我知道这可以使用 AppleScript,但在沙盒中是不允许的。我需要一个纯粹的进程内解决方案。

【问题讨论】:

标签: objective-c macos cocoa


【解决方案1】:

对于 Snow Leopard 及更高版本,您可以使用 URL 资源 API,如 this answer 所示。

【讨论】: