Illustrator script loop through sub-layers adjusting visibility to export each layer with artboard a
I have a layer in Illustrator that contains 48 sub-layers. Very simply, I want to loop the sub-layers, make each visible, export it as a PNG, then make it hidden, then move on. It works great for about half of the layers and I can't find any explanation why. Some of the sub-layers have a bunch of paths, some have a <Group> with paths, some have a named group with paths (i.e. 'logo') and some have two named groups, one hidden one visible. In any case - some that appear to have the same structre export as others that export a blank image.
The visibility error report comes up empty and the visibility report, below... suggests every layer is shown then hidden correctly....
I wish I could just export for screens and assets panel, but you can't do that using the artboard and I need all images the same dimensions, so i'm really stuck.
edgewater_combo - blank
edgewater_logo - exported
everglades - exported
feadship - blank
ferretti_logo - exported
ferretti - blank

var doc = app.activeDocument;
// Specify the target layer name
var targetLayerName = "FACE3W"; // Replace with your target layer name
// Specify the prefix for the filenames
var prefix = "FLAT_FACE3W_"; // Replace with your desired prefix
// Function to pause execution for a given time in milliseconds
function sleep(ms) {
var start = new Date().getTime();
while (new Date().getTime() < start + ms);
}
// Find the target layer
var targetLayer = null;
for (var i = 0; i < doc.layers.length; i++) {
if (doc.layers[i].name === targetLayerName) {
targetLayer = doc.layers[i];
break;
}
}
if (targetLayer) {
alert("Target layer found: " + targetLayer.name);
// Collect all items within the target layer
var subLayers = [];
var items = targetLayer.pageItems;
var layerItems = targetLayer.layers;
// Add sub-layers
for (var i = 0; i < layerItems.length; i++) {
subLayers.push(layerItems[i]);
}
// Add group items
for (var j = 0; j < items.length; j++) {
if (items[j].typename === "GroupItem") {
subLayers.push(items[j]);
}
}
alert("Total sub-layers found: " + subLayers.length);
// Initialize report strings
var visibilityReport = "";
var visibilityErrorReport = "";
// Get the count and names of sub-layers
var subLayerCount = subLayers.length;
var subLayerNames = "";
for (var i = 0; i < subLayers.length; i++) {
subLayers[i].visible = true; // Make the sub-layer visible
sleep(100); // Pause to ensure the change takes effect
if (subLayers[i].visible) {
visibilityReport += subLayers[i].name + " is now visible\n";
} else {
visibilityErrorReport += subLayers[i].name + " could not be made visible\n";
}
subLayerNames += subLayers[i].name + " (" + subLayers[i].typename + ")\n";
// Export the file
var file = new File(doc.path + '/' + prefix + subLayers[i].name + '.png');
var exportOptions = new ExportOptionsPNG24();
exportOptions.antiAliasing = true;
exportOptions.transparency = true;
exportOptions.artBoardClipping = true;
doc.exportFile(file, ExportType.PNG24, exportOptions);
subLayers[i].visible = false; // Hide the sub-layer again
sleep(100); // Pause to ensure the change takes effect
if (!subLayers[i].visible) {
visibilityReport += subLayers[i].name + " is now hidden again\n";
} else {
visibilityErrorReport += subLayers[i].name + " could not be hidden again\n";
}
}
// Compile the final report
var finalReport = "Total sub-layers found: " + subLayerCount + "\n\nNames of sub-layers:\n" + subLayerNames;
finalReport += "\n\nVisibility Report:\n" + visibilityReport;
finalReport += "\n\nVisibility Error Report:\n" + visibilityErrorReport;
// Display the final report
alert(finalReport);
} else {
alert("Layer '" + targetLayerName + "' not found.");
}

