Highlighted

Sorting the layers and using a loop

Explorer ,
Apr 18, 2020

Copy link to clipboard

Copied

Hello,

I have such part of code. It is sorting me the layers in proper order.

My question is how to use a loop of existence of every layer for that ? "Check if such given layer exists, use the code. When it doesn't exist, go to the next layer" ?

 

"

var aLay = app.activeDocument.layers.getByName("tertiary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

var aLay = app.activeDocument.layers.getByName("secondary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

var aLay = app.activeDocument.layers.getByName("primary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

 

"

Topics

How to, Scripting

Views

609

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

Sorting the layers and using a loop

Explorer ,
Apr 18, 2020

Copy link to clipboard

Copied

Hello,

I have such part of code. It is sorting me the layers in proper order.

My question is how to use a loop of existence of every layer for that ? "Check if such given layer exists, use the code. When it doesn't exist, go to the next layer" ?

 

"

var aLay = app.activeDocument.layers.getByName("tertiary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

var aLay = app.activeDocument.layers.getByName("secondary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

var aLay = app.activeDocument.layers.getByName("primary");
aLay.zOrder( ZOrderMethod.BRINGTOFRONT);

 

"

Topics

How to, Scripting

Views

610

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
Contributor ,
Apr 18, 2020

Copy link to clipboard

Copied

What do you mean by "proper order"?  Do you want the first-created to be topmost?  Or do you want them ordered by name ("primary" to be topmost)? 

 

 

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
Reply
Loading...
Explorer ,
Apr 19, 2020

Copy link to clipboard

Copied

No, no, it is working nice now. It is sorting me the layers from the top in this order: primary, secondary, tertiary.

But when for example there is no layer "secondary", then the script crashes in that moment.

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
Reply
Loading...
Adobe Community Professional ,
Apr 19, 2020

Copy link to clipboard

Copied

you can use a try/catch to trap errors

 

var idoc = app.activeDocument;
try {
var ilayer = idoc.layers.getByName('Layer 2');
// DO SOMETHING
alert(ilayer.name);
}
catch(e) {
// DO SOMETHING ELSE
alert(e);
}

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
Reply
Loading...
Explorer ,
Apr 19, 2020

Copy link to clipboard

Copied

No, no, I needn't catch errors.

 

For me it would be ideal if script checks if such layer exists, then bring it to front. When it doesn't exist, do nothing.

Then next layer the same, and next and next...

 

These are only three example layers.

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
Reply
Loading...
Contributor ,
Apr 19, 2020

Copy link to clipboard

Copied

Loop thru layers and check if a named-layer exists; if so, bring to front; if not, check if a next named-layers exists:

 

 

for (var i = 0; i < activeDocument.layers.length; i++) {
 if (activeDocument.layers[i].name == "tertiary") {
   activeDocument.layers["tertiary"].zOrder(ZOrderMethod.BRINGTOFRONT);
 }
 if (activeDocument.layers[i].name == "secondary") {
   activeDocument.layers["secondary"].zOrder(ZOrderMethod.BRINGTOFRONT);
 }
 if (activeDocument.layers[i].name == "primary") {
   activeDocument.layers["primary"].zOrder(ZOrderMethod.BRINGTOFRONT);
 }
}

 

 

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
Reply
Loading...
Enthusiast ,
Apr 20, 2020

Copy link to clipboard

Copied

Bonjour,

C'est bien femkeblanco, je te propose une solution plus séduisante:

// JavaScript Document pour Illustrator
var listlayerName = ["primary","secondary","tertiary"];
//-----------------------------------
var aDoc = app.activeDocument;
//-----------------------------------
 //listlayerName.reverse();  // suivant l'ordre croissant ou décroissant

    for (var i = 0; i < listlayerName.length; i++) {
      var layer = getLayers(aDoc,listlayerName[i])
          if (layer) {layer.zOrder(ZOrderMethod.BRINGTOFRONT);}
    }
//----
function getLayers(relativeObjet,layerName) {
     try {
       var klayer = relativeObjet.layers.getByName(layerName);
     } catch (e) {
        //alert( "The specified layer doesn’t exist" );
        return undefined; // undefined si non trouve
    }
     return klayer;
}
//----

de elleere

 

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
Reply
Loading...
Adobe Community Professional ,
Apr 20, 2020

Copy link to clipboard

Copied

For me it would be ideal if script checks if such layer exists, then bring it to front. When it doesn't exist, do nothing.

it would be ideal for me as well, but there isn't a command to check for layer existance

 

No, no, I needn't catch errors.

checking for errors is a way of telling if a layer exists

 

 

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
Reply
Loading...
Beff LATEST
Explorer ,
Apr 20, 2020

Copy link to clipboard

Copied

Thank you. They are working great.

 

I still have problems with these kind of loop.

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
Reply
Loading...