Skip to main content
Inspiring
August 13, 2014
Answered

How do I flatten the layers in an illustrator document using extendscript

  • August 13, 2014
  • 1 reply
  • 1359 views

The nearest I can find is

   activeDocument.mergeVisibleLayers(); 

But this doesn't work in illustrator CS5

Thanks

This topic has been closed for replies.
Correct answer Bob Haslett
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;

     $.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;

           

            // moveTo() method is deprecated, use move() instead

            pgItem.move(destination, ElementPlacement.PLACEATBEGINNING);

        }

     }

}


Got it thats great thanks very much

CarlosCanto
Community Expert
Community Expert
August 13, 2014

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.

Inspiring
August 18, 2014

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;

     $.writeln(currentLayer.name)

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

         var contents=currentLayer.hasSelectedArtwork = true;

         contents.moveTo(destination);

        

         }

   

    }

CarlosCanto
Community Expert
Community Expert
August 19, 2014
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;

     $.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;

           

            // moveTo() method is deprecated, use move() instead

            pgItem.move(destination, ElementPlacement.PLACEATBEGINNING);

        }

     }

}