Changing the color of characters in InDesign

I’m trying to change the color of every text character in a document that has it’s fill color set to registration.
So far, I"m getting an error message that says Can’t set <> of {“V”, “o”, “r”, “t” “e”, “c”, “.”} to “Black”
So, it appears to be recognizing the characters that are in Registration" color. It just won’t change those characters to black.


on open of droppedfiles
	repeat with afile in droppedfiles
		
		tell application "Adobe InDesign CC 2014"
			tell document 1

				--Changing all registration text to black
				set registrationSwatch to swatch "Registration"
				set registrationCharacters to (every character of every word of every story whose fill color is registrationSwatch)
				tell registrationCharacters
					set fill color to "Black"
				end tell
				
			end tell
		end tell
	end repeat
end open

AppleScript: AppleScript 2.2.1
Browser: Safari 537.78.2
Operating System: Mac OS X (10.7)

Hi. Your registrationCharacters’ line dereferences the object”converting the text into list items that have no color property. You can specify that to be an object reference, but it’s easier to just prevent, by doing the actions on one line.

tell application "Adobe InDesign CS3"'s document 1
	tell (stories's characters whose fill color's name is "Registration") to if not it is {} then set fill color to "Black"
end tell

Thanks! That worked great. It’s part of a larger code that does a bunch of pre-press stuff to an InDesign file before I drop it on the other script that creates a pdf. I’ll post once the whole code is done. Maybe someone else will find it useful.

--Changing all registration text to black
					set registrationSwatch to swatch "Registration"
					tell (stories's characters whose fill color's name is "Registration") to if not it is {} then set fill color to "Black"

This code snippet works well, but does anyone know how to prevent converting characters to the “Black” swatch if their original fill is “Registration” with a tint value other than 100%? In other words, I need a script that converts only those characters filled with “Registration” at 100% tint to “Black” at 100% tint.

Thanks,
-Jeff

2 things:

  • don’t use characters: it can be very slow for long documents. Use text style ranges instead.
  • for overprint colors (like Registration or Black by default), the value of tint is always -1.
tell (text style ranges of stories whose name of fill color = "Registration" and fill tint = -1) to if not it is {} then set fill color to "Black"

Thank you, Ionah — using text style ranges instead of characters is very helpful to know!

As for what I was trying to achieve, I could only accomplish it using JavaScript. In other words, I wasn’t able to compile an AppleScript that would selectively convert only text style ranges set to Registration with a 100% tint. For example, if the tint were set to 50%, the script would still convert it to Black, which wasn’t the desired outcome.

Here’s the final script I ended up with:

tell application id "com.adobe.InDesign"
	activate
	set jsScript to "
	var doc = app.activeDocument;
	var blackSwatch = doc.swatches.itemByName('Black');
	var registrationSwatch = doc.swatches.itemByName('Registration');
	var unconvertedChars = [];
	for (var s = 0; s < doc.stories.length; s++) {
		var chars = doc.stories[s].characters;
		for (var c = 0; c < chars.length; c++) {
			var ch = chars[c];
			try {
				if (ch.fillColor == registrationSwatch) {
					var tint = ch.fillTint;
					// Change only if tint is 100 or -1
					if (tint === 100 || tint === -1) {
						ch.fillColor = blackSwatch;
					} else {
						var chVal = ch.contents;
						var code = chVal.charCodeAt(0);
						if (code >= 32 && code <= 126) {
							unconvertedChars.push(chVal);
						}
						// Else skip unprintable chars
					}
				}
			} catch (e) {}
		}
	}
	if (unconvertedChars.length > 0) {
		unconvertedChars = unconvertedChars.join('');
	} else {
		unconvertedChars = '';
	}
	unconvertedChars;
	"
	set jsResult to do script jsScript language javascript
	
	if jsResult is not "" then
		set userResponse to display dialog "Some characters using the Registration swatch were not converted to Black due to a tint value other than 100%:

" & jsResult & return & return & "You need to fix these characters by setting them with another swatch color, ideally the [Black] swatch." & return & return buttons {"Stop & Fix"} default button "Stop & Fix" with icon 0 with title "Registration Swatch Detected"
	end if
end tell

If I get correctly what’s in your javascript, you want to convert Registration colored text of any tint to Black color with the same tint.
It can’t be done like this because once you changed the color by script, the reference to the text is lost.

Try this:

tell application id "InDn"
	activate
	
	tell active document
		set theRefs to object reference of text style ranges of stories whose name of fill color = "Registration"
		repeat with aRef in theRefs
			set aTint to fill tint of aRef
			if aTint = -1 then set aTint to 100
			set fill color of aRef to "Black"
			set fill tint of aRef to aTint
		end repeat
	end tell
	
end tell

Update: the above script can be very slow on long documents.
Here is a version using Grep search.

tell application id "InDn"
	activate
	
	set find grep preferences to nothing
	set change grep preferences to nothing
	set find change grep options to nothing
	set fill color of find grep preferences to "Registration"
	set fill color of change grep preferences to "Black"
	set fill tint of change grep preferences to 100 -- remove this line if you want to keep the original tint
	change grep active document
	
end tell

Thank you ionah, I have yet to look to see if your solution works - I have been slammed at work lately. I will let you know the outcome soon! I really appreciate your help with this!

My findings, etc:
I tried both your scripts, and they are very helpful—thank you for sharing the code. I can definitely see myself using it elsewhere.

Regarding my current concern: both scripts are converting text that uses the Registration color at less than 100% tint to Black. I’d prefer this condition to be ignored. Instead, I’d like the fill to remain as Registration, and for the script to display an alert when this scenario is detected.

Thanks again for all your help. That said, there’s no need to spend much time on this—your JavaScript solution does the trick.

-Jeff