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

jsx script illustrator - replace items color with a corresponding swatch pattern

New Here ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

I am attempting to create a script to replace all the colors in a picture with a number pattern. I have created a script but it does not work as intended.

The picture is coloured with swatches and need to be replaced by a swatch with the same name but for a "." at the end, i.e. all items of colour Bh will need to be replaced with Bh. pattern.

doc = activeDocument,

swatches = doc.swatches,

items = doc.pathItems;

var correspondingSwatch;

for (var c = 0; c < swatches.length; c++) {

    correspondingSwatch = null

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

        if (swatches.name == swatches.name + "." ) correspondingSwatch = swatches;

    }

    if (correspondingSwatch != null) {

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

            if (items.fillColor == swatches.color) items.fillColor= correspondingSwatch.color;

        }

    }

}

The first and second for loop work as intended but the third for loop does not.

(items.fillColor == swatches.color) i want this to return true when the color of the item is the same as the swatches but it does not.

(items.fillColor  = correspondingSwatch.color) i want this to fill in the item with the correspondingSwatch pattern, not the color. However I do not know the syntax to use a swatches color to paint an item.

Thank you any one who gives this a read, i am familiar with programming but am just starting on adobe illustrator scripting.

If you could change the code and send it back that would be great, or even telling me what methods or variables to use.

TOPICS
Scripting

Views

7.4K

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

correct answers 1 Correct answer

Community Expert , Jan 07, 2018 Jan 07, 2018

since we're in a controlled environment, I assume all swatches have their corresponding pattern replacement swatch. That being said, we don't need to compare swatch names (we assume all pattern swatches correctly named exist).

to avoid looping through all swatches in a document (in case there are hundreds of swatches), I recommend moving all your cat swatches into their own group. Then we safely go through all swatches in the swatch group, get their swatch names, select all with the same color, g

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

Comparing color objects does not work as seen in this test:

Screen Shot 2018-01-07 at 11.03.13 AM.png

So for process colors, we have to compare the color values. But for pattern colors, it's a whole different animal - you would have to look at the name property of the PatternColor.pattern object, and compare that name to the name of your swatch.

Screen Shot 2018-01-07 at 10.51.03 AM.png

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 ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

1. Thanks a lot for demonstrating the fault with my color comparison, however I don't understand how to compare the color values, do you mean compare the numbers that represent the colours i.e. #FF0000 (red) and if that is what you mean, can you tell me the syntax to compare the color values.

2. I want to have a script to automatically change a picture to a painting by numbers picture, as select ->same-> fill color, then clicking on the new fill color for every single distinct color is extremely tedious and repetitive and i need to do it every time i create a painting by numbers picture.

(an example of the intended use)

I don't need the colour of the pattern swatch, I need to be able to paint an item with the pattern.

I need a way to paint an item with a swatch pattern. i can imagine it looks something like item.fillWith(swatch) or  item.fill = swatch. However, have been unable to find the correct syntax to paint an item with a swatch pattern. If you knew the code to do this it would be amazing.

3. Thanks a lot for the fast reply, I am also very interested in your ide (looks a bit nicer than notepad++), could you please tell me the name?

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
Community Expert ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

IDE is Adobe's ESTK

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
Community Expert ,
Jan 07, 2018 Jan 07, 2018

Copy link to clipboard

Copied

since we're in a controlled environment, I assume all swatches have their corresponding pattern replacement swatch. That being said, we don't need to compare swatch names (we assume all pattern swatches correctly named exist).

to avoid looping through all swatches in a document (in case there are hundreds of swatches), I recommend moving all your cat swatches into their own group. Then we safely go through all swatches in the swatch group, get their swatch names, select all with the same color, get the corresponding pattern swatch and replace colors

move all swatches to their own group, in this example I named my swatch group "colors"

replaceSwatches0.PNG

after running the script

replaceSwatches1.PNG

// replace swatch colors

// CarlosCanto

// https://forums.adobe.com/thread/2434441

function main() {

    var idoc = app.activeDocument;

    var colorgroup = idoc.swatchGroups['colors'];

   

    var swatches = colorgroup.getAllSwatches();

    var swatchcount = swatches.length;

    var swatch, a, replacementSwatch;

    for (a=0; a<swatchcount; a++) {

        swatch = swatches;

       

        replacementSwatch = idoc.swatches[swatch.name+'.'];

       

        // deselect All

        idoc.selection = null;

       

        idoc.defaultFillColor = swatch.color;

       

        // select same fill

        app.executeMenuCommand("Find Fill Color menu item");

       

        // apply replacement swatch color to selection

        idoc.defaultFillColor = replacementSwatch.color;

    }

}

main ();

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
Contributor ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

Hi,

the following line is the replacementColor

// apply replacement swatch color to selection

idoc.defaultFillColor = replacementSwatch.color;

 

Ist there a way to use a named swatch (e.g. "darkblue") as a replacement?

 

Thanks Stefan

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
Explorer ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

Nice. Might be misunderstanding, but shouldn't

  swatch = swatches;

be

  swatch = swatches[a];

?

 

Thanks!

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
Community Expert ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

Yes, that's how it should be and that's how it was.

 

Unfortunately, the changeover to the new forum software destroyed a large part of the old code. Since then, the counter in square brackets has almost always been missing somewhere in the old topics.

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
Explorer ,
Apr 28, 2024 Apr 28, 2024

Copy link to clipboard

Copied

LATEST
Oops!

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
Explorer ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

how we can lookup in entire swatches range, not a group?

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
Community Expert ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

var swatches = idoc.getAllSwatches();

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
Contributor ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

var swatches = idoc.swatches works for me

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