Skip to main content
Participating Frequently
October 30, 2011
Answered

(NOT SOLVED) Duplicate layers into another document

  • October 30, 2011
  • 2 replies
  • 8661 views

Hello, honorabe participants of Photoshop Scripting forum!

I have a problem - how to implement in action/script duplication of two linked layers from one document to another already opened document? Linked layers is a specially formatted text, so I can't just copy-paste them into opened document. Already opened document is a CR2-file.

When I record action, PS also record name of the reciever-document. But this name changes when I open another file so all acation breaks.

PS can record 'Select next document' and 'Select previous document', but cant' record 'Duplicate layers into previous document'.

Please, help me to solve my problem.

This topic has been closed for replies.
Correct answer dgolberg

I'm not clearly understand - is this all a news script? Or should I patch only DpLayer procedure by lines under /== line?

Anyway, I have tried both of methods.

In result - when I call script from DESTINATION, and enter in filename SOURCE, it duplicates layer from DEST to SOURCE. Should be other way - from SOURCE to DEST.

So, nothing changed to me :(


Hey Borizzzed,

Are you trying to run this on multiple files (ie: use this in a batch process)?  I do something very similar for work; but I'm not sure about linked layers.  You may have to put them in a group for them to stay linked the way I do it.  Anyway, the code I use is below:

#target photoshop

var activeDoc = app.activeDocument; //This stores the currently active document to the variable activeDoc

var originalDoc = app.documents.getByName("source.psd"); //This stores your source .psd file's name so Photoshop can look for the layers you wish to copy in the correct document.

    var theLayer = originalDoc.layerSets.getByName("text"); //This stores the group you wish to copy to the other document to a variable named theLayer

    app.activeDocument = originalDoc  //Switches the active document to the source document since duplicating can only be done from the front-most document.

    var theCopiedLayer = theLayer.duplicate(activeDoc, ElementPlacement.PLACEATBEGINNING);  //This places the group at the top of the layer stack in the target document.

If you're trying to batch run this; just record the script to an action and add a save option after it, then run that action with the batch.  Also, if you do run it as a batch; be sure the source .psd is open before you start the batch.  Anyway, give it a try and let me know if it works!

dgolberg

2 replies

January 20, 2015

Hello,

I just want to duplicate a layer on a particular place then what should I have to do..?

c.pfaffenbichler
Community Expert
Community Expert
January 20, 2015

With "place" do you mean a position on the canvas or a position in the Layer structure?

And why did you attach your post to this fairly old thread?

January 20, 2015

I mean a position in the Layer structure.
and I was trying this code that's why reply in this thread.

October 30, 2011

Hi Borizzzed!

This is not possible do to Action. Its only work for Script. here is it... the Script!!!

N'joy the scripting.

-yajiv

// Duplicate Layer one document to another.....

if ( app.documents.length >0 ) {

        var docRef = app.activeDocument

        var layerIndex = 0

        var visiblelayerCount = 0

        var activelayers = app.activeDocument.activelayers;

        var myLayers = docRef.layers;

        var selAlpha, Lname

        var Fname=prompt("Enter The file name with extension ...!","","Do Smart") // like temp1.jpg, yajiv.psd

           

        if (Fname!="")

        {

        for (var layerIndex = visiblelayerCount; layerIndex < myLayers.length; layerIndex++) {

            selAlpha=docRef.layers.getByName(myLayers[layerIndex].name);

            Lname=myLayers[layerIndex].name; // Selected current layer name

            if(Lname!="Background")

                DpLayer(Lname,Fname); // duplicate layer name to other Photoshop Document.

              }

                    }

        }

    function DpLayer(Lname,fname){                    

        var id59 = charIDToTypeID( "Dplc" );

        var desc14 = new ActionDescriptor();

        var id60 = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var id61 = charIDToTypeID( "Lyr " );

        var id62 = charIDToTypeID( "Ordn" );

        var id63 = charIDToTypeID( "Trgt" );

        ref14.putEnumerated( id61, id62, id63 );

        desc14.putReference( id60, ref14 );

            var id64 = charIDToTypeID( "T   " );

            var ref15 = new ActionReference();

            var id65 = charIDToTypeID( "Dcmn" );

                ref15.putName( id65, fname );

                desc14.putReference( id64, ref15 );

            var id66 = charIDToTypeID( "Vrsn" );

                desc14.putInteger( id66, 2 );

            executeAction( id59, desc14, DialogModes.NO );

                        }

BorizzzedAuthor
Participating Frequently
October 30, 2011

Sorry, but this script does nothing for me.

In my first file there is only one layer called "Background", so script even not going to call DpLayer procedure

Is it possible to fix script?

October 30, 2011

Ok... Use this one....

if ( app.documents.length >0 ) {

        var docRef = app.activeDocument

        var layerIndex = 0

        var visiblelayerCount = 0

        var activelayers = app.activeDocument.activelayers;

        var myLayers = docRef.layers;

        var selAlpha, Lname

        var Fname=prompt("Enter The file name with extension ...!","","Do Smart") // like temp1.jpg, yajiv.psd

           

        if (Fname!="")

        {

        for (var layerIndex = visiblelayerCount; layerIndex < myLayers.length; layerIndex++) {

            selAlpha=docRef.layers.getByName(myLayers[layerIndex].name);

            Lname=myLayers[layerIndex].name; // Selected current layer name

            DpLayer(Lname,Fname); // duplicate layer name to other Photoshop Document.

              }

                    }

        }

    function DpLayer(Lname,fname){                    

        var id59 = charIDToTypeID( "Dplc" );

        var desc14 = new ActionDescriptor();

        var id60 = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var id61 = charIDToTypeID( "Lyr " );

        var id62 = charIDToTypeID( "Ordn" );

        var id63 = charIDToTypeID( "Trgt" );

        ref14.putEnumerated( id61, id62, id63 );

        desc14.putReference( id60, ref14 );

            var id64 = charIDToTypeID( "T   " );

            var ref15 = new ActionReference();

            var id65 = charIDToTypeID( "Dcmn" );

                ref15.putName( id65, fname );

                desc14.putReference( id64, ref15 );

            var id66 = charIDToTypeID( "Vrsn" );

                desc14.putInteger( id66, 2 );

            executeAction( id59, desc14, DialogModes.NO );

                        }