Illustrator Script: How can I duplicate a layer ( with sublayers ) with javascript code ?
Copy link to clipboard
Copied
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 ?
Explore related tutorials & articles
Copy link to clipboard
Copied
Not sure that you can in one command.
have a look at this thread and see if it helps:
Copy link to clipboard
Copied
Thanks for your answer.
- #target illustrator
- var docRef = app.activeDocument;
- var ln = 'Layer 1';
- var ol = docRef.layers.getByName(ln);
- var nl = docRef.layers.add();
- nl.name = ln+' Copy';
- for (var a = ol.pageItems.length-1; a >= 0; a--) {
- ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING);
- }
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
Copy link to clipboard
Copied
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).

