Illustrator Script - Change Stroke color in a specific layer

New Here ,
Jul 03, 2019 Jul 03, 2019

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

1.1K

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
community guidelines

correct answers 1 Correct Answer

Enthusiast , Apr 03, 2021 Apr 03, 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; }

Likes

Translate

Translate
Contributor ,
Jul 03, 2019 Jul 03, 2019

Copy link to clipboard

Copied

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.

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
community guidelines
Explorer ,
Apr 03, 2021 Apr 03, 2021

Copy link to clipboard

Copied

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?

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
community guidelines
Enthusiast ,
Apr 03, 2021 Apr 03, 2021

Copy link to clipboard

Copied

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

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
community guidelines
Explorer ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

LATEST

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

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
community guidelines