Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Could you please attach an image that shows what kind of result you are looking for? "the color of an image on another parent layer" - but in your code (by ChatGPT?) the "parentLayerColor" variable is looking for the color of the layer outliner, not some image.
If you want to apply Layer 2 outliner color to objects, replace the "var parentLayerColor = parentLayer.colorSamplingProperties.color.value" with "var parentLayerColor = parentLayer.color".
Copy link to clipboard
Copied
Thank you for your quick response. Indeed, I do not have any coding skills, and I have used Chat GPT to assist me.
Here is an example of what I would like as a result:
Ideally, I would like the geometric center of each shape to serve as a reference point for selecting the color behind it using a pipette tool.
Thank you very much for your help!
Copy link to clipboard
Copied
There is no direct way to get color information about raster pixels. To do this, you have to look in the direction of the script created by @Inventsable : https://community.adobe.com/t5/illustrator-discussions/scripting-tools-to-get-the-color-of-a-particu...
Copy link to clipboard
Copied
Thank you for your response. Is it possible to retrieve color information from an image that has been dynamically vectorized? That would be sufficient for me.