0

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)")
}
soundflix
  • 928
  • 9
  • 22
yash
  • 18
  • 3
  • You will need to [edit] and add more code to your question, right now there are essential pieces missing to run or analyze your code. Try to post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – soundflix Aug 24 '23 at 15:55
  • with "it sets the comments" you mean that `setExtendedAttribute` runs without error? – soundflix Aug 24 '23 at 15:57
  • The Finder "Get Info" seems to buffer the old comment. If you use `xattr -w` on the command line, Finder still shows the old value. If I run your code and then use `xattr -p` on the command line then it shows the updated comment even though Finder does not. – HangarRash Aug 25 '23 at 04:51
  • One other comment on your code (no pun intended) - You want some consistency between the read and write of the comment. Your code uses `PropertyListSerialization` to decode the comment on read. But you just write a plain string on update. Either use `PropertyListSerialization` for both read and write or don't use on either. – HangarRash Aug 25 '23 at 04:57
  • I also suggest having a look at https://stackoverflow.com/questions/38343186/write-extend-file-attributes-swift-example for a Swift implementation. – HangarRash Aug 25 '23 at 04:59
  • When I call `extendedAttribute` it returns the comment in Binary plist so I have to use `PropertyListSerialization` to read and I don't know how to write the same. So while calling `setExtendedAttribute` it sets the comment but when I again call `extendedAttribute` to get comment this time it is not in binary plist. So I want to know how to set the comment so it would reflect in the get info also. – yash Aug 25 '23 at 06:13
  • @yash Use the following to convert the new comment into `Data` for writing: `let data = try PropertyListSerialization.data(fromPropertyList: newComment, format: .binary, options: 0)` – HangarRash Aug 25 '23 at 06:31
  • 1
    @HangarRash I am able to set it in binary plist, but still it is not getting reflected in "Get Info" window. – yash Aug 25 '23 at 06:40
  • @yash It is strange. One thing I've just learned is that when you use Finder's "Get Info" to set a comment, the `.DS_Store` file in the folder is updated. But when you set the file's extended attribute with your code or the `xattr` command-line tool, the `.DS_Store` file is not updated. So changes made in "Get Info" can be read by your code and `xattr`, but changes made by your code or `xattr` are not shown in "Get Info". Perhaps the AppleScript based Swift code you linked resolves that issue. Did you try it? – HangarRash Aug 25 '23 at 07:01
  • @HangarRash, I don't want to use AppleScript or 3rd party libraries. Is there any way we can reflect the changes in `.DS_Store` also? – yash Aug 25 '23 at 07:15

0 Answers0