Changing Colors in Illustrator with Javascript

I'm very new to javascript and I am writing a script to find all pathItems of a specified fill color and change them to another fill color. This must be done in RGB or hex without using swatches. So far I've put together bits of other scripts I found but I'm running into a lot of errors. Here is what I have so far:

var myDoc =app.activeDocument
var fillRGBColor = function (pathItem){
    var fillColor = new Array();
    fillColor[0] = myDoc.pathItem.fillColor.red;
    fillColor[1] = myDoc.pathItem.fillColor.green;
    fillColor[2] = myDoc.pathItem.fillColor.blue;
    return fillColor;
}

fillRGBColor();
var pathItems = myDoc.pathItems;
for (i=0; i<pathItems.length; i++){
    fillColor[255,255,255] ==fillColor[50,50,50];
}

Thank you!

Answers:

Answer

I needed this same question answered, but the existing answer was incomplete. Here is a full working solution. You specify the "from" RGB and the "to" RGB colors and call fillRGBColor for as many colors you need to change.

The next step is to figure out how to automate this script for more than 1 illustrator file :)

var fillRGBColor = function (pathItems, fr,fg,fb,tr,tg,tb){
    for (var i=0; i < pathItems.length; i++) {

        if (pathItems[i].fillColor.red == fr
           && pathItems[i].fillColor.green == fg
           && pathItems[i].fillColor.blue == fb) {
                pathItems[i].fillColor.red = tr;
                pathItems[i].fillColor.green = tg;
                pathItems[i].fillColor.blue = tb;
        }

    }
}

fillRGBColor(app.activeDocument.pathItems, 20, 20, 20, 50, 50, 50);
Answer

Indeed you have a lot of erros in your script. Looking at the type of errors I suggest you to read Adobe Illustrator CS5 Reference: JavaScript or any JavaScript tutorial.

In any case you can reduce your JavaScript errors by my modified version of your code.

var fillRGBColor = function (pathItems, r, g, b){
    for (var i=0; i < pathItems.length; i++) {
        pathItems[i].fillColor.red = r;
        pathItems[i].fillColor.green = g;
        pathItems[i].fillColor.blue = b;
    }
}

fillRGBColor(app.activeDocument.pathItems, 50, 50, 50);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.