5 Replies Latest reply on Oct 10, 2014 7:35 AM by CarlosCanto

    Copy item style attributes/properties to other item?

    Sakari Niittyma

      Just want to ask if someone know if there is short way to copy item properties to other object, than go through every possibilities with script. There is quite many possibilities with strokes and so on, so this could be helpful...

      I mean like with "Eyedropper Tool", but with the script

       

      I can set them manually like below. But just want to know is there already some "shortcut" way to handle this kind object style copying?

       

      var doc = app.activeDocument;
      var selectedItems = doc.selection;
      
      var newLayer = doc.layers.add();
      var ellipse = newLayer.pathItems.ellipse(100, 0, 100.0, 100.0, false, true );
      
      ellipse.fillColor = selectedItems[0].fillColor;
      ellipse.strokeColor = selectedItems[0].strokeColor;
      ellipse.strokeWidth = selectedItems[0].strokeWidth;
      ellipse.strokeDashes = selectedItems[0].strokeDashes;
      ellipse.strokeDashOffset = selectedItems[0].strokeDashOffset;
      // etc etc...
      
        • 1. Re: Copy item style attributes/properties to other item?
          Silly-V Adobe Community Professional

          You can make an object to store attributes you want to copy from some art item and then use that object as data for a function which takes another art item as an argument and applies all appropriate options from the data object.

          My sample code here relies on having 2 path items with names of 'mySource' and 'myDest'  .

          The appStyle object acts as a clipboard to hold the style attributes of the art, and uses the for-in loops to apply and copy them, which means if AI art objects had a 'copyStyle' or 'applyStyle' properties, it would mess you up, then the names would need to change to be unique.

           

           

          #target illustrator
          function test(){
              var doc= app.activeDocument;
              var appStyle = {
                   fillColor: null,
                   strokeColor: null,
                   strokeWidth: null,
                   strokeDashes: null,
                   strokeDashOffset: null,
                   copyStyle: function(sourceArt){
                      var a = sourceArt;
                      for(var all in this){
                          if(a.hasOwnProperty(all)){
                              this[all] = a[all];
                          }
                      }
                   },
                  applyStyle: function(destArt){
                    var b = destArt;
                      for(var all in this){
                          if(b.hasOwnProperty(all) && this[all] != null){
                              b[all] = this[all];
                          }
                      }
                  }
              };
              
              var mySourcePath = doc.pathItems.getByName('mySource');
              var myDestPath = doc.pathItems.getByName('myDest');
              
              appStyle.copyStyle(mySourcePath);
              appStyle.applyStyle(myDestPath);
          }
          
          
          test();
          
          • 2. Re: Copy item style attributes/properties to other item?
            Sakari Niittyma Level 1

            Thanks Silly-V! I was already thinking something like to use reference object as data storage for properties. Your example is much more better than I have tried. Didn't know "hasOwnProperty" function , so this is great time saver! Thanks again, you saved my day again

            • 3. Re: Copy item style attributes/properties to other item?
              Sakari Niittyma Level 1

              I was testing some more about this topic. Just want to share it with you guys.

              So I loop every property of item1 to item2 with this simple function. I'll catch errors if there is with some of properties and also console out all properties that we have tested.

              It doesn't copy all like text bolding, alignment... These need to set different way I guess.

               

              // COPY: Styles between objects
              copyStyleAll( docActive.selection[0], docActive.selection[1] );
              
              function copyStyleAll( src, dest ) {
                 
                  var count = 0;
                 
                  for ( var i in src ) {
                     
                      var iString = String(i);
              
                      try {
                          if (dest.hasOwnProperty(i) && src[i] != null) dest[i] = src[i];
                          $.writeln(count + '. ' + i);
                      }
                      catch ( i ) {
                          $.writeln( count + ' ' + iString + '. ' + i )
                      }
                         
                      count++;
                  }
              }
              
              • 4. Re: Copy item style attributes/properties to other item?
                Silly-V Adobe Community Professional

                There will be some style properties which are on an art object which are controlled by yet another property of that art object- for example, a text frame art object has a textRange which has characterAttributes which have properties such as strokeWeight (or something to that effect) and 'size' which is the size of the font used.  So what this amounts to is that you have to treat different art types with each-own ways.

                • 5. Re: Copy item style attributes/properties to other item?
                  CarlosCanto Adobe Community Professional & MVP

                  I was thinking of a cheap workaround...

                   

                  - Make an action to create a Graphic Style out of the source object

                  - Apply Graphic Style to destination object

                   

                  ...didn't have a chance to test