Illustrator Scripts for changing colors
These are two scripts that are almost identical but one uses RGB colours and the other CMYK. For these to work correctly the Illustrator document needs to be put into the correct color mode (RGB or CMYK) from the File menu/Document Color Mode.
The parts of the scripts that you would need to change are at the top of the scripts. So for example in the CMYK variant of the script you would need to change one or more of the four values for the original color (the color that you are searching for) and the new color that you are replacing it with.
Be aware that this code is searching for paths and not images or text. For it to work with text it should have been converted to outlines before the code is run. The code could be enhanced to add this additional functionality. It would also be possible to add a CEP (HTML and JavaScript) user interface so that users could change the colors that way rather than directly in the script.
You will also see two flags:
var changeFill = true;
var changeStroke = true
This are currently set so that both stroke and fill colors will be changed. Switching either of these to false will toggle that switch that setting off.
This is the CMYK variant:
// Define the original color and the new color
var originalColor = new CMYKColor();
originalColor.cyan = 100;
originalColor.magenta = 100;
originalColor.yellow = 0;
originalColor.black = 0;
var newColor = new CMYKColor();
newColor.cyan = 0;
newColor.magenta = 0;
newColor.yellow = 100;
newColor.black = 0;
var changeFill = true;
var changeStroke = true
// Get the active document
var doc = app.activeDocument;
// Get all the top-level items in the document
var topLevelItems = doc.pageItems;
// Loop through all the top-level items
for (var j = 0; j < topLevelItems.length; j++) {
// Check if the item is a path item
if (topLevelItems[j].typename == "PathItem") {
var fillColor = topLevelItems[j].fillColor;
var strokeColor = topLevelItems[j].strokeColor;
if(fillColor && changeFill){
// Check if the fill color is the original color
if (fillColor.typename == "CMYKColor" && fillColor.cyan == originalColor.cyan && fillColor.magenta == originalColor.magenta && fillColor.yellow == originalColor.yellow && fillColor.black == originalColor.black) {
// Change the fill color to the new color
topLevelItems[j].fillColor = newColor;
}
}
if(strokeColor && changeStroke){
// Check if the stroke color is the original color
if (strokeColor.typename == "CMYKColor" && strokeColor.cyan == originalColor.cyan && strokeColor.magenta == originalColor.magenta && strokeColor.yellow == originalColor.yellow && strokeColor.black == originalColor.black) {
// Change the stroke color to the new color
topLevelItems[j].strokeColor = newColor;
}
}
}
}
This is the RGB variant:
// Define the original color and the new color
var originalColor = new RGBColor();
originalColor.red = 255;
originalColor.green = 0;
originalColor.blue = 0;
var newColor = new RGBColor();
newColor.red = 0;
newColor.green = 255;
newColor.blue = 0;
var changeFill = true;
var changeStroke = true
// Get the active document
var doc = app.activeDocument;
// Get all the top-level items in the document
var topLevelItems = doc.pageItems;
// Loop through all the top-level items
for (var j = 0; j < topLevelItems.length; j++) {
// Check if the item is a path item
if (topLevelItems[j].typename == "PathItem") {
var fillColor = topLevelItems[j].fillColor;
var strokeColor = topLevelItems[j].strokeColor;
if(fillColor && changeFill ){
// Check if the fill color is the original color
if (fillColor.typename == "RGBColor" && fillColor.red == originalColor.red && fillColor.green == originalColor.green && fillColor.blue == originalColor.blue) {
// Change the fill color to the new color
topLevelItems[j].fillColor = newColor;
}
}
if(strokeColor && changeStroke){
// Check if the stroke color is the original color
if (strokeColor.typename == "RGBColor" && strokeColor.red == originalColor.red && strokeColor.green == originalColor.green && strokeColor.blue == originalColor.blue) {
// Change the stroke color to the new color
topLevelItems[j].strokeColor = newColor
}
}
}
}
The scripts can be run by copying the code from this blog (just use the copy button on the top right) and saving either of them into files with a .js extension (for example ConvertColorRGB.js and ConvertColorCMYK.js). They can then be run from the Illustrator File/Scripts/Other Scripts menu.