Highlighted

Illustrator script to get compoundPathItem/PathItem filled colors on few layers

Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

I want get each Items filled colors code on each layers. I tried these codes with no success

Please help me.

var doc = app.activeDocument;
for (i=0; i<doc.layers.length; i++) {
    var pathcolor = doc.layers[i].pathItems[0].color;
    //this line returned Error 1302: No such element
    
    var pathcolor = doc.layers[i].pathItems[0].fillcolor;
    //this line returned Error 1302: No such element
    
    var compoundpathcolor = doc.layers[i].compoundPathItems[0].color;
    //this line returned Error 21: undefined
        
    var compoundpathcolor = doc.layers[i].compoundPathItems[0].fillcolor;
    //this line returned Error 21: undefined  
}

 

You can try this script:

var doc = app.activeDocument;
for (i=0; i<doc.layers.length; i++) {
for(j=0;j<doc.layers[i].pageItems.length;j++)
{
if(doc.layers[i].pageItems[j].typename=="PathItem" ||
doc.layers[i].pageItems[j].typename=="TextFrame")
{
var fillcolor = doc.layers[i].pageItems[j].fillColor;
}
else if(doc.layers[i].pageItems[j].typename=="CompoundPathItem")
{
var cp=doc.layers[i].pageItems[j];
for(c=0;c<cp.pathItems.length;c++)
{
var pathColor = cp.pathItems[c].fillColor;
}
}
}
}

TOPICS
Scripting

Views

430

Likes

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

Illustrator script to get compoundPathItem/PathItem filled colors on few layers

Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

I want get each Items filled colors code on each layers. I tried these codes with no success

Please help me.

var doc = app.activeDocument;
for (i=0; i<doc.layers.length; i++) {
    var pathcolor = doc.layers[i].pathItems[0].color;
    //this line returned Error 1302: No such element
    
    var pathcolor = doc.layers[i].pathItems[0].fillcolor;
    //this line returned Error 1302: No such element
    
    var compoundpathcolor = doc.layers[i].compoundPathItems[0].color;
    //this line returned Error 21: undefined
        
    var compoundpathcolor = doc.layers[i].compoundPathItems[0].fillcolor;
    //this line returned Error 21: undefined  
}

 

You can try this script:

var doc = app.activeDocument;
for (i=0; i<doc.layers.length; i++) {
for(j=0;j<doc.layers[i].pageItems.length;j++)
{
if(doc.layers[i].pageItems[j].typename=="PathItem" ||
doc.layers[i].pageItems[j].typename=="TextFrame")
{
var fillcolor = doc.layers[i].pageItems[j].fillColor;
}
else if(doc.layers[i].pageItems[j].typename=="CompoundPathItem")
{
var cp=doc.layers[i].pageItems[j];
for(c=0;c<cp.pathItems.length;c++)
{
var pathColor = cp.pathItems[c].fillColor;
}
}
}
}

TOPICS
Scripting

Views

431

Likes

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
Jun 14, 2020 1
Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

I would also like to know how this might be achieved.

 

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

Hi,

What I understand that, you want to use layer color as a fillColorfor each item inside the layer.

You can try following snippet. But this will fill only pathItems, not groupItems and compoundPaths.

 

var doc = app.activeDocument;
for (i = 0; i < doc.layers.length; i++) {
  var _layer = doc.layers[i];
  var layerColor = doc.layers[i].color;
  for (var j = 0; j < _layer.pageItems.length; j++) {
    var _pageItem = _layer.pageItems[j];
    _pageItem.fillColor = layerColor;
  }
}

 

Let us know if this works for you.

Best regards

Likes

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
Reply
Loading...
Jun 14, 2020 0
Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

Thank Charu,

 

I mean I've just only want to get the item color property inside every layers, not applying layer color to its children.

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

OK, so you can get color property of each layer using color property. The following will give color of first layer. If you want color of each layer you can loop over all layers and get the color of each layer.

 

var doc = app.activeDocument;
var layerColor = doc.layers[0].color;

 

I hope this helps you.

 

Best regards

Likes

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
Reply
Loading...
Jun 14, 2020 0
Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

Sorry Charu again for my explaining.

I want to get items color are staying inside layers. Your guide is about layer color, not item color

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

You can get color of an item inside the layer using "fillColor",

var itemColor = doc.layers[0].pathItems[0].fillColor

but for compoundPathItems and GroupItems, fillColor does not exists. So, for coumpoundpathItems, you need to first release the compund path, and then loop to each item present in the compundPath to get colors of the items and similary for the groupItems, you need to traverse each item inside the group to get color of the item.

 

Let me know if you want more clarification.

 

Best regards

Likes

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
Reply
Loading...
Jun 14, 2020 0
Community Beginner ,
Jun 14, 2020

Copy link to clipboard

Copied

I already tried your code with no success:

var doc = app.activeDocument;
var itemColor = doc.layers[0].pathItems[0].fillColor;

I'm using illustrator CC 22.0

Error 1302: No such element Line: 2 -> var itemColor = doc.layers[0].pathItems[0].fillColor; 

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

May be a better explanation

 

1. First check if item is Path item, -> Yes, then check if it is filled -> Yes, then you get get color of the path item using following code. To run below code, select the path item, I mean rectangle, ellipse, etc.

 

if(app.selection[0].typename == "PathItem" && app.selection[0].filled)
var pathColor = doc.layers[0].pathItems[0].fillColor;   // this will give color of the first pathItem in first layer

 

2. If item is compundPathItem -> Release -> Traverse -> GetColor -> Make.

To run below code, select the compoundPathItem in your document.

 

if(app.selection[0].typename == "CompoundPathItem"){
 app.executeMenuCommand('noCompoundPath');
  for (c = 0; c < app.selection.length; c++) {
    if (app.selection[c].filled)
      var compoundPathItemColor = app.selection[c].fillColor; // this will give color of the items inside the compoundPath
      //DO whatever you want to do with compoundPathItemColor
  }
  app.executeMenuCommand('compoundPath');
  }

 

I hope this explains you how to get color of the item for different objects.

 

 

Best regards

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 14, 2020

Copy link to clipboard

Copied

This error will come when there is no pathItems in first layer of your document.

Best regards

Likes

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
Reply
Loading...
Jun 14, 2020 0
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

Charu,

 

For compound paths, you needn't (and indeed shouldn't) start releasing compound paths to get the color properties. You could very well end up changing the appearance of the object if you start releasing the compound paths and then re-compounding them.

 

First point is that you don't need to check the color of all elements of a compoundPath. Every pathItem inside a compoundPathItem will have the same color information. You can't have one path inside a compoundPath with a fill color of red, and another path inside the same compoundPath with a fill color of green. So you simply need to isolate a single pathItem and then you can be confident that the rest will match up. However, this is NOT the case if you release the compoundPath to take your measurements. If you take a red shape and a green shape and create a compound path out of them, they will both inherit the color of the topmost object. But when they are released, they will retain their original color information. So if you release your compound path to get color information, you might see unexpected results.

 

Second point is that a compoundPathItem is a container object just like groupItem so you can access the child elements in the same way. You can get the fill/stroke color of a compoundPathItem just like this:

 

var myCompoundPathItem = app.activeDocument.pageItems[0];
var fillColor = myCompoundPathItem.pathItems[0].fillColor;
var strokeColor = myCompoundPathItem.pathItems[0].strokeColor;

 

 It's important to note that it is possible (though somewhat unlikely) that you can have a compoundPathItem that is created out of groupItems instead of pathItems. That's not inherently a problem, though.. You just do the same thing as i showed above to access the child elements of the groupItem and then get the fillColor property from that child item. Unfortunately because you can nest groups as deep as you like, this means that you either have to know the exact structure of the compoundPath you're analyzing at the time of writing the script, or you need a recursive function to dig into the compound path until a pathItem is found, then just return the color of that item. Here's one that I use for this purpose:

 

function getColorOfObject(item)
{
	var result = {"fill":undefined,"stroke":undefined,"valid":false};

	//anything matching this regex won't have standard fill/stroke color properties
	//you'll need to use another method to find color information
	var noColorItemRegex = /mesh|placed|plugin|raster|group/ig;

	if(item.typename.match(noColorItemRegex))
	{
		alert("Can't get the color of a " + item.typename);
	}
	else if(item.typename === "PathItem" || item.typename === "TextFrameItem")
	{
		result.fill = item.fillColor;
		result.stroke = item.strokeColor;
		result.valid = true;
	}
	else if(item.typename === "CompoundPathItem")
	{
		dig(item);
	}

	return result;



	function dig(item)
	{
		var curSubItem;
		if(item.typename === "CompoundPathItem" && item.pathItems.length && !result.valid)
		{
			curSubItem = item.pathItems[0];
			result.fill = curSubItem.fillColor;
			result.stroke = curSubItem.strokeColor;
			result.valid = true;
			return;
		}

		
		for(var i=0,len=item.pageItems.length;i<len && !result.valid;i++)
		{
			curSubItem = item.pageItems[i];
			if(curSubItem.typename === "PathItem")
			{
				result.fill = curSubItem.fillColor;
				result.stroke = curSubItem.strokeColor;
				result.valid = true;
			}
			else if(curSubItem.typename === "GroupItem")
			{
				dig(curSubItem);
			}
		}
	}

}

Likes

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
Reply
Loading...
Jun 15, 2020 1
Adobe Community Professional ,
Jun 15, 2020

Copy link to clipboard

Copied

Thankyou for this wonderful explanation. 🙂

I know it is not neceessary to release compundpath, But I don't know why this comes first in my mind.

 

But there is something new information here, I mean I have never noticed that before, compundpathitem cannot have a differnt color. It is so basic but did not ever realise that.

 

Highly appreciated for this post.

Best regards

Likes

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
Reply
Loading...
Jun 15, 2020 1
Enthusiast ,
Jun 15, 2020

Copy link to clipboard

Copied

You can try this script:

var doc = app.activeDocument;
for (i=0; i<doc.layers.length; i++) {
for(j=0;j<doc.layers[i].pageItems.length;j++)
{
if(doc.layers[i].pageItems[j].typename=="PathItem" ||
doc.layers[i].pageItems[j].typename=="TextFrame")
{
var fillcolor = doc.layers[i].pageItems[j].fillColor;
}
else if(doc.layers[i].pageItems[j].typename=="CompoundPathItem")
{
var cp=doc.layers[i].pageItems[j];
for(c=0;c<cp.pathItems.length;c++)
{
var pathColor = cp.pathItems[c].fillColor;
}
}
}
}

Likes

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
Reply
Loading...
Jun 15, 2020 0
Community Beginner ,
Jun 15, 2020

Copy link to clipboard

Copied

Thank you daitranthanhoa,

It's the thing I'm looking for.

Cám ơn bạn nhiều.

Likes

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
Reply
Loading...
Jun 15, 2020 1