Finder comments without finder

I’ve seen multiple times, the Finder doesn’t write comments to files reliably, no matter what OS I have been using.

tell application "Finder" to set comment of getFile to "Test" 

What I wanna say is, sometimes the “Finder” writes, sometimes it doesn’t, even if the input is declared as string.

Adding tags would be another way to add the info to my files, but because the content of my comments varies very much, i only would spam my tag list.
Is it actually possible to write to file comments without the Finder?

I’m not convinced that’s the case. My guess is that the Finder writes to, and reads from, the containing folder’s .DS_Store folder, and that the Finder’s Spotlight plug-in reads comments from there when creating the corresponding Spotlight kMDItemFinderComment metadata item.

I misunderstood what you were saying. Spotlight has its own Comments key (kMDItemComment), and I thought you were referring to that.

That action has a strange name…

I refer to text in my first post :

@Fredrik71
None of the example links given show how to apply comments without the Finder.
Scripters who interact with the file system know the Finder is slow and unreliable. In fact, I’d not need to ask for alternative ways to add comments, as the Finder code to add comments to files is stupid simple. But this stupid simple code is, unfortunately, unreliable

I recommend to read with more attention what people post, English isn’t my first language either

Since file comments are an extended attributes, one way to do it without the Finder could be with xattr, if I’m right. I never used it but it seems to modify files, not file attributes. Just a quick test : (This code doesn’t throw errors but won’t write anything either)

tell application "Finder" to set sel to item 1 of (get the selection) as text
set theW to "Test"
do shell script "xattr -w 'kMDItemFinderComment = \"" & theW & "\"' '" & POSIX path of sel & "'"

Be aware that this throws an error if nothing or more than one item is selected.

I would like to see at least one code (involving Finder) that cannot set the comment property of a Finder element. In the meantime, I agree with only one statement: Finder is not fast enough compared to System Events or AsObjC.

Let’s say you edited the .DS_Store of the folder. How does this relate to the comment property of the Finder app? I think so - unidirectionally. I think .DS_Store gets data from the comment property to update, but not backwards.

I would not like to state that the search for a solution is meaningless. I myself wonder how this can be. But I’m pessimistic.

Maybe, this is the most reliable syntax for the Finder (that is, avoid using AppleScript alias or other non-Finder file specifications directly):

set hfs to (choose file) as text
tell application "Finder" to tell file hfs to set comment to "Test2"

It’s not applescript but you could use Automator to set the comments. In years past, I too had problems in getting applescript to reliably set finder comments but for whatever reason, it now works for me in a handful of scripts that I use regularly.

But at the time, I used Automator because I found it worked reliably and I could easily change the comment. When I need to add comments to many folders I leave the workflow open and change the text to insert as needed. Basically, with command-V, command-R, I can modify the selected files.

But as you didn’t mention how you plan to set the text, I thought about just using the clipboard so I created a workflow with the following actions:

Application receives files and folders as input

Get Contents of Clipboard
Set Value of Variable [Variable: Text]
Get Selected Finder Items
Set Spotlight Comments for Finder Items [Text]

The ‘Text’ in the last action is the previously defined variable. I have ‘ignore this action’s input’ checked on the ‘Get Contents of Clipboard’ action.

If you don’t want to go have to switch to Automator and type Command-R each use, then try this: Save as an Application and drag it onto the Dock. To use, click the Dock icon. Quick and dirty but it works well. Hope this helps.