Copy link to clipboard
Copied
I'm not much into scripting, so I apologise for any stupid questions. I'm trying to create a script to set up layers that I usually create manually every time. I copied this script a while ago and it worked in previous versions of Illustrator, but i get errors in Illustrator 2023. Any help would be much appreciated.
//Apply to myDoc the active document
var layerName = LayerOrderType;
var myDoc = app.activeDocument;
//define first character and how many layers do you need
var layerName
var numberOfLayers=0;
//Create the layers
for(var i=0; i<=numberOfLayers; i++)
{ var layerName = "BACKGROUND"; var myLayer = myDoc.layers.add(); myLayer.name = layerName; }
{ var layerName = "ARTWORK"; var myLayer = myDoc.layers.add(); myLayer.name = layerName; }
{ var layerName = "FOLD"; var myLayer = myDoc.layers.add(); myLayer.name = layerName; }
{ var layerName = "KNIFE"; var myLayer = myDoc.layers.add(); myLayer.name = layerName; }
// Moves the bottom layer to become the topmost layer
if (documents.length > 0) {
countOfLayers = activeDocument.layers.length;
if (countOfLayers > 1) {
bottomLayer = activeDocument.layers[countOfLayers-1];
bottomLayer.zOrder(ZOrderMethod.BRINGTOFRONT);
}
else {
alert("The active document only has only 1 layer")
}
}
This is the error I get
Hi @pnsyd, no need to apologize; scripting is challenging and it's good that you are trying to make things easier for yourself!
Unfortunately your script has lots of errors—but here is a fixed version:
(function () {
var layerNames = [
"BACKGROUND",
"ARTWORK",
"FOLD",
"KNIFE",
];
var doc = app.activeDocument;
for (var i = 0; i < layerNames.length; i++) {
var myLayer = doc.layers.add();
myLayer.name = layerNames[i];
}
...
Copy link to clipboard
Copied
Hi @pnsyd, no need to apologize; scripting is challenging and it's good that you are trying to make things easier for yourself!
Unfortunately your script has lots of errors—but here is a fixed version:
(function () {
var layerNames = [
"BACKGROUND",
"ARTWORK",
"FOLD",
"KNIFE",
];
var doc = app.activeDocument;
for (var i = 0; i < layerNames.length; i++) {
var myLayer = doc.layers.add();
myLayer.name = layerNames[i];
}
// Moves the bottom layer to become the topmost layer
doc.layers[doc.layers.length - 1].zOrder(ZOrderMethod.BRINGTOFRONT);
})();
- Mark
Copy link to clipboard
Copied
Thank you so much 🙂
Copy link to clipboard
Copied
Is it possible to assign a color to the layer?
Copy link to clipboard
Copied
Yes, here's an example. You can adjust the RGB values to match the color you want.
(function () {
var layerData = [
{ name: "BACKGROUND", color: [255, 128, 0] },
{ name: "ARTWORK", color: [128, 255, 128] },
{ name: "FOLD", color: [0, 0, 255] },
{ name: "KNIFE", color: [0, 255, 255] },
];
var doc = app.activeDocument;
for (var i = 0; i < layerData.length; i++) {
var myLayer = doc.layers.add();
myLayer.name = layerData[i].name;
if (layerData[i].color === undefined)
continue;
var c = new RGBColor();
c.red = layerData[i].color[0];
c.blue = layerData[i].color[1];
c.green = layerData[i].color[2];
myLayer.color = c;
}
if (doc.layers[doc.layers.length - 1].locked == false)
// Moves the bottom layer to become the topmost layer
doc.layers[doc.layers.length - 1].zOrder(ZOrderMethod.BRINGTOFRONT);
})();
Copy link to clipboard
Copied
Amazing. Thank you