Read Comments from File?

How can I read the comments from a single (selected) file, in Finder, via AppleScript?

I’ve been unable to successfully read the Spotlight Comments field (called “Comments” in the info pane) from a single file.

The closet I have found is the following, but it is not exactly what I am looking for, and my attempts to edit it have resulted in errors.


tell application "Finder"
	set the_strings to ""
	repeat with this_file in (get items of (choose folder))
		set the_strings to the_strings & name of this_file & return & comment of this_file & return & return
	end repeat
end tell

Browser: Safari 605.1.15
Operating System: macOS 10.14

I don’t know if it works with 10.14 but the script below does with 10.13.6.

set aFile to ((path to desktop as string) & "Viugreun.rtf") as alias
tell application "Finder"
	comment of aFile --> "blahblahblah"
end tell

It’s just what is described in the Finder’s dictionary.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 12 juillet 2020 16:53:33

Thank you. I don’t know why the “as alias” is needed, but it is. Also, I did not realize that I could simply state “comment” without a verb like “get comment” or setting the comment to a variable and then pulling from that.

Anyway, here is the script that I have incorporated into my Automator workflow:


tell application "Finder"
	set theFile to selection as alias
	comment of theFile
end tell

That may be related to the version of macOS, as all of the following work on Catalina. These scripts assume only one file is selected in Finder.


-- Finder file format
tell application "Finder"
	set theFile to selection
	set theComment to comment of (item 1 of theFile)
end tell

-- Alias
tell application "Finder"
	set theComment to comment of (selection as alias)
end tell

-- Coerce to text
tell application "Finder"
	set theComment to comment of file (selection as text)
end tell

The alias part isn’t necessary, but the coercion converts the Finder selection—which is always a list—to a singular item; without that workaround, you’ll need to target a specific list item. The ‘get’ verb in most cases is implicit, however, there are instances where something isn’t realized without it, and then it must be explicit. You can see when it’s needed by regularly watching the event log in the Editor.

Adding to your solution.

Documentation what action in Automator will return is not maybe always clear.
But there is ways to debug it by asking for class

ex.

tell application "Finder" to set theSelection to get its selection as list
its class of theSelection
--> list
--> {document file "Eagle.tif" of folder "Animals" of folder "User Pictures" of folder "Library" of startup disk}
tell application "Finder" to set theSelection to get its selection as alias
its class of theSelection
--> alias
--> {alias "Macintosh HD:Library:User Pictures:Animals:Eagle.tif"}
tell application "Finder" to set theSelection to get its selection as text
its class of theSelection
--> text
--> {"Macintosh HD:Library:User Pictures:Animals:Eagle.tif"} 

So if Automator workflow do not work it could be that one action ask for ‘class A’ and get ‘class B’
In other words Automator do not give that information if we do not ask for it.

I’m sure many people have made that mistake, include me.