Select Text in InDesign-CS2

I am trying to write a script that looks at all of the text boxes on my page, and changes the fill color of the text based on what the original color was. I can get the script to work on the fill color of the text box itself and I can also get it to work on a single character, but not all the characters in the box. I would appreciate any pointers.

I am posting the 2 scripts that are working to show you how far I’ve gotten. Thanks so much for all your help!

tell application "Adobe InDesign CS2"
	tell active document
		set myBlackSwatch to swatch "Black"
		set redswatch to swatch "red"
		set blueswatch to swatch "blue"
		
		set TextBoxCount to the count of every text frame
--this works, changing the background fill of all the text boxes.
		repeat with i from 1 to TextBoxCount
			tell text frame i
				if fill color is myBlackSwatch then
					set fill color to redswatch
				else
					set fill color to blueswatch
				end if
			end tell
		end repeat
	end tell
end tell
tell application "Adobe InDesign CS2"
	tell active document
		set myBlackSwatch to swatch "Black"
		set redswatch to swatch "red"
		set blueswatch to swatch "blue"
		
		set TextBoxCount to the count of every text frame
		repeat with i from 1 to TextBoxCount
			tell text frame i				
--alt this works to change the color of letter 1, but i can't make select every character work
				select character 1
				if fill color of character 1 is myBlackSwatch then
					set fill color of character 1 to redswatch
				else
					set fill color of character 1 to blueswatch
				end if
			end tell
		end repeat
	end tell
end tell

Try this method - should be substantially faster - and it’s much simpler.

tell application "Adobe InDesign CS2"
	tell active document
		set myBlackSwatch to swatch "Black"
		set redswatch to swatch "red"
		set blueswatch to swatch "blue"
		tell (every character of every text frame of every page whose fill color ≠ myBlackSwatch) to set fill color to blueswatch
		tell (every character of every text frame of every page whose fill color is myBlackSwatch) to set fill color to redswatch
	end tell
end tell

That works, but the reason I was trying an if/else loop is because if the only text color in the document is Black, the first tell fails because InDesign can’t find any items with that color, and then the script stops because of the error. Do you have any suggestions on how I can make the script continue to the next tell even if the first one fails?

Thanks so much for your help!

OK, I figured out how to get the script to continue. I used a try statement for each color change and had an error message display if the try failed. That seems to work!

Thanks so much!!