Using Illustrator Scripts to Modify Colors

Automating color changes across Illustrator artwork with ExtendScript — understanding RGB and CMYK color models and scripting color transformations.

← Back to Blog

Why Script Color Changes?

Manually changing colors across large Illustrator documents — particularly when a brand recolor or a spot color substitution is needed across hundreds of objects — is time-consuming and error-prone. Illustrator's scripting support (via ExtendScript and the Illustrator DOM) makes it straightforward to automate these changes systematically.

RGB vs CMYK in Illustrator

Before writing color scripts, it's important to understand how Illustrator represents color internally:

  • RGB colors are represented as three values (red, green, blue), each in the range 0–255. Used for screen-based and web output.
  • CMYK colors are represented as four values (cyan, magenta, yellow, black), each in the range 0–100. Used for print output.
  • Spot colors are named colors (e.g. Pantone values) with a tint value. They cannot be directly converted to RGB/CMYK without losing the spot color identity.

A document can contain objects with mixed color modes. Your script needs to handle each type appropriately.

Accessing Colors via ExtendScript

In the Illustrator scripting DOM, path items have fillColor and strokeColor properties. The color type determines which properties are available:

var doc = app.activeDocument;
var items = doc.pathItems;

for (var i = 0; i < items.length; i++) {
    var item = items[i];
    if (item.filled) {
        var color = item.fillColor;
        if (color.typename === "RGBColor") {
            // Access color.red, color.green, color.blue
        } else if (color.typename === "CMYKColor") {
            // Access color.cyan, color.magenta, color.yellow, color.black
        } else if (color.typename === "SpotColor") {
            // Access color.spot.name, color.tint
        }
    }
}

Replacing a Specific Color

To replace all objects with a specific RGB color with a new color:

function replaceRGBColor(doc, oldR, oldG, oldB, newR, newG, newB) {
    var items = doc.pathItems;
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.filled && item.fillColor.typename === "RGBColor") {
            var c = item.fillColor;
            if (Math.round(c.red) === oldR &&
                Math.round(c.green) === oldG &&
                Math.round(c.blue) === oldB) {
                var newColor = new RGBColor();
                newColor.red = newR;
                newColor.green = newG;
                newColor.blue = newB;
                item.fillColor = newColor;
            }
        }
    }
}

replaceRGBColor(app.activeDocument, 255, 0, 0, 0, 0, 255); // Replace red with blue

Iterating Through All Object Types

The example above only covers top-level path items. A complete solution needs to handle grouped objects and other container types recursively:

function processItem(item) {
    if (item.typename === "PathItem") {
        // Apply color change
    } else if (item.typename === "GroupItem") {
        for (var i = 0; i < item.pageItems.length; i++) {
            processItem(item.pageItems[i]);
        }
    } else if (item.typename === "CompoundPathItem") {
        for (var i = 0; i < item.pathItems.length; i++) {
            processItem(item.pathItems[i]);
        }
    }
}

var doc = app.activeDocument;
for (var i = 0; i < doc.pageItems.length; i++) {
    processItem(doc.pageItems[i]);
}

Converting Between Color Modes

Converting a CMYK colour to RGB requires a conversion formula. Note that the conversion is not perfectly reversible due to the gamut differences between the two colour spaces. For accurate results, use Illustrator's built-in colour management rather than manual formula conversion wherever possible. See our article on Understanding Colour Models: RGB and CMYK for a deeper explanation of how these colour spaces relate.

Handling Spot Colours in Scripts

Spot colours are the most common source of confusion when scripting colour changes in Illustrator. A spot colour is not defined by channel values alone — it has a name (typically a Pantone reference), a colour book, and a tint value. The actual ink mixture is defined by the spot's colorant values, which are used for on-screen preview but do not represent what the press uses.

When you encounter a SpotColor in a script, you can access its name via color.spot.name and its tint via color.tint. If your goal is to replace one named spot colour with another, you search the document's swatches for the target spot and assign it:

function replaceSpotColor(doc, oldSpotName, newSpotName) {
    var newSwatch = null;
    for (var s = 0; s < doc.swatches.length; s++) {
        if (doc.swatches[s].name === newSpotName) {
            newSwatch = doc.swatches[s];
            break;
        }
    }
    if (!newSwatch) return; // target spot not in document

    var items = doc.pathItems;
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.filled && item.fillColor.typename === "SpotColor") {
            if (item.fillColor.spot.name === oldSpotName) {
                var newColor = new SpotColor();
                newColor.spot = newSwatch.color.spot;
                newColor.tint = item.fillColor.tint;
                item.fillColor = newColor;
            }
        }
    }
}

This preserves the tint value of the original spot while substituting the named colour — useful when rebranding artwork that uses tints of a spot colour throughout.

Lab Colours and Global Colour Adjustments

Illustrator also supports Lab colours (LabColor) with l, a, and b properties. Lab is a device-independent colour model that represents colours as a human would perceive them, which makes it a useful intermediate space when doing automated colour adjustments that need to be visually consistent across different output profiles. If you are writing a script that shifts all artwork colours towards a slightly warmer tone, doing the calculation in Lab and then converting back to CMYK will typically produce more predictable results than manipulating CMYK channel values directly.

Batch Processing Across Multiple Files

The real power of colour scripting comes when you apply it across a folder of files — a brand recolour across an entire asset library, for instance, or converting a suite of documents from RGB to print-ready CMYK. Illustrator's scripting API can open, process, and save files without user interaction, making genuine batch operations practical.

var folder = Folder.selectDialog("Select folder of AI files");
if (folder) {
    var files = folder.getFiles("*.ai");
    for (var f = 0; f < files.length; f++) {
        var doc = app.open(files[f]);
        // apply colour changes here
        doc.save();
        doc.close();
    }
}

For larger libraries — hundreds or thousands of files — you are better served by combining this scripting approach with Illustrator's Action Manager or by building a proper Illustrator plugin that runs the batch with a progress indicator and error logging. Bare scripts can fail silently on problematic files, leaving you unsure which files were processed correctly.

Tips for Production-Quality Colour Scripts

A few things that consistently trip up colour scripts in real production environments. First, locked layers. Illustrator will throw an error if you try to modify an object on a locked layer. Check item.layer.locked before attempting any modification, and skip locked items or unlock them explicitly if your workflow requires it.

Second, hidden objects. Objects on hidden layers are accessible via the API but will not reflect your changes visually until the layer is shown. If your script is processing hidden layers unintentionally, the results can be confusing. Filter by item.hidden if you only want to process visible artwork.

Third, text frames. TextFrame objects have their own character attributes with colour properties separate from path items. If your artwork includes coloured text that also needs to change, you need a separate traversal of doc.textFrames and manipulation of each character's colour through textFrame.characters[i].fillColor. Text colour scripting is more verbose but follows the same colour type pattern as path items.

Finally, always test on a copy of your files. Colour scripts that modify and save files in place have no undo once the save has completed. Build a convention of writing to a separate output folder rather than overwriting the originals.

Further Reading

For more on Illustrator scripting and colour handling, see the Adobe Illustrator Scripting Reference. For custom colour automation solutions, contact Mapsoft — we build bespoke Illustrator scripts and plugins for production workflows. See also our article on Combining Illustrator Extension Technologies and our deeper look at spot colours in print production.

Related Articles

History of Adobe Illustrator

Explore the history of Adobe Illustrator, from its launch as Illustrator 88 in 1987 to its current status as the industry standard in vector graphics editing.

Combining Illustrator Extension Technologies

Explore how combining ExtendScript, C++ SDK plugins, and modern UI frameworks unlocks the full potential of Adobe Illustrator automation and extensibility.

Switching Python Versions for Illustrator SDK Development

Learn how to switch between Python versions for Adobe Illustrator SDK plugin development on Windows and macOS. Includes batch scripts and pyenv setup instructions.

Custom Illustrator Automation

Mapsoft builds Illustrator scripts, plugins, and batch processing solutions for design and production workflows.

Get Adobe Creative Cloud →