add spotlight comments read from file: kMDItemFinderComment=(NULL)

Hello Macscripters,

i wrote an Applescripts that reads comments from a text file and adds them as spotlight comments to my files.
This text file was generated as a tab seperated file from Filemaker export and looks like this.

Users:xxx:Desktop:spotlight:folder1: file_38_01 COMMENT1, COMMENT TEXT1 PNG Bild
Users:xxx:Desktop:spotlight:folder1: file_49_01 COMMENT2, COMMENT TEXT2 PNG Bild
Users:xxx:Desktop:spotlight:folder2: file_01_01 COMMENT3, COMMENT TEXT3 PNG Bild
Users:xxx:Desktop:spotlight:folder2: file_01_02 COMMENT4, COMMENT TEXT4 PNG Bild
Users:xxx:Desktop:spotlight:folder3: file_43_01 COMMENT5, COMMENT TEXT5 PNG Bild

I have 4 columns to extract all the information that I need. In my script, all comments from my textfile where added as spotlight comments and if I hit cmd+I on each file I can see the comments but in spotlight search they where not displayed.

If I do a $mdls file_38_01.png | grep Comment in the terminal I did not get a result for kMDItemFinderComment. It is NULL

If I do not read the comments to be added from the tab seperated file
but as a string in the script the spotlight search is working.
This just drives me nuts since two days.
I supposed it is an issue with the string or the text file.
Do I have to read my list in a different way or as Unicode text?
Or do I have to update the spotlight index?
Can someone help me out here?

set theFile to "/Users/xxx/Desktop/spotlight/myspotlight.txt"
set theFileReference to open for access theFile
set theData to read theFileReference using delimiter return
close access theFileReference

set theBigList to {}
set text item delimiters to tab
repeat with i from 1 to count of theData
	set theLine to text items of item i of theData
	copy theLine to the end of theBigList
end repeat

set ext to ".png"

repeat with i from 1 to (count of theBigList)
	set text item delimiters to ""
	set theSFXPathname to item 1 of item i of theBigList
	set theSFXFilename to item 2 of item i of theBigList
	set theSpotlightComment to item 3 of item i of theBigList
	set theFileType to item 4 of item i of theBigList
	set theCompleteURL to theSFXPathname & theSFXFilename
	set theSlashFilename to POSIX path of theCompleteURL -- ////
	if theFileType is equal to "PNG-Bild (Portable Network Graphics)" then
		tell application "Finder"
			if exists theSlashFilename as POSIX file then
				display dialog theFileType & " " & theSlashFilename
				do shell script "mv " & theSlashFilename & " " & theSlashFilename & ext
			end if
		end tell
	end if
	set theSlashFilename to theSlashFilename & ext
	set theDoublePointFilename to file theSlashFilename as POSIX file -- ::::
	--set theAlias to alias theDoublePointFilename
	tell application "Finder" to set comment of file theDoublePointFilename to (theSpotlightComment)
	--tell application "Finder" to set comment of theAlias to (theSpotlightComment as string)
	display dialog (do shell script "mdls " & theSlashFilename & " | grep Comment")
end repeat

display dialog "All files has been added with spotlight comment"

Model: Mac Book Pro 2GHz Core i7 | 4GB RAM
AppleScript: 2.4.2
Browser: Firefox 12.0
Operating System: Mac OS X (10.7)

Spotlight Comments are notoriously bad. I would suggest not using them for searchable data and to use an OpenMeta enabled application.


For the sake of transparency, I work for Ironic Software, the creators of the OpenMeta standard. But this is not a sales pitch, it is just trying to help someone with a technology that works.

The Finder’s comments and Spotlight’s kMDItemFinderComment metadata are not the same thing. The Finder saves its comments for itself (in .DS_Store files). Spotlight should import them, but there’s no guarantee – and you certainly can’t expect it to happen instantly.

Thanks for your replies.
I know that spotlight comments are not the best way for adding meta data.
But for a quick and dirt solution I just want to give it try.
And I finally found my fault. All my files where read-only. I added write access and now all the comments where found using spotlight search.

Regards schdefan