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

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

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

 

TOPICS
Scripting

Views

6.5K

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

Enthusiast , Jun 15, 2020 Jun 15, 2020

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;
}
}
}
}

Votes

Translate

Translate
Adobe
Explorer ,
Jun 14, 2020 Jun 14, 2020

Copy link to clipboard

Copied

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

 

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

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

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

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

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

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

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

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 ,
Jun 15, 2020 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);
			}
		}
	}

}

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 ,
Jun 15, 2020 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

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
Advisor ,
Jan 13, 2025 Jan 13, 2025

Copy link to clipboard

Copied

@Disposition_Dev 

 

Can i ask with item is in this case as input to the function. Im getting errors if i try to input an selection

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 13, 2025 Jan 13, 2025

Copy link to clipboard

Copied

My guess is that you're passing in document.selection?

 

If so, the issue is that the function expects an object (like a pathItem or group item, etc). Document.selection is a "collection". Try using document.selection[0] instead.

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
Advisor ,
Jan 14, 2025 Jan 14, 2025

Copy link to clipboard

Copied

I think i solved that indeed. But than i got something else. We get a warning it wont work on a group. Yet at the end i see code which works on groups. What is that for?

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 14, 2025 Jan 14, 2025

Copy link to clipboard

Copied

So the function itself can't accept a group item because a group could have many objects of different colors. The logic in there that handles group items is because of a quirk in compound paths that can prevent you from being able to read the fill/stroke color of the compound path. 

 

Essentially, the problem is that compoundPath does not have a "fillColor" property. To get the color of a compound path, you need to access the any pathItem in the compoundPath and get its fill/stroke. But it's possible (and quite easy) to build a compound path that contains no pathItems.... If you select some groupItems and then make a compoundPath out of them, compoundPath.pathItems.length will be 0. As an additional wrench in the works, compoundPath also doesn't have the properties "pageItems" or "groupItems".. so you can't traverse through the compound path until you find a pathItem.

 

So I had to build a workaround that breaks up the compound path, ungroups everything, then rebuilds the compound path out of pathItems so that the color can be extracted.

 

However, just yesterday I had an issue at work related to the same compoundPath buffoonery. And with the benefit of time and experience since I wrote this code originally, I realized none of that buffoonery is necessary.... The compound path doesn't need to be, and shouldn't be, altered. Instead of breaking apart and ungrouping everything, we can take advantage of one of the intrinsic properties of the compound path. Every item inside has the same fill/stroke properties. 😱🤯🤦

 

By creating a temporary pathItem inside the compoundPath in question, it's trivial to read the color info of that pathItem, which has inherited the color info from the rest of the compound path, then delete the temporary pathItem and boom... Color info extracted without disturbing the art at all.

 

I'll post an updated function later today.

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
Enthusiast ,
Jan 15, 2025 Jan 15, 2025

Copy link to clipboard

Copied

Oh, I've been using a temporary path inside CompoundPathItem for a long time when it's created from groups. But there are scripts where this solution is not ideal either. For example, we need to change the color of a compound path from groups.

1) We add a temporary path to it,

2) Assign a new color and see how the color of the whole CompoundPathItem has changed
3) We remove the temporary path.

 

Later, when we manually release the CompoundPathItem, the color of the groups in it will be the old one 😞 I always hope that users will have this as a rare case and not need to release the compound path.

 

However, to change the color of a compound path, we have to change the color of each PathItem within it. That's in an ideal world, but we live in Adobe's world.

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
Advisor ,
Jan 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

LATEST

@Disposition_Dev 

I came to thread again. Because indeed if a compound was made by sub groups. We can loop through it again some how. I tried using code to expand style and trim pathfinder. It does work on simple compound shape items but not ones with nested groep.

 

Things is compound shape can actually have a different method of color in terms of a group. When I applied a gradiënt to the compoundshap, when I ungrouo the gradiënt is set per sperate pathItem

 

I'll try your trick, see if that helps me. Thanks for the headsup

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

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
Enthusiast ,
Jun 15, 2020 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;
}
}
}
}

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 Beginner ,
Jun 15, 2020 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.

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