Part- 3 Illustrator Scripting Looping the shape within layer

Illustrator is a powerful design tool that is widely used by graphic designers and artists around the world. One of the key features of Illustrator is the ability to automate repetitive tasks through scripting. This can save designers a significant amount of time and effort, allowing them to focus on more creative aspects of their work.

In this article, we will explore how to use Illustrator scripting to loop shapes within a layer and change their color. We will walk through two functions that take parameters of the active layer and process individual elements to change their color.

Below is couple of functions that takes parameter of active layer and process individual element to change color.

function iterateOverGroup(groupCoreItem,colorIs){
for (var ai = 0; ai < groupCoreItem.pathItems.length; ai++) {
colorShape(groupCoreItem.pathItems[ai],colorIs);
}
for (var gi = 0; gi <groupCoreItem.groupItems.length; gi++) {
iterateOverGroup(groupCoreItem.groupItems[gi],colorIs);
}
for (var ci = 0; ci <groupCoreItem.compoundPathItems.length; ci++) {
var compoundPath = groupCoreItem.compoundPathItems[ci];
var pathItems = compoundPath.pathItems;
for (j=0; j< pathItems.length; j++) {
var pathItem = pathItems[j];
colorShape(pathItem,colorIs);
}
}
}

function colorShape(shapeItemCore, replaceThisColor){
if(shapeItemCore.filled==true){
shapeItemCore.fillColor = replaceThisColor;
//if(shapeItemCore.fillColor!=white){ FILL it }
}
if(shapeItemCore.stroked==true){
shapeItemCore.strokeColor=replaceThisColor;
//if(shapeItemCore.strokeColor!=white){ FILL it }
}
}

Note: there are three types of elements within layer that are being checked in first function i.e item can be PathItem, GroupItem or CompondPathItem.

The first function, called iterateOverGroup, is responsible for iterating over the elements within a group, which can be a PathItem, GroupItem, or CompondPathItem. The function takes two parameters: groupCoreItem and colorIs. The groupCoreItem parameter represents the active layer, while the colorIs parameter represents the color that the shapes should be changed to.

The iterateOverGroup function contains three nested loops, each of which iterates over a different type of element. The first loop iterates over the pathItems within the groupCoreItem, changing the color of each shape using the colorShape function. The second loop iterates over the groupItems within the groupCoreItem, calling the iterateOverGroup function recursively to process each nested group. The third loop iterates over the compoundPathItems within the groupCoreItem, extracting the individual pathItems and passing them to the colorShape function to change their color.

The second function, called colorShape, is responsible for changing the color of an individual shape. The function takes two parameters: shapeItemCore and replaceThisColor. The shapeItemCore parameter represents the individual shape being processed, while the replaceThisColor parameter represents the color that the shape should be changed to.

The colorShape function first checks if the shape is filled or stroked. If the shape is filled, the function changes the fillColor property to the specified color. If the shape is stroked, the function changes the strokeColor property to the specified color. In both cases, the function also checks if the color is white and fills the shape if it is not.

In conclusion, these two functions demonstrate how to use Illustrator scripting to loop shapes within a layer and change their color. By automating this task, designers can save time and focus on more creative aspects of their work.

FOR ANY EXTENSION / SCRIPT / PLUGIN WORK DON’T HESITATE TO LEAVE ME A MESSAGE OR CONTACT ME at mr.bunt@gmail.com


Posted

in

,

by

Comments

Leave a Reply