Skip to main content
Participant
April 12, 2023
Question

Help with script to change fill and stroke colors

  • April 12, 2023
  • 2 replies
  • 1187 views

I am new to scripting and found some code I could get to work on all my layers:

var doc = app.activeDocument;
var swatch = app.activeDocument.swatches.getByName('Birch Product');
var stroke = app.activeDocument.swatches.getByName('New Pattern Swatch 9');


for ( i = 0; i <doc.pathItems.length; i++ ) {
pathArt = doc.pathItems[i];
pathArt.strokeWidth = 2;
pathArt.strokeColor = stroke.color;
pathArt.fillColor = swatch.color;
}

I would like to apply this to only a specific layer instead of all layers.  Tried the code below but it did not work; nothing happened.  What do I need to do to get it to change just that layer.  I also tried "PageItems", but that didn't work either.

var greenLayer = app.activeDocument.layers.getByName("GREEN");
var swatch = app.activeDocument.swatches.getByName('Birch Product');
var stroke = app.activeDocument.swatches.getByName('New Pattern Swatch 9');

for ( i = 0; i <greenLayer.pathItems.length; i++ ) {
pathArt = greenLayer.pathItems[i];
pathArt.strokeWidth = 2;
pathArt.strokeColor = stroke.color;
pathArt.fillColor = swatch.color;
}

 

 

This topic has been closed for replies.

2 replies

pixxxelschubser
Community Expert
Community Expert
April 12, 2023

Very often compound paths or clipping paths or groups causing such issues.

Participant
April 13, 2023

There shouldn't be any compound paths, clipping paths or grouping.

m1b
Community Expert
Community Expert
April 13, 2023

It was confusing, because the groups are called "Polyline".

m1b
Community Expert
Community Expert
April 12, 2023

Hi @robertp99924539, I tested your second script and it worked as expected. Can you post a small example file so we can see why it isn't working? (Save it as .pdf because the forum software doesn't accept .ai files.)

- Mark

Participant
April 13, 2023

I attached the file.  Some background.  This is a dxf file that is created in Cut2D and exported as DXF.  I also tried to export as AI, EPS, SVG but the program only maintains the true layer names with DXF.  We use the DXF files to create pictures for our website by coloring in the different layers.  It is the picture creation I am trying to automate.

m1b
Community Expert
Community Expert
April 13, 2023

Thanks @robertp99924539, it always helps to have a sample file. The problem was that every pathItem in the DXF artwork was grouped (each group was called POLYLINE). So here's a slight adjustment to the script:

var greenLayer = app.activeDocument.layers.getByName("GREEN");
var swatch = app.activeDocument.swatches.getByName('Birch Product');
var stroke = app.activeDocument.swatches.getByName('New Pattern Swatch 9');

for (i = 0; i < greenLayer.groupItems.length; i++) {
    pathArt = greenLayer.groupItems[i].pathItems[0];
    pathArt.strokeWidth = 2;
    pathArt.strokeColor = stroke.color;
    pathArt.fillColor = swatch.color;
}

 

... or you could just Select All and then Ungroup, and then run your original script. (Assuming that you've put the Birch Product and New Pattern Swatch 9 in the document.)

- Mark