Separating Spotlight Comments

I have a big directory of jpegs. i started adding “tags” to them using spotlight comments. now i want to convert the spotlight comments to IPTC metadata keywords. I managed to do this using Automator and Photoshop CS3, but I would like the IPTC keywords to be separated, instead of run together like a sentence. So basically I need to replace any spaces in the comments with semicolons. I have been combing the web and these forums and found some scripts that got me close, but I am new to Applescript and I can’t quite get anything to work.

This one seems closest to working. It will successfully get the comments of all the files in a folder and pass them to the function replace_chars, which works on its own in a separate applescript file with preset strings to replace spaces with semicolons. But I get an error on that function.

set Fldr to choose folder

tell application "Finder"
	set Pics to files of entire contents of Fldr -- digs down through enclosed folders
	set tKinds to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
	repeat with onePic in Pics -- iterate through the pics
		if kind of onePic is in tKinds then
			set Cmt to comment of onePic -- get the comment from the Finder
			tell AppleScript
                        set the newCmt to replace_chars(Cmt, " ", ";")
			end tell
                        set comment of onePic to newCmt
		end if
	end repeat
end tell


on replace_chars(this_text, search_string, replacement_string)
		set AppleScript's text item delimiters to the search_string
		set the item_list to every text item of this_text
		set AppleScript's text item delimiters to the replacement_string
		set this_text to the item_list as string
		set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

At first I was getting the error “Finder got an error: Can’t continue replace_chars.”
I figured the function was getting passed to the Finder instead of Applescript so I tried the tell AppleScript command. then I got the error: “«script AppleScript» doesn’t understand the replace_chars message.”

As an alternative to replace_chars, I found this method of split and join, which has not worked for me either

-- split the string into a list by some separator
to split of aString by sep
	local aList, delims
	tell AppleScript
		set delims to text item delimiters
		set text item delimiters to sep
		set aList to text items of aString
		set text item delimiters to delims
	end tell
	return aList
end split

-- join a list into a string with some separator
to join of aList by sep
	local aString, delims
	tell AppleScript
		set delims to text item delimiters
		set text item delimiters to sep
		set aString to aList as string
		set text item delimiters to delims
	end tell
	return aString
end join

AppleScript: 1.10.7
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

Hi,

instead of

tell AppleScript
set the newCmt to replace_chars(Cmt, " ", ";")
end tell

use

set newCmt to my replace_chars(Cmt, " ", ";")

the Applescript tell block is not needed, and within the Finder tell block,
you have to add my to the handler call

Welcome. :slight_smile:

Side note: I would replace the replace_chars and join subroutines. (They are both forcing Unicode text to be returned as string; Also replace_chars is making assumptions about the default text item delimiter.)

simpleReplace
implode

See also:
Text Item Delimiters are De-limitless
Tutorial for Using AppleScript’s Text Item Delimiters

Brilliant! The ‘of me’ or ‘to my’ commands worked like a charm. The simpleReplace subroutine was wiping out all my comments. Now if I can get Graphic Converter to add the new comments to the Metadata instead of running the files through Photoshop via Automator - which takes forever because Photoshop has to open and save each file, I’ll have the perfect script!

This is what I have now


set Fldr to choose folder

tell application "Finder"
	set Pics to files of entire contents of Fldr -- digs down through enclosed folders
	set tKinds to {"GIF document", "GIF Image", "JPEG document", "JPEG Image", "PNG document", "TIFF document", "TIFF Image", "Adobe Photoshop JPEG file"} -- should include your photos
	repeat with onePic in Pics -- iterate through the pics
		
		if kind of onePic is in tKinds then
			set Cmt to comment of onePic -- get the comment from the Finder
			set Cmt to replace_chars(Cmt, " ", ";") of me
			set comment of onePic to Cmt
		end if
	end repeat
end tell


on replace_chars(this_text, search_string, replacement_string)
	
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	
	return this_text
end replace_chars

Perhaps that’s because it has a different order for the parameters.