Skip to main content
Known Participant
April 21, 2014
Answered

Duplicate a layer with a new Name

  • April 21, 2014
  • 1 reply
  • 592 views

I am writing a script that duplicates all layers in a document, but adds a new name (In this case, it's the original layer name with the words Hat at the end of it).

My script is pasted below and works fine (It's based on another script I wrote).

I've just got a few questions:

1) I only want it to work on selected layers and not every layer in the document. How can I do this?

2) When the script is finished, it displyas the following error message:

Error 2: sourceDoc is undefined.

Line: 9

->                                with(sourceDoc) {

I don't understand this error, could someone explain it. Thanks!

Here is my code:

var myDoc = app.activeDocument; // returns the document object

var myLayers = myDoc.layers;

//Go through all layers of source document and copy artwork

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

          myLayers.hasSelectedArtwork = true;

};

with(sourceDoc) {

          var count = pageItems.length;

          for (var i = 0; i < count; i++) {

                    pageItems.selected = true;

          }

          redraw();

          copy();

          for (var i = 0; i < count; i++) {

                    pageItems.selected = false;

          }

          var title = myLayers.name;

          var title = title + "hat";

          newLayer = myDoc.layers.add();

          newLayer.name = title;

          //Paste into this new layer

          newLayer = app.paste();

}

This topic has been closed for replies.
Correct answer pixxxelschubser

Hi big_smile,

Replace your sourceDoc with your myDoc. That's the reason for the error message. But there are some more …

Can you test, if the following code works for you?

(I wrote this only quick & dirty)

// LayersTopLevel_Duplicate.jsx

// quick&dirty

// use it on your own risk

// regards pixxxelschubser

var aDoc = app.activeDocument;

var theLayers = aDoc.layers;

var count = theLayers.length-1;

var pI, count2;

for (i = count; i >= 0; i--) {

    aDoc.layers.add();

    redraw();

    }

for (j = count; j >= 0; j--) {

    count2 = j+count+1;

    theLayers.name = theLayers[count2].name + " hat";

    pI = theLayers[count2].pageItems;

    for (k=pI.length-1; k>=0;k--) {

    pI.duplicate(theLayers, ElementPlacement.PLACEATBEGINNING);

    }

    redraw();

    }

NOTE: this code will only works with top level layers.

Have fun

1 reply

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
April 21, 2014

Hi big_smile,

Replace your sourceDoc with your myDoc. That's the reason for the error message. But there are some more …

Can you test, if the following code works for you?

(I wrote this only quick & dirty)

// LayersTopLevel_Duplicate.jsx

// quick&dirty

// use it on your own risk

// regards pixxxelschubser

var aDoc = app.activeDocument;

var theLayers = aDoc.layers;

var count = theLayers.length-1;

var pI, count2;

for (i = count; i >= 0; i--) {

    aDoc.layers.add();

    redraw();

    }

for (j = count; j >= 0; j--) {

    count2 = j+count+1;

    theLayers.name = theLayers[count2].name + " hat";

    pI = theLayers[count2].pageItems;

    for (k=pI.length-1; k>=0;k--) {

    pI.duplicate(theLayers, ElementPlacement.PLACEATBEGINNING);

    }

    redraw();

    }

NOTE: this code will only works with top level layers.

Have fun