• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Changing Colors without Swatches in Javascript

New Here ,
Dec 29, 2011 Dec 29, 2011

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

878

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guru ,
Dec 29, 2011 Dec 29, 2011

Copy link to clipboard

Copied

See this post from only the other day… Should contain 90% of what you want to do…

http://forums.adobe.com/message/4099589#4099589

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 29, 2011 Dec 29, 2011

Copy link to clipboard

Copied

Thanks Muppet Mark!  I modified the code based on that post:

var myDoc = app.activeDocument;

var oldFill = new RGBColor();

oldFill.red = 173;

oldFill.green = 173;

oldFill.blue = 132;

var newFill = new RGBColor();

newFill.red = 67;

newFill.green = 67;

newFill.blue = 181;

var paths = myDoc.pathItems;

for (var i=0; i<paths.length; i++){

    if (paths.fillColor == oldFill){

            paths.fillColor = newFill;

        }

    }

No errors anymore but nothing happens when I run the script. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 30, 2011 Dec 30, 2011

Copy link to clipboard

Copied

LATEST

You have to check values like below:

var myDoc = app.activeDocument;

var newFill = new RGBColor();

newFill.red = 67;

newFill.green = 67;

newFill.blue = 181;

var paths = myDoc.pathItems;

for (var i=0; i<paths.length; i++) {

    var f = paths.fillColor;

    if (f instanceof RGBColor // is it RGB

        && f.red.toFixed(0) == 173 //check red

        && f.green.toFixed(0) == 173 //check green

        && f.blue.toFixed(0) == 132 // check blue

    ) {

        paths.fillColor = newFill;

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines