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

Loop through pathItems and select based on fillColor?

Explorer ,
Mar 17, 2020 Mar 17, 2020

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?

TOPICS
Scripting
1.8K
Translate
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
Mentor ,
Mar 17, 2020 Mar 17, 2020
#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();
Translate
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 ,
Mar 18, 2020 Mar 18, 2020

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();

Translate
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
Mentor ,
Mar 19, 2020 Mar 19, 2020

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();
Translate
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 ,
Mar 18, 2020 Mar 18, 2020

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)) {

 

Translate
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 ,
Mar 19, 2020 Mar 19, 2020

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;

}
}

Translate
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
Mentor ,
Mar 19, 2020 Mar 19, 2020

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. 

Translate
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 ,
Mar 19, 2020 Mar 19, 2020
Translate
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
Mentor ,
Mar 19, 2020 Mar 19, 2020

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. 

Translate
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 ,
Mar 20, 2020 Mar 20, 2020

So I converted my colors to Gray Scale and ran the script.  I get this error

grayacale.PNG

 

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 

cmykColor.PNG

Translate
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 ,
Mar 20, 2020 Mar 20, 2020
LATEST

javascript is case sensitive, CMYK needs to be upper case

 

var newCMYKColor = new CMYKColor();

Translate
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