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
}
if (correspondingSwatch != null) {
for (var i = 0; i < items.length; i++) {
if (items.fillColor == swatches
}
}
}
The first and second for loop work as intended but the third for loop does not.
(items.fillColor == swatches
(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.
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
...Copy link to clipboard
Copied
Comparing color objects does not work as seen in this test:
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.
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?
Copy link to clipboard
Copied
IDE is Adobe's ESTK
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"
after running the script
// 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 ();
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
how we can lookup in entire swatches range, not a group?
Copy link to clipboard
Copied
var swatches = idoc.getAllSwatches();
Copy link to clipboard
Copied