Need help with an Adobe Illustrator script
Hello,
I am currently working on a project in Adobe Illustrator and I need help writing a script.
I would like to write a script that fills the closed shapes of a layer with the color of an image located on another parent layer named "Layer 2". However, I would like the content of the parent layer "Layer 2", even if it contains closed shapes, not to be affected by the script.
I started writing the script myself, but I encountered several errors that I cannot solve. Therefore, I am seeking help from the Adobe community to help me correct my script or write a new one.
Here is the script I have written so far:
var doc = app.activeDocument;
// Find the parent layer named "Layer 2"
var parentLayer = null;
for (var i = 0; i < doc.layers.length; i++) {
var layer = doc.layers[i];
if (layer.name == "Layer 2" && layer.parent == doc) {
parentLayer = layer;
break;
}
}
if (!parentLayer) {
alert("Parent layer 'Layer 2' not found");
} else {
var selectedItems = [];
// Loop through all page items
for (var i = 0; i < doc.pageItems.length; i++) {
var item = doc.pageItems[i];
// Check if the item is a closed path and not a compound path
if (item.typename == "PathItem" && item.pathPoints.length > 2 && !item.pathItems) {
// Check if the item is not on the parent layer
if (item.layer.parent != parentLayer) {
selectedItems.push(item);
}
}
}
// Get the color of the parent layer
var parentLayerColor = parentLayer.colorSamplingProperties.color.value;
// Loop through all selected closed paths and apply the background color
for (var i = 0; i < selectedItems.length; i++) {
var pathItem = selectedItems[i];
pathItem.filled = true;
pathItem.fillColor = parentLayerColor;
}
}
If you have any suggestions or solutions to solve the errors I am encountering, or if you can write a new script that meets my needs, I would be very grateful.
Thank you in advance for your help.
Best regards, Nico G
