I am working on a macOS application using Swift which generates a file on the desktop and I want to get and set the comment field as string using Swift. I don't want to use AppleScript or 3rd party libraries. If anyone knows how to do it, please help.
I have used this and this to get the comments, but I am not able to set the comments. I tried the set function, it sets the comments, but when I open 'Get Info' it is not updated there.
Here is my code, in which I tried to get and set the comments:
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)")
}