Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Copy item style attributes/properties to other item?

Explorer ,
Oct 09, 2014 Oct 09, 2014

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...

TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Oct 09, 2014 Oct 09, 2014

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

...
Translate
Adobe
Valorous Hero ,
Oct 09, 2014 Oct 09, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2014 Oct 10, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 10, 2014 Oct 10, 2014

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 != null) dest = src;

            $.writeln(count + '. ' + i);

        }

        catch ( i ) {

            $.writeln( count + ' ' + iString + '. ' + i )

        }

          

        count++;

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Oct 10, 2014 Oct 10, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2014 Oct 10, 2014
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines