InDesign searching for text characters with a specific fill color

I’m trying to write a script to find text characters with a specific fill color so I can gather information about them and make them a different color. This is basically what I have so far. The “number of characters” will not work. I have tried “count of characters” and “length” to no avail. The line ‘display dialog “There are " & numOfChar & " characters in this text box.”’ comes up with a zero. Please help!!


tell application "Adobe InDesign CS6"
	activate
	set myDocument to active document
	set countPages to count of pages of myDocument
	tell myDocument
		set startOver to "False"
		set allPageItems to all page items as list
		set countItems to count of items in allPageItems
		display dialog "There are " & countItems & " items in the document"
		repeat with i from 1 to countItems
			try
				select page item i
				set theObject to page item i of myDocument
				set theClass to class of theObject
				if theClass is text frame then
					display dialog "This is where I do some stuff."
					set theColor to "C=0 M=100 Y=0 K=0"
					set theText to text of theObject
					set numOfChar to number of characters of theText
					display dialog "There are " & numOfChar & " characters in this text box."
					--repeat with i from 1 to numOfChar
					--	if fill color of character i of theText is theColor then
					--		display dialog "Found some magenta text."
					--	end if
					--end repeat
				else if theClass is group then
					ungroup theObject
					set startOver to "True"
					exit repeat
					display dialog "This was a group."
				end if
			on error number n
				display dialog "Got an error: #" & n
			end try
		end repeat
	end tell
end tell

Hi there,

Try this:


tell application "Adobe InDesign CC 2015"
	tell front document
		
		set colorToFind to swatch "the-color-to-find"
		set changeToThisColor to swatch "the-color-to-change-to"
		
		set x to (every word of every story whose fill color is colorToFind)
		set fill color of (every word of every story whose fill color is colorToFind) to changeToThisColor
		
	end tell
end tell

HTH

Thank you TecNik!! That works for the most part but it is skipping a couple of the text boxes. I’m wondering why it wouldn’t hit every text box.

I changed “every word” to “every character” and it works!! Now I need to get it to recognize “[None]” as a color swatch. Any suggestions? I’m trying to change every Magenta character to fill “[None]”.

I made a few minor changes to your script (see below) and it works wonderfully!! Thank you so much! Now I just need to add a little functionality to it and we’ll be good to go.

Thanks again!


tell application "Adobe InDesign CS6"
	tell front document
		
		activate
		
		set colorToFind to swatch "C=0 M=100 Y=0 K=0"
		
		set x to (every character of every story whose fill color is colorToFind)
		set fill color of (every character of every story whose fill color is colorToFind) to "None"
		
	end tell
end tell

Hi there,

Glad to hear you’ve sorted it :slight_smile:

I’d used ‘words’, instead of ‘characters’, because I thought it might be quicker. I’d also gone down that line having seen ‘text characters’ in your original post. Thinking about it again punctuation marks etc would probably not be included therefore ‘characters’ would be better.