Adding Spotlight metadata to Iphoto images or photo library

I am trying to make it easy to add comments to images so they can
be found easily using the spotlight search engine. None of my images
have comments yet and I have hundreds if not thousands. I am trying
to make a simple script that will search for only images that have not
had comments entered yet. Please help.

problem #1 I cannot seem to filter items by comment like you can by extention.
problem #2 I cannot get Preview to close the window, just to quit.


set this_folder to (choose folder with prompt "Pick the folder containing the images to add comments too") as string -- pick the folder
tell application "System Events" -- make sure they are images that don't have comments already
	set these_files to every file of folder this_folder whose name does not start with "." and (file type is "TIFF" or file type is "JPEG" or name extension is "tiff" or name extension is "tif" or name extension is "jpeg" or name extension is "jpg") --problem #1
end tell
repeat with i from 1 to the count of these_files --line all the images up to run one after another
	set thePicture to (item i of these_files as alias)
	tell application "Preview"
		open thePicture --open the image of the first picture in Preview	
	end tell
	set stringToBeDisplayed to "Add your comment" -- make a window to add commments to the photo
	set tempVar to display dialog stringToBeDisplayed default answer "" buttons {"Quit", "Ok"} default button "Ok"
	set theButtonPressed to button returned of tempVar
	if theButtonPressed is "Quit" then --if Quit is selected then quit
		say "good by"
		tell application "Preview"
			quit
		end tell
	else
		set commentEntered to text returned of tempVar --make the text entered the spotlight comments
		tell application "Finder"
			set the comment of thePicture to the commentEntered
			tell application "Preview"
				close front window --problem #2
			end tell
		end tell
	end if
end repeat

Both problems should be fixable, bigtex:

Solution #1: Try using Finder, which recognises comments, rather than System Events.
Solution #2: Besides a few common commands, Preview isn’t scriptable - so we’ll have to resort to UI scripting instead.

Here’s an adaptation of your script that should demonstrate these suggestions:

to getFiles from this_folder
	tell application "Finder"
		set these_files to a reference to (this_folder's files whose name does not start with ¬
			"." and comment is "" and (file type is "TIFF" or file type is "JPEG" or name extension is ¬
			"tiff" or name extension is "tif" or name extension is "jpeg" or name extension is "jpg"))
		try
			these_files as alias list
		on error number -1700
			these_files as alias as list
		end try
	end tell
end getFiles

on commentEntered for this_file
	tell application "Preview"
		activate
		open this_file
		set {button returned:buttonReturned, text returned:textReturned} to display dialog ¬
			"Add your comment:" default answer "" buttons {"Quit", "OK"} default button "OK"
	end tell
	if buttonReturned is "Quit" then
		tell application "Preview" to quit
		error number -128
	end if
	tell application "System Events" to keystroke "w" using command down
	textReturned
end commentEntered

set this_folder to choose folder with prompt "Choose a folder containing images requiring comments:"

repeat with this_file in (getFiles from this_folder)
	tell application "Finder" to set comment of this_file to my (commentEntered for this_file)
end repeat

thanks kai! :smiley: