5 Replies Latest reply on Dec 6, 2012 8:22 PM by Michael L Hale

    How to move layers into group.

    stophobia

      Hi.

       

      I want to make group and input it's name and get layers into it.

       

      var doc = app.activeDocument;

      var layers2 = doc.layers;

      var txt = prompt("Input layer group name :");

      var layerSetRef = doc.layerSets.add();

      layerSetRef.name = txt;

      for(var i = 0; i < layers2.length; i++) {

      layers2[i].move(layerSetRef, ElementPlacement.PLACEATEND);

      }

       

      Can you tell me why this code shows error?

       

      Indicator shows "layers2[i].move(layerSetRef, ElementPlacement.PLACEATEND);" is not right.

        • 1. Re: How to move layers into group.
          Paul Riggott Level 6

          This is one way of doing it...

           

           

          main();
          function main(){
          var txt = prompt("Input layer group name :",'');
          if(txt == null ) return;
          selectAllLayers();
          createLayerSet();
          activeDocument.activeLayer.name = txt;
          }
          function selectAllLayers() {
              var desc29 = new ActionDescriptor();
                  var ref23 = new ActionReference();
                  ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
              desc29.putReference( charIDToTypeID('null'), ref23 );
              executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
          }
          function createLayerSet() {
          var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putClass( stringIDToTypeID('layerSection') );
          desc.putReference( charIDToTypeID('null'), ref );
          var ref2 = new ActionReference();
          ref2.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
          desc.putReference( charIDToTypeID('From'), ref2 );
          executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );
          };
          
          
          • 2. Re: How to move layers into group.
            Michael L Hale Level 5

            I see two main problems with your code.

             

            One is that the enum should be ElementPlacement.INSIDE when moving an artLayer into a layerSet.

             

            The second problem is moving layers changes the index so you can not loop using the index.

             

            Try something like this:

             

            var doc = app.activeDocument;
            var txt = prompt("Input layer group name :");
            var layerSetRef = doc.layerSets.add();
            layerSetRef.name = txt;
            while(doc.artLayers.length > Number(doc.layers[doc.layers.length-1].isBackgroundLayer) ) {
                if(!doc.artLayers[0].allLocked || !doc.artLayers[0].positionLocked ){ 
                    doc.artLayers[0].move(layerSetRef, ElementPlacement.INSIDE);
                }
            }
            

            That should move all the top level artLayers into the layerSet. It will skip locked layers and the background layer. You could change the layer locking test to change those properties if you want to move locked layers. To move the background layer you will need to convert it to a normal artLayer before moving.

            • 3. Re: How to move layers into group.
              stophobia Level 1

              This is great! Thanks you!

              It works perfectly!

              I can't think about the way using "while"...

              • 4. Re: How to move layers into group.
                stophobia Level 1

                Oh, I tried this code and surprised about the speed!

                Sadly, frankly speaking, I can't understand some code you wrote.

                Yes, It's difficult to me...

                I will build up my skill to analize this code...

                • 5. Re: How to move layers into group.
                  Michael L Hale Level 5

                  Because the first four lines of what I posted are the same as what you posted I assume the part you don't understand is in the while loop.

                   

                  Number(doc.layers[doc.layers.length-1].isBackgroundLayer) checks to see if the bottom most layer is the background layer then converts that into either a 0 or 1. That allows the loop to skip the background layer if it exists.

                   

                  Then there is an if statement to check for layer locking. If you try to move an allLocked or positionLocked layer it will cause an error.

                   

                  Let me know if there is any other parts you don't understand.