Skip to main content
Participating Frequently
March 23, 2015
Answered

script to copy color from object below

  • March 23, 2015
  • 2 replies
  • 2569 views

Hello,

I was wondering if there is a script to copy the fill color from an object below.

I need to transfer the fill colors from 300 different squares to 300 circles, how can I do this quickly?

Thanks

Pisistrato

This topic has been closed for replies.
Correct answer Silly-V

What if I overlay the squares to the circles? Would it work?


Updated:

#target illustrator

function test(){

    function isSquare(item){

        if(item.pathPoints.length > 4){

            return false;

        }

        for(var i=0; i<item.pathPoints.length; i++){

            var thisPoint = item.pathPoints;

            if(Math.floor(thisPoint.leftDirection[0]) !=  Math.floor(thisPoint.rightDirection[0]) ||

                Math.floor(thisPoint.leftDirection[1]) !=  Math.floor(thisPoint.rightDirection[1]) ||

                Math.floor(thisPoint.leftDirection[0]) !=  Math.floor(thisPoint.anchor[0]) ||

                Math.floor(thisPoint.leftDirection[1]) !=  Math.floor(thisPoint.anchor[1])){

                return false;

            }

        }

        return true;

    }

    if(app.documents.length > 0){

        var doc = app.activeDocument;

        for(var i=0; i<doc.pathItems.length; i++){

            var thisPath = doc.pathItems;

            if(isSquare(thisPath)){

                var v = thisPath.geometricBounds;

                for(var j=0; j<doc.pathItems.length; j++){

                    var thisInnerPath = doc.pathItems;

                    if(thisInnerPath != thisPath){

                        var v1 = thisInnerPath.geometricBounds;

                        if(v1[0] >= v[0] && v1[2] <= v[2] && v1[1] <= v[1] && v1[3] >= v[3]){

                            thisInnerPath.fillColor = thisPath.fillColor;

                        }

                    }

                }

            }

        }

    }

}

test();

2 replies

Larry G. Schneider
Inspiring
March 23, 2015

Have you considered just using the Effect>Convert to Shape on the rectangles?

Inspiring
March 23, 2015

pisistrato wrote:  I need to transfer the fill colors from 300 different squares to 300 circles

Hi, welcome to the forum. Can you post a screen capture of your squares and circles, please?

Participating Frequently
March 24, 2015

Hi, Thank you.

As you can see I want to transfer the fill color from the squares to the circles (ellipses). The circles/ellipses are all different in shape, so unfortunately I cannot convert the squares using Effect>Convert to Shape.

I was thinking that there might be a script or another way to get the fill color from an abject below (or above) that is present in the same space...so I can just align all the squares to the ellipses, move them backwards, extracts the fill colors and remove the squares at the end.

Silly-V
Braniac
March 25, 2015

Hello,

Thank you for the script, however I cannot get it working...


Sorry, my snippet only works with items which are directly above or below the square.

AND, the circles or ovals need to be smaller than the squares, too.  (Change the .visibleBounds with .geometricBounds in the snippet to exclude stroke width from circle's width measure)

See my screenshot for a working scene.