4 Replies Latest reply on Feb 10, 2016 9:38 AM by Laubender

    Create layer if not exists

    Elmnas

      Hello people,

       

      How do I create a layer with name "test"

      if not exists?

       

      I got this code so far:

       

       

        for(myCounter = 0; myCounter <app.activeDocument.layers.length; myCounter++)

                  {

                  myLayer = app.activeDocument.layers.item(myCounter);

              

                  if(app.documents.item(0).layers.itemByName("TT_IDML").exists){

                     

                     

                      alert("asd");

                      }

           

                  } 

       

       

      I am not sure what you use instead of .exists

       

       

       

      Thank you in advance

        • 1. Re: Create layer if not exists
          Elmnas Level 1

          I have also tried this code:

           

          but it still alerts the both messages:

           

          for (var i = 0; i < doc.layers.length; i++) 

          //Set up Variable to access layer name 

           

           

          if (doc.layers[i].name=="TT_IDML") 

          alert("the layer exists")

          else

          {

          alert("the layers does not exists");

              }

          • 2. Re: Create layer if not exists
            TᴀW Adobe Community Professional & MVP

            Use isValid:

             

            app.activeDocument.layers.itemByName("Layer 14").isValid

             

            will return true if there is such a layer, false otherwise.

             

            Ariel

            • 3. Re: Create layer if not exists
              Elmnas Level 1

              I made this instead

               

              var layer1 = MakeLayer("TT_IDML");

              function MakeLayer(name, layerColor) {

                var layer = docSource.layers.item(name);

                if (!layer.isValid) {

                layer = docSource.layers.add({name: name});

                if (layerColor != undefined) layer.layerColor = layerColor;

                }

                return layer;

              }

              • 4. Re: Create layer if not exists
                Laubender Adobe Community Professional & MVP

                Hi Elmnas,

                for questions about scripting InDesign we have a special forum here:

                InDesign Scripting

                 

                Using the "Use advanced editor" feature of the forum and formatting your posted code for JavaScript would always be a good idea.

                 

                Befor posting, select your code and go to:

                >> Syntax Highlighting > javascript

                 

                Here the formatting example with your posted code:

                 

                var layer1 = MakeLayer("TT_IDML");
                function MakeLayer(name, layerColor) {
                  var layer = docSource.layers.item(name);
                  if (!layer.isValid) {
                  layer = docSource.layers.add({name: name});
                  if (layerColor != undefined) layer.layerColor = layerColor;
                  }
                  return layer;
                }
                

                 

                Btw.: the Layer Object has a move() method, so you could move your new layer e.g. to the top of the stack.

                 

                Uwe