How do you access the Finder Comment of a file with AsObjC or Cocoa?

That is what it boils down to.

I am not sure that it is possible, but please do prove me wrong. :wink:

I know I can execute an osascript from the system function of a c-program, but there must be something slightly better than that?

Hi,

easiest way: AppleScript.

tell application "Finder" to get comment of item "disk:path:to:item"

Finder comments are Spotlight comments (as indicated in Finder info window), so use Spotlight API


NSURL *url = [NSURL fileURLWithPath:@"/path/to/item"];
MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, (CFURLRef)url);
CFStringRef comment = MDItemCopyAttribute(item, kMDItemFinderComment);
NSLog(@"%@", (NSString *)comment);
CFRelease(item);
CFRelease(comment);

Hello.

The Finder comment are extended attributes too. (xattr -l /path/to/your/file), but the .DS_Store format are prorpietary, so there is no way, I am going to write anything into that, knowing what I have to do to read and decode it for the various Uniocode planes.

By your code, it seems I can pull it off "easily, without having wrap everything written into a NSApplication. Thank you very much Stefan! :slight_smile:

Thanks for ruining the worst piece of code I have written to this day! :slight_smile: 6 hours in gdb.

But I am not any closer to writing, a new comment, or have I missed something?

Had this been a regular property, then I could have created a dictionary, and I could have used the CFURL Access functions, to write just the properties to a file.

But this resides in the MetaData Framework, and it at least seems to me, that there are only methods to retrieving items.

If anybody knows about another way, or a work other than using AppleScript, then it is you Stefan. (But I guess, writing a commandline tool for setting Finder Comment files, are rather futile, at least without the Carbon framework.

Spotlight is read only, so use the Finder / osascript

Well … there is some documentation for the .DS_Store format out there, but then there is the encoding again, and getting the preamble right and all that, it is a binary propertylist file, stored in a record in the .DS_Store file.

I guess a system call with some osascript in a string, “compiled” for every file will do. :smiley:

Thank you very much for your help Stefan. Really appreciated!

take a look at this.
Maybe it helps

Hello.

Thank you. I have seen that already. The thing is that it should be kind of straight forward to create, or read a property list file, from memory, so you get it encoded to UTF8 directly from UTF16BE, but I didn’t figure out how to do that. Then there is writing it back, both to the xattr resource, as a propertylist file inside it. now that could be easy too, as long as you iterated over any other properties that were there. But the next step, would be to update the .DS_Store file, but that is a b-tree structure, and now, that could have been solved, if I had forensic interests, and an abundance of time, but…

I think something like this, with an alarm firing off after a second if it Finder can’t cope, will be just fine. :slight_smile:

system("osascript -e \"tell application \\\"Finder\\\" to set the comment of  POSIX FILE " & quoted form of anItem & " to " &quoted form of  theComment & " \" 2>&1 >/dev/null");

Hello.

By the way Stefan, I am not sure if you know this, -I learned it recently: hitting Esc ‘.’ in a terminal fills out the current line with the arguments (at least last) of the previously executed command. Nice, when the path is a long one. :wink:

Hello.

It seems to be doable the thing with finder comments, but I think AppleEvents is the way to go, given that the format is private for Apple. Spotlight Comments

Hello.

Here is a link to the executable with full source I have pondered through this, and enters it as a github project, after I have written this. So hopefully somebody finds the pieces for writing reliable command-line tools, that I then can use. :slight_smile: it should be very robust, and work from Tiger and onwards. I test for everything hence the size. -For now it is free to use for Non-Commercial purposes, you may not sell it as part of something, without giving me notice at least. And then you’ll have to attach this source with your product. GnU LPGL 2.0

Just enter “comment” for help in a Terminal window, once installed in your path.

You can delete a comment by issuing the command comment -s ‘’ yourfile.

Hello.

I tossed in the source, should at least be usable for people needing a command line tool.

Hello.

If anybody noticed, that setting comments on a folder didn’t work, then I am happy to tell you that it works now.:o

Hello.

I also forgot to remove the echoing of arguments from the osascript part last night, that is fixed now.

The link in post #11 contains the updated version. (And this should constitute the total of fixes for comment.)

MacUsrII,

Reassure me this is part of a very clever scheme to make using Applescript look particularly attractive :wink:

He he he.

It is. Of course it is, but this works a tad faster from the command line, and the idea is really to use the comment fields as csv records, and nothing stops you from using comment in combination with sed and awk, etc. And even invoke it from a do shell script!

Apart from that, since I only use AppleEvents for setting the comments, and not reading them, it is kind of kinder to Finder, than a normal Applescript is.

And it contains AppleScript too. :slight_smile: That was the only way.

And, I fixed the osascript finally, so it doesn’t output any result to stdout, but makes one.

The code could of course have been refactored, to something more pleasant here and there, but this is a one off, and I can’t hardly see any option missing. I purport that the code is of production quality.

And before somebody starts wondering; it sets one comment on a fil at a time, to prevent congestion of the event queue. A working Finder is of the highest priority. Because I can’t know that the eventqueue are congested, before it is too late.

Hello.

I found a bug concerning getting the maximum path, the new solution should work as long as you have a root directory.

The updated code can still be found at the link provided in post #11 above.

I was reading about NSAppleScript I think it was. Can you use that?

Maybe I could, but I use CoreServices, and the MetaDataFramework to set and get the Finder comments from the command line. The problem was that I needed to send with a file, to get the system configuration parameters for the maximum component of a path, and the maximum total length, it turned out that the program name wasn’t an ideal choice when the command isn’t executed from from the same path as the command resides. Now I use “/”, which you pretty much must have to execute the program anyway, so that issue is solved. :slight_smile: