• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with Illustrator Script for Creating Layers

Explorer ,
Jul 30, 2023 Jul 30, 2023

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

Screenshot 2023-07-31 at 11.01.46 am.png

TOPICS
Scripting

Views

339

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

Community Expert , Jul 30, 2023 Jul 30, 2023

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

    
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 30, 2023 Jul 30, 2023

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

Votes

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 ,
Jul 30, 2023 Jul 30, 2023

Copy link to clipboard

Copied

Thank you so much 🙂

Votes

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 ,
Jul 30, 2023 Jul 30, 2023

Copy link to clipboard

Copied

Is it possible to assign a color to the layer?

Votes

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
Community Expert ,
Jul 30, 2023 Jul 30, 2023

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

})();

Votes

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 ,
Jul 30, 2023 Jul 30, 2023

Copy link to clipboard

Copied

LATEST

Amazing. Thank you

Votes

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