Getting a file's spotlight comment

All ~

for the life of me, I can’t figure out why I get a run time error with this:


property flag : "ABC"
property delim : "-"

tell application "Finder"
	tell folder flag of folder "Documents" of folder "bno" of folder "Users" of startup disk
		set fileList to (get name of every file) -- stores every filename in list
		set max to count files -- sets upper boundary for looping
		set needComment to 0
		display dialog max
		say "You now have " & max & " files in folder " & flag
	end tell
	
	repeat with n from 1 to 3
		set curFile to (item n of fileList)
		set curComment to (get comment of curFile) -- get contents of comment field of file   <----- ERRORS on this line with "can't get comment of file"
		display dialog curComment  -- added to see value of curComment
		
		if curComment = "" then set needComment to needComment + 1 -- counts and increments the number of files which have no comment


I’ve just started working with AS and have a directory of files which I’ve read into the list and counted. I’d like to work through several scripts:

  1. generate a list of files which have a blank comment field
  2. if the comment field is blank, set it to a specified value
  3. rather than read in every file to a list, only read in those who have a “” comment

I’m either missing a basic concept because I’m “ahead of myself” or I’ve stepped on the syntax, any help is greatly appreciated…

Hi,

fileList is a list of the file names, to read or modify the comment you need a file reference.
Try this


property flag : "ABC"
property delim : "-"

tell application "Finder"
	set fileList to files of folder flag of (path to documents folder)
	set max to count fileList
	set needComment to 0
	display dialog max as text
	say "You now have " & max & " files in folder " & flag

	repeat with n from 1 to max
		set curFile to item n of fileList
		set curComment to (get comment of curFile) -- get contents of comment field of file   <----- ERRORS on this line with "can't get comment of file"
		display dialog curComment -- added to see value of curComment
		if curComment = "" then set needComment to needComment + 1 -- counts and increments the number of files which have no comment
	end repeat
end tell

or simple version


property flag : "ABC"
tell application "Finder" to set needComment to count (files of folder flag of (path to documents folder) whose comment is "")

Stefan ~

thank you for the replies (esp the second one as it achieves in one line what I wanted [count the uncommented files])

In comparing your first suggestion to the one I posted, I looked at my effort and am still confused:

mine:


   repeat with n from 1 to 3
       set curFile to (item n of fileList)
       set curComment to (get comment of curFile)

wouldn’t these three lines set curFile to the first item in fileList, then get the comment of the first item in fileList setting it to the variable curComment?

I understand that you can’t get the comment of a list, but I thought the above 3 lines resolved that challenge, although maybe less efficiently than hoped…

I initially took the 3 line approach though as in addition to calculating the number of files where the comment is “”, i also wanted to gather all of the filename into a list if the comment was “”…

Again ~ many thanks for the assistance/explanations.

No, because item n of fileList is just a string, not a file reference, and a literal string has no comment. property.

This gives you a list of the names of all files which have non comment


property flag : "ABC"
tell application "Finder" to set needComment to name of (files of folder flag of (path to documents folder) whose comment is "")

Stefan ~

thank you for that explanation. That makes perfect sense. Have a great day.

Cheers

Thanks, I had a great day. I was in the swiss alps with my motorbike :slight_smile:

Stefan ~

In working through a couple of online tutorials and reading several chapters across a couple AS books over the past couple of days, I realized that adding the as alias modifier would make my original logic work


repeat with n from 1 to max
			set curFile to file (item n of fileList) as alias -- iterates through fileList 1 at a time
			-- applies change to curFile
			set curComment to (get comment of curFile) -- gets contents of comment field
			if curComment is "" then display dialog curFile as string -- if curComment is blank, display filename
		end repeat

this will change a literal string into a file reference which does have a comment property…

Posted for posterity for others who may run into this same challenge ~ I believe I’ve seen a couple of recent instances (in addition to my own) where something is being attempted with a list item which is a string and not a file reference…

Cheers!

No, your original script cannot work with this syntax, unless the files are located on desktop.
A file name is something like “myfile.ext”. To specify a file you need the full path like “MacHD:Users:MyUser:Desktop:myfile.ext”.
The full path can be coerced to an alias or used in a Finder tell block with the file keyword as a Finder file specifier.

A file name can not be coerced to an alias but can be used in a Finder tell block with the file keyword, but only if the file is located on desktop

Stefan ~

well, I admit that I’ve a lot to learn so I’ll go back and look at this script as I believe it worked for getting/setting a comment for the file. Many thanks for the clarification…

Cheers!