13 Replies Latest reply on Mar 6, 2015 6:37 AM by Jean-Claude Tremblay

    Duplicate object into multiple selected frame

    Jean-Claude Tremblay User Group Manager

      I’m looking for a script that will duplicate the selected object or the content of the clipboard into multiple selected layers inside a document.

      I have not find any discussions around this, but I’m asking if anyone have already seen such script.

       

      Thanks

      Jean-Claude

        • 1. Re: Duplicate object into multiple selected frame
          Jump_Over Level 5

          Hi,

           

          "multiple selected layers" could be a main issue here, cause - as far as I know - script is not able to read UI panels states (except active property).

          But if you may find other condition rule...

           

          Jarek

          • 2. Re: Duplicate object into multiple selected frame
            Kai Rübsamen Adobe Community Professional

            Just a idea: What if the user select the layers not in the normal panel, instead he click on checkboxes in a dialog?

             

            –Kai

            • 3. Re: Duplicate object into multiple selected frame
              Jump_Over Level 5

              Hi Kai,

               

              If so - good condition is found.

              Jarek

              • 4. Re: Duplicate object into multiple selected frame
                Kai Rübsamen Adobe Community Professional

                Hi Jarek,

                 

                what do you mean with good condition?

                 

                I would display a dialog with checkboxes, based on the layers names in the acual document. If the checkboxes are checked, the selected object will be duplicated on these layers.

                 

                We had a similiar goal some weeks ago with checked layers in ai-files in our german forum.

                 

                –Kai

                • 5. Re: Duplicate object into multiple selected frame
                  Jump_Over Level 5

                  Hi Kai,

                   

                  I ment the way code could be based on that input and output data are defined definitely.

                  A dialog with check boxes is a good solution.

                   

                  Jarek

                  • 6. Re: Duplicate object into multiple selected frame
                    Kai Rübsamen Adobe Community Professional

                    O.K. lets wait what Jean-Claude thinks about a dialog and if he wants to duplicate only single objects, multiple objects, groups …

                    • 7. Re: Duplicate object into multiple selected frame
                      Jean-Claude Tremblay User Group Manager

                      My first idea was indeed a dialog with layers names in it. Much quicker than having to turn on/off visibilities of layers, than return to original stay of document.

                      For the objects, it could be a single or multiple object or groups.

                      • 8. Re: Duplicate object into multiple selected frame
                        Laubender Adobe Community Professional & MVP

                        @Jean-Claude – that is all doable, also a "deluxe" version where the dialog is a panel constantly updating the list of layers, if you add and remove, change order or turn on/off visibility.

                         

                        For your basic need, the following snippet in ExtendScript should be sufficient.
                        All selected objects will be duplicated to all layers that are visible and not locked:

                         

                        var mySel = app.selection;
                        var myLayers = app.documents[0].layers.everyItem().getElements();
                        
                        for(var s=0;s<mySel.length;s++){
                        
                            var layerOfSelection = mySel[s].itemLayer;
                           
                            for(var n=0;n<myLayers.length;n++){
                               
                                if(
                                    myLayers[n] !== layerOfSelection
                                   
                                    && myLayers[n].visible === true
                                    && lmyLayers[n].locked === false
                                   
                                    ){
                                    mySel[s].duplicate(myLayers[n]);
                                    };
                                };
                        
                        };
                        

                         

                        Uwe

                        • 9. Re: Duplicate object into multiple selected frame
                          Kai Rübsamen Adobe Community Professional

                          Hi Guys,

                           

                          I rewrite Hans Haeslers script a bit. In a first step it will only work with single selected items and will not check, if the selected layers are locked or hidden:

                           

                          // duplicate on different layers _b01
                          
                          // vorbeugenderweise das Anzeigen von Dialogen aktivieren 
                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; 
                          
                          // prüfen, ob ein Dokument geöffnet ist 
                          if ( app.documents.length == 0 ) { 
                              alert ( "Open a document" ); 
                              exit(); 
                          } 
                          
                          // die Auswahl speichern ... 
                          
                          if ( app.selection.length == 1 ) {
                              var curSel = app.selection[0];
                          }
                          else {
                              alert ( "Warning\rWorks only with one selected item at the moment" );
                              exit();
                          }
                          
                          var curDoc = app.activeDocument;
                          var allLayers = curDoc.layers;
                          var nLayers = allLayers.length;
                          
                          // eventuell vorhandene Dialoge entfernen 
                          try { 
                              app.dialogs.everyItem().destroy(); 
                          } 
                          catch (e) { 
                          } 
                          
                          var layerNames = new Array(); 
                          
                          for ( var i = 0; i < nLayers; i++ ) { 
                              layerNames.push( allLayers[i].name ); 
                          } 
                          
                          // eventuell vorhandene Dialoge entfernen 
                          try { 
                              app.dialogs.everyItem().destroy(); 
                          } 
                          catch (e) { 
                          } 
                          
                          // den Dialog vorbereiten ... 
                          var dlogCheckList = new Array(); 
                          var aDialog = app.dialogs.add({ name: "Duplicate object to selected layers", canCancel: true }); 
                          with ( aDialog ) { 
                              with ( dialogColumns.add() ) { 
                                  with ( dialogRows.add() ) { 
                                      staticTexts.add({ staticLabel: "active = duplicate"} ); 
                                      for ( var n = 0; n < nLayers; n++ ) { 
                                          with (dialogRows.add()) { 
                                              dlogCheckList.push( checkboxControls.add({ staticLabel: layerNames[n] })); 
                                          } 
                                      } 
                                  } 
                              } 
                          } 
                          // ... anzeigen und die Wahl des Anwenders anwenden 
                          if ( aDialog.show() == true ) { 
                              for ( var k = 0; k < dlogCheckList.length; k++ ) {
                                  var curCheckBox = dlogCheckList[k];
                                  if ( curCheckBox.checkedState ) {
                                      var curLayer = curDoc.layers.itemByName( layerNames[k] ); 
                                      curSel.duplicate( curLayer );
                                  }
                              } 
                              aDialog.destroy(); 
                          } 
                          else { 
                              aDialog.destroy(); 
                          } 
                          

                           

                          Kai

                          • 10. Re: Duplicate object into multiple selected frame
                            Jean-Claude Tremblay User Group Manager

                            @Uwe Your snippets fail/stop at if( each time. Thanks!

                             

                            @Kai Wow! This works on a single group object too. Might be enough for my need.

                            • 11. Re: Duplicate object into multiple selected frame
                              Kai Rübsamen Adobe Community Professional

                              Jean-Claude try the following one:

                               

                              1. Should work with a single object, multiple objects, group

                              2. Should alert a warning, if you select nothing or select some text

                              3. Will not honor, if layers are hidden or locked

                               

                              Does it work for you?

                               

                              // duplicate on different layers _b02
                              // by Kai Rübsamen, credits to Hans Haesler
                              
                              // vorbeugenderweise das Anzeigen von Dialogen aktivieren
                              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
                              
                              // prüfen, ob ein Dokument geöffnet ist
                              if ( app.documents.length == 0 ) {
                                  alert ( "Warning\rOpen a document!" );
                                  exit();
                              }
                              
                              // die Auswahl speichern ...
                              var allSel = app.selection;
                              var nItems = allSel.length;
                              
                              // und prüfen
                              if ( !nItems ) {
                                  alert ( "Warning\rSelect something!" );
                                  exit();
                              }
                              
                              if ( nItems == 1 && app.selection[0].hasOwnProperty("baseline") ) {
                                  alert ( "Warning\rDoes not work with text. Select only frames!" );
                                  exit();
                              }
                              
                              var curDoc = app.activeDocument;
                              var allLayers = curDoc.layers;
                              var nLayers = allLayers.length;
                              
                              // eventuell vorhandene Dialoge entfernen
                              try {
                                  app.dialogs.everyItem().destroy();
                              }
                              catch (e) {
                              }
                              
                              var layerNames = new Array();
                              
                              for ( var i = 0; i < nLayers; i++ ) {
                                  layerNames.push( allLayers[i].name );
                              }
                              
                              // eventuell vorhandene Dialoge entfernen
                              try {
                                  app.dialogs.everyItem().destroy();
                              }
                              catch (e) {
                              }
                              
                              // den Dialog vorbereiten ...
                              var dlogCheckList = new Array();
                              var aDialog = app.dialogs.add({ name: "Duplicate object to selected layers", canCancel: true });
                              with ( aDialog ) {
                                  with ( dialogColumns.add() ) {
                                      with ( dialogRows.add() ) {
                                          staticTexts.add({ staticLabel: "active = duplicate"} );
                                          for ( var n = 0; n < nLayers; n++ ) {
                                              with (dialogRows.add()) {
                                                  dlogCheckList.push( checkboxControls.add({ staticLabel: layerNames[n] }));
                                              }
                                          }
                                      }
                                  }
                              }
                              // ... anzeigen und die Wahl des Anwenders anwenden
                              if ( aDialog.show() == true ) {
                                  for ( var k = 0; k < dlogCheckList.length; k++ ) {
                                      var curCheckBox = dlogCheckList[k];
                                      if ( curCheckBox.checkedState ) {
                                          var curLayer = curDoc.layers.itemByName( layerNames[k] );
                                          for ( var i = nItems-1; i >= 0; i-- ) {
                                              var curSel = allSel[i];
                                              curSel.duplicate( curLayer );
                                          }
                                      }
                                  }
                                  aDialog.destroy();
                              }
                              else {
                                  aDialog.destroy();
                              }
                              
                              

                               

                              –Kai

                              • 12. Re: Duplicate object into multiple selected frame
                                Laubender Adobe Community Professional & MVP

                                @Jean-Claude – sorry, a little typo sneaked in.

                                I had an additional character in the the variable name of myLayers inside of the if statement.

                                Here the code again:

                                 

                                var mySel = app.selection; 
                                var myLayers = app.documents[0].layers.everyItem().getElements(); 
                                
                                for(var s=0;s<mySel.length;s++){ 
                                
                                    var layerOfSelection = mySel[s].itemLayer; 
                                   
                                    for(var n=0;n<myLayers.length;n++){ 
                                       
                                        if( 
                                            myLayers[n] !== layerOfSelection 
                                           
                                            && myLayers[n].visible === true 
                                            && myLayers[n].locked === false 
                                           
                                            ){ 
                                            mySel[s].duplicate(myLayers[n]); 
                                            }; 
                                        }; 
                                
                                };
                                

                                 

                                Uwe

                                1 person found this helpful
                                • 13. Re: Duplicate object into multiple selected frame
                                  Jean-Claude Tremblay User Group Manager

                                  Kai & Uwe, both solutions works perfectly. Both are useful depending on specific need and situation.

                                   

                                  Thanks a lot guys for your help and generosity. Hope this can be useful to others too.