Set Finder/Spotlight Comments

tell application "Finder"
	activate
	
	try
		set thisFolder to (target of front Finder window) as alias
	on error
		choose folder with prompt "Set comments of files in this folder:"
		set thisFolder to result
	end try
	
	display dialog "Comment:" default answer "" with title "Set Spotlight Comments"
	set newComment to text returned of result
	
	display dialog "How to handle existing comments?" buttons {"Overwrite", "Cancel", "Append"} default button 3 with title "Set Spotlight Comments"
	
	if (button returned of result) is "Overwrite" then
		set comment of every file of folder thisFolder to newComment
	else
		get every file of folder thisFolder
		repeat with thisFile in result
			tell thisFile
				if length of (comment as text) is not 0 then
					get ", " & newComment
				else
					get newComment
				end if
				set comment to (comment & result)
			end tell
		end repeat
	end if
	
	beep
end tell

Added support for “selection”:

tell application "Finder"
	activate
	set fileList to selection
	
	if (count result) is 0 then
		try
			get (target of front Finder window) as alias
		on error
			choose folder with prompt "Set comments of files in this folder:"
		end try
		
		try
			set theFolder to result
			set fileList to every file of folder (result) as alias list
		end try
	end if
	
	display dialog "Comment:" default answer "" with title "Set Spotlight Comments"
	set newComment to text returned of result
	
	display dialog "How to handle existing comments?" buttons {"Overwrite", "Cancel", "Append"} default button 3 with title "Set Spotlight Comments"
	
	if (button returned of result) is "Overwrite" then
		if (class of first item of fileList) is alias then
			set comment of every file of folder theFolder to newComment
		else
			repeat with thisFile in fileList
				set comment of thisFile to newComment
			end repeat
		end if
	else
		repeat with thisFile in fileList
			tell thisFile
				if length of (comment as text) is not 0 then
					get ", " & newComment
				else
					get newComment
				end if
				set comment to (comment & result)
			end tell
		end repeat
	end if
	
	beep
end tell

Streamlined the interface by combining two separate dialogues into one:

tell application "Finder"
	activate
	set fileList to selection
	
	if (count result) is 0 then
		try
			get (target of front Finder window) as alias
		on error
			choose folder with prompt "Set comments of files in this folder:"
		end try
		
		try
			set theFolder to result
			set fileList to every file of folder (result) as alias list
		end try
	end if
	
	display dialog "Comment:" default answer "" buttons {"Overwrite", "Cancel", "Append"} default button 3 with title "Set Spotlight Comments"
	
	set userInput to the result
	
	set newComment to text returned of userInput
	
	if (button returned of userInput) is "Overwrite" then
		if (class of first item of fileList) is alias then
			set comment of every file of folder theFolder to newComment
		else
			repeat with thisFile in fileList
				set comment of thisFile to newComment
			end repeat
		end if
	else
		repeat with thisFile in fileList
			tell thisFile
				if length of (comment as text) is not 0 then
					get ", " & newComment
				else
					get newComment
				end if
				set comment to (comment & result)
			end tell
		end repeat
	end if
	
end tell

I did this myself, but I forgot to repost it. Thanks!

Bruce, Im thinking of marking up a lot of image files with a very similar script to yours. You don’t happen to know if there is any length limit on the comment string? I wish to add keywords to files by using TID’s on a folder hierarchy. I have not yet calculated how deep this will go.

It’s a great script!
but if you save this script as an application packege, the default is itself, so you must deselect the script file when the script running prompt,and then select the files you want to comment. Need a little modifcation for the selection part.

I use the Script Menu for running most of my scripts.

I rewrote the script a bit to work also with Launchbar.
Actually I mixed it with another one I found here: http://forums.obdev.at/viewtopic.php?f=24&t=137&start=0

  • You can select files with LaunchBar OR Finder.
  • If no item is selected on run it will open a “Choose Dialog”.
  • You can see current tags of selected items … even if you don’t enter new tags.
  • You could run the script also via e.g. the Script Menu … though it depends on LaunchBar for “large type”

The only thing I couldn’t get to work is Growl Notifications. I’m using LaunchBars “large type” feature instead for now. Any ideas?

-- »Tags« - Add Spotlight comments to your files and folders (with LaunchBar or Finder)!
-- by Ptujec 18-08-09
--
-- Sources:
-- http://macscripter.net/viewtopic.php?pid=99577#p99577
-- http://forums.obdev.at/viewtopic.php?t=137
----------------------------------

on run
	tell application "Finder"
		activate
		set itemList to selection
		
		if (count result) is 0 then
			tell me to activate
			choose file with multiple selections allowed without invisibles
			set itemList to result
		end if
		
	end tell
	
	tell me to activate
	tagging(itemList)
end run


on open (itemList)
	
	tell me to activate
	tagging(itemList)
end open

----------------------------------

on tagging(itemList)
	
	display dialog "Comment:" default answer "" buttons {"Cancel", "Overwrite", "Append/Show"} default button 3 with title "Spotlight comments"
	set userInput to the result
	set newTags to text returned of userInput
	
	-- copy new Tags to clipboard
	if newTags is not "" then
		set the clipboard to newTags
	end if
	
	
	tell application "Finder"
		
		if (button returned of userInput) is "Overwrite" then
			
			repeat with this_item in itemList
				set item_name to (get displayed name of this_item)
				
				set comment of this_item to newTags
				
				if newTags is not "" then
					-- If you don't use LaunchBar you can substitude the next tell section with:
					-- display dialog "One or more items untagged!"
					
					tell application "LaunchBar"
						display in large type "Item(s) tagged: " & "\n" & newTags as text
					end tell
				else
					tell application "LaunchBar"
						display in large type "All items untagged!" as text
					end tell
					
				end if
			end repeat
		else
			
			repeat with this_item in itemList
				set item_name to (get displayed name of this_item)
				set current_com to (get comment of this_item)
				if current_com does not contain newTags then
					if current_com is "" then
						set comment of this_item to newTags
						tell application "LaunchBar"
							display in large type "Item(s) tagged: " & "\n" & newTags as text
						end tell
					else
						set comment of this_item to (current_com & ", " & newTags) as string
						tell application "LaunchBar"
							display in large type "Item(s) tagged: " & "\n" & current_com & ", " & newTags as text
						end tell
						
						
					end if
				else
					-- If you don't use LaunchBar you can substitude the next tell section with:
					-- display dialog "One or more items already tagged: " & "»" & newTags & "«"
					
					tell application "LaunchBar"
						display in large type "Item(s) already tagged: " & "\n" & current_com as text
					end tell
					
				end if
			end repeat
		end if
		
	end tell
	
	--- option autohide
	-- tell application "LaunchBar"
	-- 	delay 1.5
	-- 	hide
	-- end tell
	
end tagging

I tried to set as comment the actual file name for each file in the chosen folder from your script however I am not skilled enough to modify it.
I need to do so to be able to edit when needed certain specific images from a Quicktime movie.

Is rather long and useless to expain why I need such thing. However it is the only way for me to solve some Editing problems in that movie.

Could you help and tell me how to modify the “insert a comment valid for all files in the folder through the dialog” to a set the comment with each file name (not path) as performed from the script.

Thanks

Browser: Safari 531.9
Operating System: Mac OS X (10.6)