4 Replies Latest reply on Aug 20, 2014 11:09 AM by Bob Haslett
      • 1. Re: How do I flatten the layers in an illustrator document using extendscript
        CarlosCanto Adobe Community Professional & MVP

        I don't see that method, unless it is new to CC, or perhaps Photoshop?

         

        workarounds, if you can record that in an action, your script could run such action.

         

        Or, the hard way, move everything manually.

        • 2. Re: How do I flatten the layers in an illustrator document using extendscript
          Bob Haslett Level 1

          I've tried this instead. Basically I'm trying to take all the artwork on a particular layer and move it to another one. Once I've cleared out the layer I'll delete it so I'll effectively end up flattening the document. However I can't get this to work. Is this because I have to form a GroupItem and move everything into that then move the group item to a different layer first?

           

          var doc = activeDocument;

          var destination=app.activeDocument.layers[1]

          $.writeln(destination.name);

           

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

               var currentLayer = app.activeDocument.layers[i];

               $.writeln(currentLayer.name)

               if(currentLayer.name!="Contents") {

                   var contents=currentLayer.hasSelectedArtwork = true;

                   contents.moveTo(destination);

                  

                   }

             

              }

          • 3. Re: Re: How do I flatten the layers in an illustrator document using extendscript
            CarlosCanto Adobe Community Professional & MVP
            I can't get this to work. Is this because I have to form a GroupItem and move everything into that then move the group item to a different layer first?

            you could make a group first, but why create an extra step? moving items into a group takes the exact same effort as moving them into a Layer

             

            this is not good

            var contents=currentLayer.hasSelectedArtwork = true;

             

            the contents variable has a value of "true"; not the actual selected objects, causing the next line to break the script.

             

            you have to move every item in the Layer, one by one...and there's no need to select them first.

             

            var doc = activeDocument;
            var destination=app.activeDocument.layers[1]
            $.writeln(destination.name);
            
            // loop backwards, to keep items in the right stacking order
            for (var i = doc.layers.length-1; i>=0; i--) {
                 var currentLayer = app.activeDocument.layers[i];
                 $.writeln(currentLayer.name)
                 if(currentLayer.name!="Contents") {
                     var itemcount = currentLayer.pageItems.length;
                     
                     // loop through all items...backwards, each time you move an item out of the layer
                     // the remaining items get re-indexed
                     for (var a=itemcount-1; a>=0; a--) {
                        var pgItem = currentLayer.pageItems[a];
                        
                        // moveTo() method is deprecated, use move() instead
                        pgItem.move(destination, ElementPlacement.PLACEATBEGINNING);
                    }
                 }
            }