19 Replies Latest reply on Jan 21, 2015 1:11 AM by niharika28

    (NOT SOLVED) Duplicate layers into another document

    Borizzzed

      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.

        • 1. Re: (NOT SOLVED) Duplicate layers into another document
          yajiv_viki Level 2

          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 );

                                  }

          • 2. Re: (NOT SOLVED) Duplicate layers into another document
            Borizzzed Level 1

            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?

            • 3. Re: (NOT SOLVED) Duplicate layers into another document
              yajiv_viki Level 2

              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 );

                                      }

              • 4. Re: (NOT SOLVED) Duplicate layers into another document
                Borizzzed Level 1

                Now better, but my PS CS4 shows an error:

                Snap3.png

                I have highlighted source file name, from which I duplicate all layers.

                • 5. Re: (NOT SOLVED) Duplicate layers into another document
                  yajiv_viki Level 2

                  Hi Borizzzed!,

                   

                  I think you are not openning the both Photoshop document. Before run this script.

                   

                  you are openning the both Photoshop document then run the script.

                   

                  Steps to run script:

                   

                  • both document open to photoshop(ie.. Source and final).
                  • Select document and run the script.
                  • Now, source document layer move to Final Photoshop document.
                  • That's it. Try again....!!!

                   

                  - yajiv

                  • 6. Re: (NOT SOLVED) Duplicate layers into another document
                    Borizzzed Level 1

                    Hmmm... lemme describe:

                    1. I open destination file;
                    2. I open source file;
                    3. I change document to source;
                    4. Run the script;
                    5. Paste in filename editbox full path to destination file;
                    6. Got duplicated layers two times

                     

                    It's not correct, I think. I cant' use script in such implementation in action cause I need paste different filenames in editbox. I can hardcode source filname to script to use in action.

                     

                    I have recorded a video for illustration:

                     

                    Can you modify script as follows?

                    1. I open destination file;
                    2. Run the script with hardcoded location of sourcefile;
                    3. Got duplicated layers in destination file one times
                    • 7. Re: (NOT SOLVED) Duplicate layers into another document
                      yajiv_viki Level 2

                      Hi Borizzzed!,

                       

                      Try 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

                                  //alert(Lname);   

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

                                    }

                                          }

                              }

                       

                       

                          function DpLayer(Lname,fname)

                          {                    

                             

                                  var id766 = charIDToTypeID( "slct" );

                                  var desc45 = new ActionDescriptor();

                                  var id767 = charIDToTypeID( "null" );

                                      var ref9 = new ActionReference();

                                      var id768 = charIDToTypeID( "Lyr " );

                                      ref9.putName( id768, Lname );

                                  desc45.putReference( id767, ref9 );

                                  var id769 = charIDToTypeID( "MkVs" );

                                  desc45.putBoolean( id769, false );

                              executeAction( id766, desc45, DialogModes.NO );

                       

                              // =======================================================

                              var id770 = charIDToTypeID( "Dplc" );

                                  var desc46 = new ActionDescriptor();

                                  var id771 = charIDToTypeID( "null" );

                                      var ref10 = new ActionReference();

                                      var id772 = charIDToTypeID( "Lyr " );

                                      var id773 = charIDToTypeID( "Ordn" );

                                      var id774 = charIDToTypeID( "Trgt" );

                                      ref10.putEnumerated( id772, id773, id774 );

                                  desc46.putReference( id771, ref10 );

                                  var id775 = charIDToTypeID( "T   " );

                                      var ref11 = new ActionReference();

                                      var id776 = charIDToTypeID( "Dcmn" );

                                      ref11.putName( id776, fname );

                                  desc46.putReference( id775, ref11 );

                                  var id777 = charIDToTypeID( "Vrsn" );

                                  desc46.putInteger( id777, 2 );

                              executeAction( id770, desc46, DialogModes.NO );

                         

                                              }

                      • 8. Re: (NOT SOLVED) Duplicate layers into another document
                        Borizzzed Level 1

                        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 :(

                        • 9. Re: (NOT SOLVED) Duplicate layers into another document
                          yajiv_viki Level 2

                          Hi Borizzzed!,

                           

                          its not a new script. its a updated script from old.

                          its works fine in my system. i don't know why this script not working to you.

                           

                          -yajiv

                          • 10. Re: (NOT SOLVED) Duplicate layers into another document
                            dgolberg Level 1

                            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

                            • 11. Re: (NOT SOLVED) Duplicate layers into another document
                              Borizzzed Level 1

                              Thank you for your help!

                              Your script is pretty easy to understand and overmore - it works just like it should =)

                              Thank you much!

                              • 12. Re: (NOT SOLVED) Duplicate layers into another document
                                dgolberg Level 1

                                No problem.  Glad I could help!

                                • 13. Re: (NOT SOLVED) Duplicate layers into another document
                                  niharika28

                                  Hello,

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

                                  • 14. Re: (NOT SOLVED) Duplicate layers into another document
                                    c.pfaffenbichler Level 9

                                    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?

                                    • 15. Re: (NOT SOLVED) Duplicate layers into another document
                                      niharika28 Level 1

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

                                      • 16. Re: (NOT SOLVED) Duplicate layers into another document
                                        c.pfaffenbichler Level 9

                                        I mean a position in the Layer structure.

                                        Which position is it sat current and which should it be?

                                        • 17. Re: (NOT SOLVED) Duplicate layers into another document
                                          niharika28 Level 1

                                          layout.PNG

                                           

                                          I just want to copy a layer from another document and paste into the selection of this document.

                                          • 18. Re: (NOT SOLVED) Duplicate layers into another document
                                            c.pfaffenbichler Level 9

                                            A Selection is not a position in the Layer structure, duplicating a Layer is not the same as pasting, …

                                            You may want to start a new thread in which you clearly explain the intended procedure.