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

Automatic layer hierarchy – script

Explorer ,
Sep 03, 2021 Sep 03, 2021

Copy link to clipboard

Copied

Hey, I managed to create a simple script that:
1. Creates a layer hierarchy for packaging designs.
2. Gives specific colors to the layer groups.
3. Deletes Base Layer "Layer 1".

obrazek1a.png

Unfortunately, I am not a developer and I have trouble creating a few exceptions to this script:
1. Currently, if there is already a layer named from the script, the script stops and an error crashes.
2. I would like the script, instead of searching by the name “Layer 1”, to find all empty layers (visible and invisible) and remove them. Of course, excluding those newly created by the script 🙂

 

Is there any of you who could help me with this? 🙂

 

d = app.activeDocument;
d.layers.add ({name: 'BACKGROUND', layerColor: UIColors.YELLOW});
d.layers.add ({name: 'FRONT', layerColor: UIColors.GREEN});
d.layers.add ({name: 'BACK', layerColor: UIColors.GREEN});
d.layers.add ({name: 'LEFT', layerColor: UIColors.TEAL});
d.layers.add ({name: 'RIGHT', layerColor: UIColors.TEAL});
d.layers.add ({name: 'BOTTOM', layerColor: UIColors.GRID_BLUE});
d.layers.add ({name: 'TOP', layerColor: UIColors.GRID_BLUE});
d.layers.add ({name: 'DIE-CUT', layerColor: UIColors.ORANGE});

d.layers.itemByName("Layer 1").remove();

 

TOPICS
How to , Scripting

Views

456

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 , Sep 05, 2021 Sep 05, 2021

You could store the created layers in an array, loop thru all layers, and only delete empty layers that are not in the array. So maybe this:

 

var d = app.documents[0];
var l = d.layers;
var a = [makeLayer(d, "BACKGROUND", UIColors.YELLOW), 
        makeLayer(d, "FRONT", UIColors.GREEN),
        makeLayer(d, "BACK", UIColors.GREEN),
        makeLayer(d, "LEFT", UIColors.TEAL),
        makeLayer(d, "RIGHT", UIColors.TEAL),
        makeLayer(d, "BOTTOM", UIColors.GRID_BLUE),
        makeLayer(d, "
...

Votes

Translate

Translate
Community Expert ,
Sep 03, 2021 Sep 03, 2021

Copy link to clipboard

Copied

You can remove layers with no page items with a reverse loop, and for the layer construction call a function that checks if the layer already exists. Something like this:

 

 

 

 

 

var d = app.documents[0];
var l = d.layers;

//removes layers with no page items in reverse order
for (var i = l.length-1; i > 0; i--){
    if (l[i].pageItems.length == 0) {
        l[i].remove()
    }
}; 

makeLayer(d, "BACKGROUND", UIColors.YELLOW);
makeLayer(d, "FRONT", UIColors.GREEN);
makeLayer(d, "BACK", UIColors.GREEN);
makeLayer(d, "LEFT", UIColors.TEAL);
makeLayer(d, "RIGHT", UIColors.TEAL);
makeLayer(d, "BOTTOM", UIColors.GRID_BLUE);
makeLayer(d, "TOP", UIColors.GRID_BLUE);
makeLayer(d, "DIE-CUT", UIColors.ORANGE);



/**
* Makes a new named Layer 
* @ param the document to add the layer 
* @ param layer name 
* @ param layer color
* @ return the new layer 
* 
*/
function makeLayer(d, n, c){
    try {
        d.layers.add({name:n, index:0, layerColor: c});
    }catch(e) {} 
    return d.layers.itemByName(n);
};

 

 

 

 

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 ,
Sep 03, 2021 Sep 03, 2021

Copy link to clipboard

Copied

@rob day thx for your reply and help 🙂

 

The first time I use your version of the script it doesn't remove the empty "Layer 1". Probably because it's at the beginning of the script and it can't remove the only one layer in the project.

 

Do you know how to make the same order of layers?

Because now if I have for example one layer called "FRONT" and I will use the script this layer is at the very bottom. And above it are added the rest of the layers.

 

obrazek2.png

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 ,
Sep 03, 2021 Sep 03, 2021

Copy link to clipboard

Copied

Try this:

 

var d = app.documents[0];
makeLayer(d, "MyLayer", UIColors.YELLOW);
var l = d.layers;

//removes layers with no page items in reverse order
for (var i = l.length-1; i > -1; i--){
    if (l[i].pageItems.length == 0) {
        try {
            l[i].remove()
        }catch(e) {}   
    }
}; 

makeLayer(d, "BACKGROUND", UIColors.YELLOW);
makeLayer(d, "FRONT", UIColors.GREEN);
makeLayer(d, "BACK", UIColors.GREEN);
makeLayer(d, "LEFT", UIColors.TEAL);
makeLayer(d, "RIGHT", UIColors.TEAL);
makeLayer(d, "BOTTOM", UIColors.GRID_BLUE);
makeLayer(d, "TOP", UIColors.GRID_BLUE);
makeLayer(d, "DIE-CUT", UIColors.ORANGE);

//check the bottom layer
var bl = d.layers[d.layers.length-1]
if (bl.pageItems.length == 0) {
    bl.remove();
}


/**
* Makes a new named Layer 
* @ param the document to add the layer 
* @ param layer name 
* @ param layer color
* @ return the new layer 
* 
*/
function makeLayer(d, n, c){
    try {
        d.layers.add({name:n, index:0, layerColor: c});
    }catch(e) {} 
    return d.layers.itemByName(n);
};

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 ,
Sep 03, 2021 Sep 03, 2021

Copy link to clipboard

Copied

Just realized if the document only has 1 empty layer it would need to be removed after the new layers are constructed, so this might be better:

 

 

var d = app.documents[0];
var l = d.layers;

//removes layers with no page items in reverse order
for (var i = l.length-1; i > -1; i--){
    if (l[i].pageItems.length == 0) {
        try {
            l[i].remove()
        }catch(e) {}   
    }
}; 

makeLayer(d, "BACKGROUND", UIColors.YELLOW);
makeLayer(d, "FRONT", UIColors.GREEN);
makeLayer(d, "BACK", UIColors.GREEN);
makeLayer(d, "LEFT", UIColors.TEAL);
makeLayer(d, "RIGHT", UIColors.TEAL);
makeLayer(d, "BOTTOM", UIColors.GRID_BLUE);
makeLayer(d, "TOP", UIColors.GRID_BLUE);
makeLayer(d, "DIE-CUT", UIColors.ORANGE);

//check the bottom layer
var bl = d.layers[d.layers.length-1]
if (bl.pageItems.length == 0) {
    bl.remove();
}


/**
* Makes a new named Layer 
* @ param the document to add the layer 
* @ param layer name 
* @ param layer color
* @ return the new layer 
* 
*/
function makeLayer(d, n, c){
    try {
        d.layers.add({name:n, index:0, layerColor: c});
    }catch(e) {} 
    return d.layers.itemByName(n);
};

 

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 ,
Sep 05, 2021 Sep 05, 2021

Copy link to clipboard

Copied

Hey, it looks great 🙂 But are you able to do that when you use the script on the same file again and again, it doesn't remove the correct layer from the hierarchy? Because that's what's happening right now.

 

obrazek3.jpg

 

I really want the script to remove empty layers, but excluding those 8 layers from the hierarchy.

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 ,
Sep 05, 2021 Sep 05, 2021

Copy link to clipboard

Copied

You could store the created layers in an array, loop thru all layers, and only delete empty layers that are not in the array. So maybe this:

 

var d = app.documents[0];
var l = d.layers;
var a = [makeLayer(d, "BACKGROUND", UIColors.YELLOW), 
        makeLayer(d, "FRONT", UIColors.GREEN),
        makeLayer(d, "BACK", UIColors.GREEN),
        makeLayer(d, "LEFT", UIColors.TEAL),
        makeLayer(d, "RIGHT", UIColors.TEAL),
        makeLayer(d, "BOTTOM", UIColors.GRID_BLUE),
        makeLayer(d, "TOP", UIColors.GRID_BLUE),
        makeLayer(d, "DIE-CUT", UIColors.ORANGE)]


//removes layers not in the a array with no page items
for (var i = l.length-1; i > -1; i--){
    if (!checkItem(a, l[i]) && l[i].pageItems.length == 0) {
        try {
            l[i].remove()
        }catch(e) {}   
    }
}; 



/**
* Makes a new named Layer 
* @ param the document to add the layer 
* @ param layer name 
* @ param layer color
* @ return the new layer 
* 
*/
function makeLayer(d, n, c){
    try {
        d.layers.add({name:n, index:0, layerColor: c});
    }catch(e) {} 
    return d.layers.itemByName(n);
};


/**
* Checks if an item is in an array
* @ param the array to check 
* @ param the item to look for 
* @ return true if the item is in the array 
* 
*/
function checkItem(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
};

 

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 ,
Sep 06, 2021 Sep 06, 2021

Copy link to clipboard

Copied

@rob day now it's great because it no longer removes layers from the hierarchy. 🙂

 

I will try to do so that the order of the layers is always the same in the hierarchy. You helped me a lot, thank you very much. 🙂

obrazek5.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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
Guide ,
Sep 06, 2021 Sep 06, 2021

Copy link to clipboard

Copied

LATEST

Hi colleagues,

 

Just a quick note: there is no reason in principle to use a try…catch block when you just need to check the availability of some named object. Better is not throwing a runtime error at all. Something like that:

 

function makeLayer(/*Document*/d,/*str*/n,/*UIColorsEnum*/c,  t)
{
	(t=d.layers.itemByName(n)).isValid
	|| (t=d.layers.add({name:n, index:0, layerColor: c}));
	return t;
}

 

Best,

Marc

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