4 Replies Latest reply on Mar 26, 2011 1:44 PM by Michael L Hale

    Duplicate layer to different document with new layer name.

    FairfieldPhotoOH Level 1

      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

        • 1. Re: Duplicate layer to different document with new layer name.
          Michael L Hale Level 5

          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 );
          };
          
          1 person found this helpful
          • 2. Re: Duplicate layer to different document with new layer name.
            FairfieldPhotoOH Level 1

            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

            • 3. Re: Duplicate layer to different document with new layer name.
              Michael L Hale Level 5

              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
              
              • 4. Re: Duplicate layer to different document with new layer name.
                FairfieldPhotoOH Level 1

                excellent.  I also learned how to work the Script Listener so I have a better understanding of what is happening now.

                 

                Color me dangerous now...

                 

                Thank you very much!

                -Mike