Skip to main content
New Participant
July 3, 2019
Answered

Illustrator Script - Change Stroke color in a specific layer

  • July 3, 2019
  • 2 replies
  • 4402 views

Hi there,

i try for just 4 hours now to get a function to change the stroke color of all objects in a layer "1".

with this script, every object is being recolored, this is working:

var docRef = app.activeDocument;

with (docRef) {

var newColor = makeColorCMYK(0,0,0,40);

for (var h = 0; h < pathItems.length; h++) {

        pathItems[h].strokeColor = newColor;

}

}

But if i only want to choose the pathItems in Layer "1", it doesn't work.

var newColor = makeColorCMYK(0,0,0,40);


var ln = '1';

var ol = docRef.layers.getByName(ln);

for (var i = 0; i < ol.pathItems.length; i++) {

    ol.pathItems[i].strokeColor = newColor;

}

I tried to lock the other layer and then to change color of all items, that are not locked. But didn't work either.

Where is my fault?

This topic has been closed for replies.
Correct answer femkeblanco
var string1 = prompt("Target Layer Name", "targetLayerName");
var string2 = prompt("CMYK Color", "100, 0, 0, 0");
var splitString2 = string2.split(", ");
var targetLayer = app.activeDocument.layers[string1];
var color1 = new CMYKColor();
color1.cyan = splitString2[0];
color1.magenta = splitString2[1];
color1.yellow = splitString2[2];
color1.black = splitString2[3];
for (var i = 0; i < targetLayer.pageItems.length; i++) {
    targetLayer.pageItems[i].strokeColor = color1;
}

2 replies

Inspiring
April 3, 2021

Hi ! Is the solution exists? the answer did't work for me!

 

Can somebody help with the script to change a stroke color in a specific layer?

femkeblanco
femkeblancoCorrect answer
Brainiac
April 3, 2021
var string1 = prompt("Target Layer Name", "targetLayerName");
var string2 = prompt("CMYK Color", "100, 0, 0, 0");
var splitString2 = string2.split(", ");
var targetLayer = app.activeDocument.layers[string1];
var color1 = new CMYKColor();
color1.cyan = splitString2[0];
color1.magenta = splitString2[1];
color1.yellow = splitString2[2];
color1.black = splitString2[3];
for (var i = 0; i < targetLayer.pageItems.length; i++) {
    targetLayer.pageItems[i].strokeColor = color1;
}
Inspiring
April 4, 2021

It works! Thank you very much!!

My code (more automated)

 

var docRef = app.activeDocument;
var targetLayer = app.activeDocument.layers.getByName('targetlayername');
var color1 = new RGBColor();
color1.red = 0;
color1.green = 0;
color1.blue = 255;
for (var i = 0; i < targetLayer.pageItems.length; i++) {
targetLayer.pageItems[i].strokeColor = color1;
}
Inspiring
July 3, 2019

Here you go, you can have a list of page items in a specific layer using following 2 lines of code

Syntax :

var layer = app.activeDocument.layers.getByName([Layername]);

Actual Code:

var layer = app.activeDocument.layers.getByName('1');       // '1' it is a name of the layer

var pageItems = layer.pageItems;

After this traverse on these pageItems. And I think your code does not work because it seems your pathItems are in group.