30 Replies Latest reply on Jun 15, 2015 8:27 AM by YaoyongYu

    Script to copy Layer/Group to another Open Document?

    boo radley 2013 Level 3

      Hi, I know this topic has been covered many times before. But to clarify, here's what I'm wondering:

       

      Copy the active layer/group to another open document in Photoshop. The simple way is to go to "Layer/Duplicate Layer..." . But this is very tedious. For starters, when you choose that method, and then select the document that you'd like to copy to via the pull-down menu, it successfully copies the layer/group to that document, but it doesn't auto-switch (activate) to the document it was just copied to. You have to hit "Ok", and then manually search through the open documents list via (Window/*choose your document*) to get there. This is a nightmare when you're trying to do this many times.

       

      Here is what I am wondering:

       

      Is it possible to:

       

      a) copy the active layer/group to the clipboard/memory

      b) switch to another existing document in Photoshop *via a custom menu* which shows all existing documents

      c) once switched to the new existing document, paste that layer/group from the clipboard into that document

       

       

       

      ..is that possible?

       

      At the very least, are there any scripts that anyone knows of that allows the user to switch between existing documents via a custom menu instead of going to Window/*choose your document* ?

        • 1. Re: Script to copy Layer/Group to another Open Document?
          boo radley 2013 Level 3

          to further simpliy this:

           

          are there any scripts that:

           

          will open up a custom menu, and show a list of all existing documents that are currently loaded into Photoshop. This menu will allow the user to select the document they wish toi switch to, and at the same time copy that document name to the clipboard/memory?

          • 2. Re: Script to copy Layer/Group to another Open Document?
            pixxxel schubser MVP & Adobe Community Professional

            Hi @boo radley 2013,

             

            do you mean something like this?

            // LayerPaste2anotherDoc.jsx
            // https://forums.adobe.com/thread/1496531
            
            // regards pixxxelschubser
            
            var aDoc = app.activeDocument;
            var AllDocs = app.documents;
            var actLay = aDoc.activeLayer;
            
            if (AllDocs.length > 1) {
            var itemDoc = null;
            actLay.copy ();
            
            var win = new Window("dialog","Copy the active layer");
            this.windowRef = win;
            win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");
            win.NewList=win.add ("dropdownlist", undefined, AllDocs);
            
            win.NewList.selection = 0;
            itemDoc = win.NewList.selection.index;
            
            win.cancelBtn = win.add("button", undefined, "Abbruch");
            win.quitBtn = win.add("button", undefined, "Ok");
            win.defaultElement = win.quitBtn;
            win.cancelElement = win.cancelBtn;
            win.quitBtn.onClick = function() {
            win.close();
            }
            
            win.NewList.onChange= function () {
                itemDoc = win.NewList.selection.index;
                return itemDoc;
                }
            
            win.show();
            
            app.activeDocument = app.documents[itemDoc];
            app.activeDocument.paste();
            app.refresh();
            } else {
                alert ("No other documents open")
                }
            
            

             

            Have fun

             

            • 3. Re: Script to copy Layer/Group to another Open Document?
              boo radley 2013 Level 3

              Thats it!! Awesome!!! Totally what I was looking for - this is a very useful script, i hope a lot of people see this one because they will all love it- thank you my friend!!

              • 4. Re: Script to copy Layer/Group to another Open Document?
                boo radley 2013 Level 3

                one question though: is there a way to have this script work with both layers *and* groups? Right now it's only working with layers. If not, then maybe have two seperate scripts, one for layers and one for groups?

                • 5. Re: Script to copy Layer/Group to another Open Document?
                  pixxxel schubser MVP & Adobe Community Professional

                  Sorry, but to copy a group to another document is much more complex. Don't use Extend Script for this. Better is to work with Script listener Plugin (AM code) for this.

                   

                  Or with Extend Script you should loop through the elements of your group and copy part by part. But there are still some things to consider.

                  What is if you have nested groups or deep nested groups inside of a group?

                  What is if you have adjustment layers?

                  What is if you have smart objects?

                   

                  And sorry for my bad english. I hope you understand nevertheless what I mean.

                   

                  One thing more:

                  You've never marked one of your threads as correct - why not?

                   

                  • 6. Re: Re: Script to copy Layer/Group to another Open Document?
                    c.pfaffenbichler Level 9

                    This might help with Groups and some other kinds of Layers.

                    But it takes the easy route with the target file by using the name to identify it.

                    // some amendments to pixxxelschubser’s code;
                    // 2014, use it at your own risk;
                    #target photoshop
                    
                    
                    var aDoc = app.activeDocument;
                    var AllDocs = app.documents; 
                    var actLay = aDoc.activeLayer;
                    var theIndex = getSelectedLayersIdx();
                    
                    if (AllDocs.length > 1) {
                    var itemDoc = null;
                    
                    var win = new Window("dialog","Copy the active layer"); 
                    this.windowRef = win; 
                    win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");
                    win.NewList=win.add ("dropdownlist", undefined)
                    for (var m = 0; m < AllDocs.length; m++) {
                    win.NewList.add("item", AllDocs[m].name)
                    };
                    
                    win.NewList.selection = 0; 
                    itemDoc = win.NewList.selection.index; 
                    
                    win.cancelBtn = win.add("button", undefined, "Abbruch"); 
                    win.quitBtn = win.add("button", undefined, "Ok"); 
                    win.defaultElement = win.quitBtn; 
                    win.cancelElement = win.cancelBtn; 
                    win.quitBtn.onClick = function() { 
                    win.close(); 
                    } 
                    
                    win.NewList.onChange= function () { 
                        itemDoc = win.NewList.selection.index; 
                        return itemDoc; 
                        } 
                    
                    win.show();
                    // duplicate layers;
                    for (var n = 0; n < theIndex.length; n++) {
                    duplicateLayer (theIndex[n], String(win.NewList.selection))
                    };
                    //
                    app.refresh(); 
                    } else { 
                        alert ("No other documents open") 
                        };
                    ////// duplicate layer //////
                    function duplicateLayer (theIndex, theDoc) {
                    // =======================================================
                    var idDplc = charIDToTypeID( "Dplc" );
                        var desc7 = new ActionDescriptor();
                        var idnull = charIDToTypeID( "null" );
                            var ref3 = new ActionReference();
                        ref3.putIndex(charIDToTypeID("Lyr "), theIndex);
                        desc7.putReference( idnull, ref3 );
                        var idT = charIDToTypeID( "T  " );
                            var ref4 = new ActionReference();
                            var idDcmn = charIDToTypeID( "Dcmn" );
                            ref4.putName( idDcmn, theDoc );
                        desc7.putReference( idT, ref4 );
                        var idVrsn = charIDToTypeID( "Vrsn" );
                        desc7.putInteger( idVrsn, 5 );
                    executeAction( idDplc, desc7, DialogModes.NO );
                    };
                    ////// by paul mr;
                    function getSelectedLayersIdx(){
                          var selectedLayers = new Array;
                          var ref = new ActionReference();
                          ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                          var desc = executeActionGet(ref);
                          if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
                            desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
                              var c = desc.count
                              var selectedLayers = new Array();
                              for(var i=0;i<c;i++){
                                try{
                                  activeDocument.backgroundLayer;
                                  selectedLayers.push(  desc.getReference( i ).getIndex() );
                                }catch(e){
                                  selectedLayers.push(  desc.getReference( i ).getIndex()+1 );
                                }
                              }
                          }else{
                            var ref = new ActionReference();
                            ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
                            ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                            try{
                                activeDocument.backgroundLayer;
                                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
                            }catch(e){
                                selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
                            }
                          }
                          return selectedLayers;
                    };
                    
                    • 7. Re: Script to copy Layer/Group to another Open Document?
                      c.pfaffenbichler Level 9

                      You've never marked one of your threads as correct - why not?

                      From a quick look at the general Forum it seems two of twenty threads are marked as correctly answered over there … personally I suspect more than that would deserve to be considered answered, so the Photoshop Scripting Forum may have posters that are more observant than average already.

                       

                      I guess if you are willing to take the time and try to help other users on these Fora you better get used to a lot of them not taking the "Best Practices" too seriously.

                      Best practices: Usage

                      And as for the points that are awarded for a "correct answer" I guess it probably evens out in the long run, everybody else’s correct answers are just as likely to not be marked accordingly.

                      • 8. Re: Script to copy Layer/Group to another Open Document?
                        pixxxel schubser MVP & Adobe Community Professional

                        Hi c.pfaffenbichler,

                        I often use the forum search. I preferred reading the correct answers and (in the past) as helpful marked answers, especially in long threads. Others are probably the same opinion.

                         

                        What photoshop version do you use?

                        var idT = charIDToTypeID( "T  " );

                        gives an error in CS3 and CS5.1 under Windows.

                        • 9. Re: Script to copy Layer/Group to another Open Document?
                          boo radley 2013 Level 3

                          sorry guys, i wasn't even aware of the 'correct answer' feature - i'll keep my eyes on it from now on

                          • 10. Re: Script to copy Layer/Group to another Open Document?
                            pixxxel schubser MVP & Adobe Community Professional

                            That was not really a problem for me. The explanation is in my other post # 8.

                             

                            And unfortunately there is no possibility more to mark a post as helpful (since the last Jive update). In my eyes that is very sad.

                             

                            By the way, does c.fpaffenbichlers script works for you?

                            • 11. Re: Script to copy Layer/Group to another Open Document?
                              boo radley 2013 Level 3

                              no, it is giving me the "var idT = charIDToTypeID( "T  " );" error

                               

                              I use Photoshop CS6

                              • 12. Re: Script to copy Layer/Group to another Open Document?
                                c.pfaffenbichler Level 9

                                And unfortunately there is no possibility more to mark a post as helpful (since the last Jive update). In my eyes that is very sad.

                                Well, the last Forum update certainly has (despite some improvements) made quite a lot of stuff worse.


                                no, it is giving me the "var idT = charIDToTypeID( "T  " );" error

                                It should be "T   " with three spaces (and I checked, that’s the way it is in my Script and that’s the way the text in the clipboard is pasted in Indesign for example) but it gets pasted here as two spaces.

                                • 13. Re: Script to copy Layer/Group to another Open Document?
                                  c.pfaffenbichler Level 9
                                  I often use the forum search. I preferred reading the correct answers and (in the past) as helpful marked answers, especially in long threads. Others are probably the same opinion.

                                  Fair enough, the Fora would benefit from these features being used as intended, but with recent changes the whole Forum experience has been degraded anyway …

                                  • 14. Re: Script to copy Layer/Group to another Open Document?
                                    pixxxel schubser MVP & Adobe Community Professional

                                    Hi c.pfaffenbichler,

                                    a short feedback: Like you said, with the „third space“ your code now works well in CS3 and CS5 .

                                    The insertion point for the selected layer (or group) is in both versions the topleft corner in the other document.

                                     

                                     

                                    • 15. Re: Script to copy Layer/Group to another Open Document?
                                      boo radley 2013 Level 3

                                      yes, now it works - ensured that there were '3' empty spaces instead of "2". Don't know why that happened, but it's good now. I added one line of code at the end of the c.pfaffenbichler's script, which causes Photoshop to auto-switch to the destination document after the layer/group has been copied to it. Originally, it just stayed on the current document after copying. Having 2 versions of this script is nice, because sometimes you'll want to auto-switch to the destination document that the layer was copied to it instead of having to search through all open documents and then manually switching to it - a pain, especially if you have many documents open at once. The second version of the script (c.pfaffenbichler's original script) keeps you on the current document even after the layer/group was copied to another document, and this is useful if you're copying multiple layers/groups over to another doc - it would be a pain switching back, forth, back, forth just to copy a few layers over. thanks guys

                                      • 16. Re: Script to copy Layer/Group to another Open Document?
                                        ramud57123156

                                        hi,

                                        Is there any way to copy selected layers and groups to all the open documents which are having the same name, without prompting us to select the particular target document.

                                         

                                        For example open documents name pattern as below mentioned

                                        87654321.A1B3C4D2E6.FRNT.extension

                                             (A)                (B)            (C)


                                        If target documents name ( A1B3C4D2E6 (B) ) matches with active document name ( A1B3C4D2E6 (B) ) then selected layers and groups in the active document should get copied to those target documents without any dialogue window.


                                        Thanks in advance

                                        • 17. Re: Script to copy Layer/Group to another Open Document?
                                          c.pfaffenbichler Level 9

                                          If I understand the naming convention correctly this should get you an array of the open documents that meet the requirement.

                                          You could use the array to perform whatever tasks on those documents.

                                          If you are likely to have large numbers of files open AM code might offer a better performance, though.

                                          // 2015, use it at your own risk;
                                          #target photoshop
                                          if (app.documents.length > 0) {
                                          var myDocument = app.activeDocument;
                                          var theNameSegment = myDocument.name.split(".")[1];
                                          var theDocs = new Array;
                                          var openDocs = app.documents;
                                          for (var m = 0; m < openDocs.length; m++) {
                                            var thisDoc = openDocs[m];
                                            if (thisDoc != myDocument) {
                                            var thisNameSegment = thisDoc.name.split(".")[1];
                                            if (thisNameSegment == theNameSegment) {theDocs.push(thisDoc)}
                                            }
                                            };
                                          alert (theDocs.join("\n"));
                                          };
                                          
                                          • 18. Re: Script to copy Layer/Group to another Open Document?
                                            ramud57123156 Level 1

                                            Thank you for the assistance

                                             

                                            I have got confused.

                                            Could you please post the complete script?

                                            • 19. Re: Script to copy Layer/Group to another Open Document?
                                              c.pfaffenbichler Level 9

                                              What about this is causing you confusion?

                                              • 20. Re: Script to copy Layer/Group to another Open Document?
                                                ramud57123156 Level 1

                                                confusion is about where to wrap this code in existing script.

                                                I'm brand new to scripting and in learning stage don't know much more about scripting.

                                                I tried in many ways but not getting output.

                                                • 21. Re: Script to copy Layer/Group to another Open Document?
                                                  c.pfaffenbichler Level 9

                                                  I don’t know which Script you are using, but where now it perform the task on one target document you can use a for-clause to do the same for each of the target documents.

                                                  • 22. Re: Script to copy Layer/Group to another Open Document?
                                                    ramud57123156 Level 1

                                                    Thank you for quick response.

                                                    I'm using the same script which you posted in this thread on Jun 15, 2014 7:33 AM.

                                                     

                                                    • 23. Re: Script to copy Layer/Group to another Open Document?
                                                      c.pfaffenbichler Level 9

                                                      In this case I would recommend not going with the DOM documents but collecting their IDs.

                                                      var myDocument = app.activeDocument;

                                                      // get active document’s id;

                                                      var ref = new ActionReference();

                                                      ref.putProperty(stringIDToTypeID ("property"),stringIDToTypeID("documentID") );

                                                      ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                                                      var docDesc = executeActionGet(ref);

                                                      var theID = docDesc.getInteger(stringIDToTypeID("documentID"));

                                                      // get name;

                                                      var theNameSegment = myDocument.name.split(".")[1];

                                                      var theDocs = new Array;

                                                      var openDocs = app.documents;

                                                      // work through open documents;

                                                      for (var m = 1; m <= openDocs.length; m++) {

                                                      var ref = new ActionReference();

                                                      ref.putIndex( charIDToTypeID("Dcmn"), m);

                                                      var docDesc = executeActionGet(ref);

                                                      var thisID = docDesc.getInteger(stringIDToTypeID("documentID"));

                                                      var thisName = docDesc.getString(stringIDToTypeID("title"));

                                                      var thisNameSegment = thisName.split(".")[1];

                                                      // collect names and ids;

                                                      if (thisNameSegment == theNameSegment && thisID != theID) {theDocs.push([thisName, thisID])}

                                                      and then wrapping the for-clause

                                                      for (var n = 0; n < theIndex.length; n++) {

                                                      in another for-clause that depends on that result and adapting the function "duplicateLayer" to use the identifier instead of the name to identify the target document.

                                                       

                                                      Edit: To identify the document by its ID instead of its name change the line

                                                              ref4.putName( idDcmn, theDoc );

                                                      to

                                                              ref4.putIdentifier( idDcmn, theDoc );

                                                      and use the ID as the second argument.

                                                      The Array "theDocs" in the above code would be an Array of Arrays with the name the first and the ID the second element.

                                                      • 24. Re: Script to copy Layer/Group to another Open Document?
                                                        c.pfaffenbichler Level 9

                                                        Maybe this can help illustrate, instead of the alert you could insert the pertinent code from the other Script:

                                                        // 2015, use it at your own risk;  
                                                        #target "photoshop-70.032"
                                                        var AllDocs = app.documents;   
                                                        if (AllDocs.length > 1) {
                                                        var theIndex = getSelectedLayersIdx();  
                                                        var myDocument = app.activeDocument;
                                                        // get active document’s id;
                                                        var ref = new ActionReference();
                                                        ref.putProperty(stringIDToTypeID ("property"),stringIDToTypeID("documentID") );
                                                        ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
                                                        var docDesc = executeActionGet(ref);
                                                        var theID = docDesc.getInteger(stringIDToTypeID("documentID"));
                                                        // get name;
                                                        var theNameSegment = myDocument.name.split(".")[1];
                                                        var theDocs = new Array;
                                                        var openDocs = app.documents;
                                                        // work through open documents;
                                                        for (var m = 1; m <= openDocs.length; m++) {
                                                        var ref = new ActionReference();
                                                        ref.putIndex( charIDToTypeID("Dcmn"), m); 
                                                        var docDesc = executeActionGet(ref);
                                                        var thisID = docDesc.getInteger(stringIDToTypeID("documentID"));
                                                        var thisName = docDesc.getString(stringIDToTypeID("title"));
                                                        var thisNameSegment = thisName.split(".")[1];
                                                        // collect names and ids;
                                                        if (thisNameSegment == theNameSegment && thisID != theID) {theDocs.push([thisName, thisID])}
                                                        };
                                                        // the documents that meet the requirement;  
                                                        for (var x = 0; x < theDocs.length; x++) {  
                                                        alert ("the document »"+theDocs[x][0]+"« has the identifier "+ theDocs[x][1])
                                                        };
                                                        } else {alert ("No other documents open")};  
                                                        ////// duplicate layer //////  
                                                        function duplicateLayer (theIndex, theDoc) {  
                                                        // =======================================================  
                                                        var idDplc = charIDToTypeID( "Dplc" );  
                                                            var desc7 = new ActionDescriptor();  
                                                            var idnull = charIDToTypeID( "null" );  
                                                                var ref3 = new ActionReference();  
                                                            ref3.putIndex(charIDToTypeID("Lyr "), theIndex);  
                                                            desc7.putReference( idnull, ref3 );  
                                                            var idT = charIDToTypeID( "T   " );  
                                                                var ref4 = new ActionReference();  
                                                                var idDcmn = charIDToTypeID( "Dcmn" );
                                                        // identify by id;
                                                                ref4.putIdentifier( idDcmn, theDoc );
                                                        //        ref4.putName( idDcmn, theDoc );  
                                                            desc7.putReference( idT, ref4 );  
                                                            var idVrsn = charIDToTypeID( "Vrsn" );  
                                                            desc7.putInteger( idVrsn, 5 );  
                                                        executeAction( idDplc, desc7, DialogModes.NO );  
                                                        };  
                                                        ////// by paul mr;  
                                                        function getSelectedLayersIdx(){  
                                                              var selectedLayers = new Array;  
                                                              var ref = new ActionReference();  
                                                              ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
                                                              var desc = executeActionGet(ref);  
                                                              if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){  
                                                                desc = desc.getList( stringIDToTypeID( 'targetLayers' ));  
                                                                  var c = desc.count  
                                                                  var selectedLayers = new Array();  
                                                                  for(var i=0;i<c;i++){  
                                                                    try{  
                                                                      activeDocument.backgroundLayer;  
                                                                      selectedLayers.push(  desc.getReference( i ).getIndex() );  
                                                                    }catch(e){  
                                                                      selectedLayers.push(  desc.getReference( i ).getIndex()+1 );  
                                                                    }  
                                                                  }  
                                                              }else{  
                                                                var ref = new ActionReference();  
                                                                ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));  
                                                                ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );  
                                                                try{  
                                                                    activeDocument.backgroundLayer;  
                                                                    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);  
                                                                }catch(e){  
                                                                    selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));  
                                                                }  
                                                              }  
                                                              return selectedLayers;  
                                                        };
                                                        
                                                        • 25. Re: Script to copy Layer/Group to another Open Document?
                                                          c.pfaffenbichler Level 9

                                                          Also it might be better to duplicate the Layers in one go instead of individually.

                                                          ////// duplicate layers //////
                                                          function duplicateLayers (theDocID) {
                                                          // =======================================================
                                                          var idDplc = charIDToTypeID( "Dplc" );
                                                              var desc8 = new ActionDescriptor();
                                                              var idnull = charIDToTypeID( "null" );
                                                                  var ref5 = new ActionReference();
                                                                  var idLyr = charIDToTypeID( "Lyr " );
                                                                  var idOrdn = charIDToTypeID( "Ordn" );
                                                                  var idTrgt = charIDToTypeID( "Trgt" );
                                                                  ref5.putEnumerated( idLyr, idOrdn, idTrgt );
                                                              desc8.putReference( idnull, ref5 );
                                                              var idT = charIDToTypeID( "T  " );
                                                                  var ref6 = new ActionReference();
                                                                  var idDcmn = charIDToTypeID( "Dcmn" );
                                                          //        ref6.putName( idDcmn, "Untitled-1" );
                                                                  ref6.putIdentifier( idDcmn, theDocID );
                                                              desc8.putReference( idT, ref6 );
                                                              var idVrsn = charIDToTypeID( "Vrsn" );
                                                              desc8.putInteger( idVrsn, 5 );
                                                          executeAction( idDplc, desc8, DialogModes.NO );
                                                          };
                                                          
                                                          • 26. Re: Script to copy Layer/Group to another Open Document?
                                                            ramud57123156 Level 1

                                                            Sorry for the late reply

                                                            Script getting executed without any error identifying the open documents with name but selected layers and groups are not getting copied to those identified documents.

                                                            • 27. Re: Script to copy Layer/Group to another Open Document?
                                                              c.pfaffenbichler Level 9

                                                              Have you made the necessary amendments and what is the Script you use now?

                                                              • 28. Re: Script to copy Layer/Group to another Open Document?
                                                                ramud57123156 Level 1

                                                                Since not getting any error alert i have not made any amendments.

                                                                I have tried above two scripts and both are giving same result as i mentioned in previous reply.

                                                                • 29. Re: Script to copy Layer/Group to another Open Document?
                                                                  c.pfaffenbichler Level 9

                                                                  You seem to have misunderstood my previous posts:

                                                                  None of the last two posted Scripts is a complete version but only provide elements that you have to combine to achieve the desired functionality.

                                                                  I thought the statement

                                                                  instead of the alert you could insert the pertinent code from the other Script:

                                                                  should have made that clear.