Skip to main content
klaszlo2004
Participant
December 2, 2014
Question

Illustrator Script: How can I duplicate a layer ( with sublayers ) with javascript code ?

  • December 2, 2014
  • 1 reply
  • 3203 views

Dear All

I want to duplicate a layer structure, as the "Duplicate Layer" submenu do with Javascript code, but I can't.

I can duplicate only PageItems or selected object, but no Layers.

Anybody knows how can I do it ?

This topic has been closed for replies.

1 reply

Qwertyfly___
Legend
December 3, 2014

Not sure that you can in one command.

have a look at this thread and see if it helps:

duplicate layer to different document

klaszlo2004
Participant
December 3, 2014

Thanks for your answer.

  1. #target illustrator 
  2.  
  3. var docRef = app.activeDocument; 
  4.  
  5. var ln = 'Layer 1'
  6.  
  7. var ol = docRef.layers.getByName(ln); 
  8.  
  9. var nl = docRef.layers.add(); 
  10.  
  11. nl.name = ln+' Copy'
  12.  
  13. for (var a = ol.pageItems.length-1; a >= 0; a--) { 
  14.  
  15.     ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING); 
  16.  

The problem with this code is, that it can't duplicate also the sublayers, and the sublayer elements.

Sure I can write a recursive function, but I think there should exist a duplicate function somewhere ( for Photoshop script exist ! ) or a small trick, but I can't find it.

Thanks

CarlosCanto
Community Expert
Community Expert
December 3, 2014

you can't compare PS or InD scripting to Illustrator, they're much better supported than Illustrator is.

small trick...create a new action, duplicate a layer (via layer's panel menu), stop recording. Write a script to play such action (it will duplicate the Active Layer).