Skip to main content
contraidaulong
Participant
June 15, 2020
Answered

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

  • June 15, 2020
  • 3 replies
  • 8386 views

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  
}

 

Correct answer daitranthanhoa

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

3 replies

daitranthanhoa
daitranthanhoaCorrect answer
Inspiring
June 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;
}
}
}
}

contraidaulong
Participant
June 15, 2020

Thank you daitranthanhoa,

It's the thing I'm looking for.

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

Charu Rajput
Community Expert
Community Expert
June 15, 2020

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
contraidaulong
Participant
June 15, 2020

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.

Charu Rajput
Community Expert
Community Expert
June 15, 2020

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
andrewh92075852
Known Participant
June 15, 2020

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