Skip to main content
Participating Frequently
March 26, 2011
Answered

Duplicate layer to different document with new layer name.

  • March 26, 2011
  • 1 reply
  • 5465 views

I am trying to replicate the Photoshop function of "Duplicate Layer" where I can take a layer from an active document and duplicate it to a target document AND set the name of the new layer once it is copied.

Javascript is the language.

Windows CS4 if it matters.

Any suggested code snippets?

Thanks

-Mike

This topic has been closed for replies.
Correct answer Michael_L_Hale

Sorry, I missed the 'target doc' in your question.

app.activeDocument.activeLayer.duplicate (app.documents.getByName('ProofMaster.psd'));// dupe the layer
app.activeDocument = app.documents.getByName('ProofMaster.psd');// switch to master doc
app.activeDocument.activeLayer.name = 'Whatever';// the active layer is the one justed dupped so rename

1 reply

Inspiring
March 26, 2011

This will dupe the activeDocument to a new document.

function newDocFromLayer(docName,layerName){
     docName == undefined ? docName = "Untitled":docName;
     layerName == undefined ? layerName = activeDocument.activeLayer.name:layerName;
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putClass( charIDToTypeID( "Dcmn" ) );
    desc.putReference( charIDToTypeID( "null" ), ref );
    desc.putString( charIDToTypeID( "Nm  " ), docName );
         var ref1 = new ActionReference();
        ref1.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
    desc.putReference( charIDToTypeID( "Usng" ), ref1 );
    desc.putString( charIDToTypeID( "LyrN" ), layerName );
executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
};

Participating Frequently
March 26, 2011

VERY VERY VERY VERY close. Great turnaround!!!!

The difference was my mistake in describing my situation.

Instead of a brand new document, I have an existing document already open that I want to copy the layer to.  The open document is a template that I am dropping images into.  The filename of the open template is "ProofMaster.psd".

What do I have to change to point to that file instead of creating a new one?

Thanks.

-Mike

Michael_L_HaleCorrect answer
Inspiring
March 26, 2011

Sorry, I missed the 'target doc' in your question.

app.activeDocument.activeLayer.duplicate (app.documents.getByName('ProofMaster.psd'));// dupe the layer
app.activeDocument = app.documents.getByName('ProofMaster.psd');// switch to master doc
app.activeDocument.activeLayer.name = 'Whatever';// the active layer is the one justed dupped so rename