Copy link to clipboard
Copied
I know how to loop through pathItems and change the fillColor, but how can I loop through the pathItems and select the first pathItem that matches a specific fillColor?
I use this script to change fillColor when my pathItems have specific names.
var aDoc = app.activeDocument;
aDoc.selection = null;
var sel, textColor, currentPath = null;
selectPathItem ("5", 4);
function selectPathItem (getName, greyColor) {
try { /* in case that name doesn't exists */
var aPath = aDoc.pathItems.getByName (getName);
aPath.selected = true;
app.executeMenuCommand ("Find Fill Color menu item");
textColor = new GrayColor ();
textColor.gray = greyColor;
loopSelection ();
}
catch (e){}
}
function loopSelection () {
sel = aDoc.selection;
for (var i = 0; i < sel.length; i++) {
currentPath = sel[i];
currentPath.filled = true;
currentPath.fillColor = textColor;
}
app.executeMenuCommand ("lock");
}
So now I just have a 1,000's of pathItems all named the same thing <Path>. I want to loop through them and compair them to a specific color like 5 on the gray scale. Select that one then run app.executeMenuCommand ("Find Fill Color menu item") and finaly rename all the pathItems selected. I can do the renaming part I just can't figure out how to find the pathItem by fillColor.
Does anyone know how to do this?
Copy link to clipboard
Copied
#target Illustrator
function test()
{
var aDoc = app.activeDocument;
var testColor = new GrayColor();
testColor.gray = 5;
aDoc.defaultFillColor = testColor;
app.executeMenuCommand("Find Fill Color menu item");
alert("Selected " + aDoc.selection.length + " items.");
}
test();
Copy link to clipboard
Copied
I'm struggling to find a way to compair a pathItems fillColor to a specific color. I thought the code below would work but it doesn't do anything.
function test()
{
var aDoc = app.activeDocument;
var paths = aDoc.pathItems;
var testColor = new GrayColor();
testColor.gray = 5;
for (var i=0; i < paths.length; i++) {
pathColor = paths[i];
if(pathColor.fillColor == testColor) {
pathColor.selected = true;
}
}
}
test();
Copy link to clipboard
Copied
as Carlos said, you need to compare the properties of the objects. you're comparing equality of two unequal objects.
Carlos' example will definitely work. but since i typed it up already, i'll share my solution as well.
#target illustrator
function test()
{
var aDoc = app.activeDocument;
var paths = aDoc.pathItems;
var testColor = new GrayColor();
testColor.gray = 5;
var currentPath;
for (var i = 0; i < paths.length; i++)
{
currentPath = paths[i];
if(!currentPath.filled)
{
continue;
}
//first check that the pathItem has a gray value
//to avoid runtime errors from reading missing values
//check the gray value of the path
//and see if it's the same as the gray value
//of the testColor object.
if (currentPath.fillColor.gray)
{
if(Math.round(currentPath.fillColor.gray) === Math.round(testColor.gray))
{
currentPath.selected = true;
}
}
}
}
test();
Copy link to clipboard
Copied
you need to break it down to the actual components. In the case of GrayScale colors the only component is gray, if you had cmyk colors, you would have to test each color separately
for your example, edit your comparison check to this
if(Math.round(pathColor.fillColor.gray) == Math.round(testColor.gray)) {
Copy link to clipboard
Copied
This is what I have now and it still doesn't do anything. ????????
var aDoc = app.activeDocument;
var paths = aDoc.pathItems;
var testColor = new GrayColor();
testColor.gray = 5;
for (var i=0; i < paths.length; i++) {
pathColor = paths[i];
if(Math.round(pathColor.fillColor.gray) == Math.round(testColor.gray)) {
pathColor.selected = true;
}
}
Copy link to clipboard
Copied
Can you share the AI file you're using? Are you positive there are some items in the document that are filled with a GrayColor that has a gray value of 5?
Please note that this will only work for path items with a simple fill color. It won't pick up gradients or pattern fills even if they use the same GrayColor.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Yea. there are no objects in that file that that have a fill color that matches the criteria...
If you manually create a rectangle, fill it with a 5% GrayColor value, the script i posted above works as expected.
Copy link to clipboard
Copied
So I converted my colors to Gray Scale and ran the script. I get this error
I also tried this script for CMYK colors
var aDoc = app.activeDocument;
var paths = aDoc.pathItems;
var newCMYKColor = new cmykColor();
newCMYKColor.black = 20;
newCMYKColor.cyan = 0;
newCMYKColor.magenta = 0;
newCMYKColor.yellow = 0;
for (var i=0; i < paths.length; i++) {
pathColor = paths[i];
if(Math.round(pathColor.fillColor.black) == Math.round(newCMYKColor.black) &&
Math.round(pathColor.fillColor.cyan) == Math.round(newCMYKColor.cyan) &&
Math.round(pathColor.fillColor.magenta) == Math.round(newCMYKColor.magenta) &&
Math.round(pathColor.fillColor.yellow) == Math.round(newCMYKColor.yellow)) {
pathColor.selected = true;
}
}
And get this error
Copy link to clipboard
Copied
javascript is case sensitive, CMYK needs to be upper case
var newCMYKColor = new CMYKColor();
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more