7 Replies Latest reply on Feb 16, 2012 6:54 AM by winterm

    Toggling layer lock

    winterm Level 4

      hi all,

      have one liner, simple as that:

       

      app.documents.item(0).layers.itemByName("Graphics").locked = true;

       

      I wonder, would it be possible to turn it to a toggler, without too much blood, sweat and tears?

      I mean, script should lock named layer, if it founds that layer unlocked, and vice versa - UNlock it, if founded locked.

      please don't kick me badly, I really tried, but... still nothing to write home about 

        • 1. Re: Toggling layer lock
          கற்பனை (Imagine) Level 3

          app.documents.item(0).layers.itemByName("Layer 1").locked ^= 1;

           

          I hope this helps to you.  and what you expected...

          • 2. Re: Toggling layer lock
            winterm Level 4

            Yes, that's exactly what I was looking for... thank You!

            Now I just need to find out, what the beast is that ^= 1. Never saw that expression before.

            Thank You again!

            • 3. Re: Toggling layer lock
              Laubender Adobe Community Professional & MVP

              @winterm – If I understand you right you want to un-lock every locked layer and lock every un-locked one?

               

              Then see the following script:

               

              var d= app.documents[0];
              var allLayers = d.layers;
              
              var lockedLayersIDsTrue = new Array();
              var lockedLayersIDsFalse = new Array();
              
              for(var n=0;n<allLayers.length;n++){
                  if(allLayers[n].locked){
                      lockedLayersIDsTrue.push(allLayers[n].id);
                      }
                  else{lockedLayersIDsFalse.push(allLayers[n].id)};
                  };
              
              for(var n=0;n<lockedLayersIDsTrue.length;n++){
                  allLayers.itemByID(lockedLayersIDsTrue[n]).locked = false;
                  };
              
              for(var n=0;n<lockedLayersIDsFalse.length;n++){
                  allLayers.itemByID(lockedLayersIDsFalse[n]).locked = true;
                  };
              

               

              First the locked state of every layer (in fact its ID) is recorded in two arrays, one for locked layers, one for unlocked ones.
              Then the script goes through the locked layers array and unlocks all recoded layers, after that we visit the locked layers array and lock all recorded layers.


              Uwe

              • 4. Re: Toggling layer lock
                கற்பனை (Imagine) Level 3

                ^= expression performs a bitwise XOR and assigns the result to the first operand.

                 

                Here is a link for full description about XOR.

                 

                http://en.wikipedia.org/wiki/XOR_gate

                • 5. Re: Toggling layer lock
                  Laubender Adobe Community Professional & MVP

                  @winterm – forget my version. Imagine's solution is much more smarter:

                   

                  var d= app.documents[0];
                  
                  //Toggle: for true set false and vice versa:
                  for(var n=0;n<d.layers.length;n++){
                      d.layers[n].locked ^= 1;
                      };
                  

                   

                  Uwe

                  1 person found this helpful
                  • 6. Re: Toggling layer lock
                    [Jongware] Most Valuable Participant

                    Technically it should be

                     

                    ..locked ^= true

                     

                    because the property 'locked' is a Boolean, not a number. It does work because Javascript internally changes the numbers 0 and Not-0 to boolean false and true (which may or may not be interesting to know, anyway).

                     

                    The expression a ^= b is shorthand for a = a ^ b. All of the simple math and logical functions can be written that way:

                     

                    a = a - b -> a -= b

                    a *= b -> a = a*b

                    a &= b -> a = a & b

                     

                    which may seem just to be a programmers' affection at first, but there are actual advantages to it. For example, not only would you have to write out this in full:

                     

                    app.documents.item(0).layers.itemByName("Layer 1").locked = app.documents.item(0).layers.itemByName("Layer 1").locked ^ true;

                     

                    but Javascript would have to parse it in its entirety as well. With the singular expression, the left hand part (the a above) is only evaluated once.

                     

                    The ^ command is a simple Exclusive-Or (a.k.a. XOR), that is one or the other, not neither nor both. If ("Layer 1").locked = false, then

                     

                    ("Layer 1").locked (new) = ("Layer 1").locked=false XOR true

                     

                    -- the left value (a) is false, the right value (b) is true, so in this case the result is "true", because it is 'true' that 'false XOR true' is "one OR the other". If the layer was locked, it would be

                     

                    ("Layer 1").locked(new) = ("Layer 1").locked=true XOR true

                     

                    and the expression "true XOR true" equals false because then it is NOT "one OR the other".

                    1 person found this helpful
                    • 7. Re: Toggling layer lock
                      winterm Level 4

                      Ugh...

                      you guys, are too smart for the real world tasks...

                       

                      nevertheless, Imagine's one liner just does the trick I needed - seamlessly switch dedicated layer lock on and off.

                      With KB shortcut assigned it speeds up my workflow, it's simply faster than hiting F7 and clicking on icons...

                       

                      Uwe, Jongware - your solutions are undoubtfully useful in the long run and in understanding how the whole stuff works.

                      Thank you all, I appreciate it and believe, other users will have benefit of it, too.