4 Replies Latest reply on May 24, 2016 4:08 AM by Johnwhite

    How to make the layer visible on and off

    Johnwhite Level 1

      Hi, experts

       

      this is my script:

       

              var docs = app.documents;

                   for (var i = docs.length-1; i >= 0; i--) {

                          docs[i].layers.item("Chi").visible = true;

                          docs[i].layers.item("Chi").locked = false;

                          docs[i].layers.item("Eng").visible = false;

                          docs[i].layers.item("Eng").locked = true;

                           }

            alert("Done.")

       

      but how can make the layer

      1. if the layer "chi" visible = true; while "eng" visible = false

      "chi" locked = false; while "eng" locked = true

      just to turn the

      layer "chi" visible = false; while "eng" visible = true 

      "chi" locked = true; while "eng" locked = false

       

      just like two layer switch;

       

      regard

      John

        • 1. Re: How to make the layer visible on and off
          Sajeev Sridharan Level 4

          Try this,

           

          var docs = app.documents;
          for (var i = docs.length-1; i >= 0; i--) {
              if(docs[i].layers.item("Chi").visible == true && docs[i].layers.item("Chi").locked == false)
              {
                  docs[i].layers.item("Chi").visible = false;
                  docs[i].layers.item("Chi").locked = true;
                  docs[i].layers.item("Eng").visible = true;
                  docs[i].layers.item("Eng").locked = false;
              }
              else if(docs[i].layers.item("Eng").visible == true && docs[i].layers.item("Eng").locked == false)
              {
                  docs[i].layers.item("Chi").visible = true;
                  docs[i].layers.item("Chi").locked = false;
                  docs[i].layers.item("Eng").visible = false;
                  docs[i].layers.item("Eng").locked = true;
              }
          }
          alert("Done.")
          
          • 2. Re: How to make the layer visible on and off
            vinothr Level 3

            Couple of things to note:

            1. You should get layer by name layer.itemByName

            2. Consider reuse and error handling

             

            You can modify below code if you think your requirement is not translated.

             

            // Toggle between Chi and Eng layers

            function toggleLayers() {

                 var total = app.documents.length;

                 for(var i = 0; i < total; i++) {

                      var doc = app.documents.item(i);

                      var chiLayer = doc.layers.itemByName("Chi");

                      if(chiLayer && chiLayer.isValid && chiLayer.visible) {

                           lockLayer(doc, "Eng");

                           unlockLayer(doc, "Chi");

                      } else {

                           lockLayer(doc, "Chi");

                           unlockLayer(doc, "Eng");

                      }

                 }

            }

             

            // Lock layer and make it invisible

            function lockLayer(doc, layerName) {

                 try{

                      var layer = doc.layers.itemByName(layerName);

                      if(layer && layer.isValid) {

                           layer.visible = false;

                           layer.locked = true;

                      }

                 }catch(e) { }

            }

             

            // Unlock layer and make it visible

            function unlockLayer(doc, layerName) {

                 try{

                      var layer = doc.layers.itemByName(layerName);

                      if(layer && layer.isValid) {

                           layer.visible = true;

                           layer.locked = false;

                      }

                 }catch(e) {}

            }

            • 3. Re: How to make the layer visible on and off
              Loic.Aigon Adobe Community Professional

              Like this maybe ?

               

              var main = function() {
                var docs = app.documents,
                layers,
                n = docs.length,
                chi, eng;
              
                while ( n-- ) {
                layers = docs[n].layers;
                chi = layers.item("Chi");
                eng = layers.item("Eng");
                if ( chi.isValid && eng.isValid ) {
                eng.visible = chi.locked = !chi.visible;
                eng.locked = chi.visible;
                }
                }
              }
              
              
              main();
              

               

               

              HTH

               

              Loic

              http://www.ozalto.com/

              1 person found this helpful
              • 4. Re: How to make the layer visible on and off
                Johnwhite Level 1

                Thank you all guys

                 

                both are works, amazing.

                 

                thanks appreciate.

                regard

                John