this is a simple doodle in Illustrator using javascript that has an example of creating functions, coloring shapes with a fill color, and exporting layers to png. i'm still new to javascript so this example doesn't have a ui yet. there may be bugs or better ways to implement so please use modify at your own risk.
//a simple tool to color all shapes in illustrator document and then export each layer to png //utility methods function getLayersVisibilityDict(doc) { /* Args: doc (document object) Returns: (dictionary): keys int indexes values bool for whether layer is visible */ var result = {}; for(var i=0; i<doc.layers.length; i++) { result[i] = doc.layers[i].visible; } return result; } function setLayersVisibilityByDict(doc, data) { /* Args: doc (document object) data (dictionary): keys are layer index value is visiblity to set to */ for(var i=0; i<doc.layers.length; i++) { doc.layers[i].visible = data[i]; } } //end utility methods //color all shapes function colorAllShapes(r, g, b) { /* Args: r (int) red rgb value g (int) green rgb value b (int) blue rgb value */ //requires setting all layers visible first. alert("coloring all shapes"); //get reference to active document var doc = app.activeDocument; layerVisibilityDict = getLayersVisibilityDict(doc); //starting layer visiblity //make all layers visible for(var i=0; i<doc.layers.length; i++) { doc.layers[i].visible = true; } //loop all paths for(var i=0; i<doc.pathItems.length; i++) { shape = doc.pathItems[i]; color = new RGBColor(); color.red = r; color.green = g; color.blue = b; //fill shape with color shape.fillColor = color; //ensure shape has fill shape.filled = true; } //restore layer visibility setLayersVisibilityByDict(doc, layerVisibilityDict); } //function that acts as a constructor. for older illustrator that doesnt have class available function LayerExporter(){ /*responsible for exporting illustrator layers to individual png files */ this.doc = app.activeDocument; //export each layer to individual png files this.doIt = function() { var doc = this.doc; if(!doc) { alert("no active document"); return; } //todo get export folder from artist //var exportDir = "/Users/Nathaniel/Desktop/learning_/illustrator_dev_testing/coloringShapes"; var exportFolder = Folder.selectDialog("Select a folder to export layers to"); //new Folder(exportDir); if(!exportFolder) { alert("no export folder"); return; } //export options var exportOptions = new ExportOptionsPNG24(); exportOptions.antiAliasing = true; //enable anti aliasing exportOptions.transparency = true; //preserve transparency exportOptions.artBoardClipping = true; //if false it might export stuff outside art clip board exportOptions.horizontalScale = 100; //100 pct scale exportOptions.verticalScale = 100; //100 pct scale //store starting layer visibility const layersVisiblityDict = getLayersVisibilityDict(this.doc); //iterate through each layer for(var i=0; i<doc.layers.length; i++) { var currentLayer = doc.layers[i]; //hide all other layers this.hideAllLayersExceptIndex(i); //construct file name var exportFileName = currentLayer.name+".png"; var exportFileObj = new File(exportFolder.fsName + "/" + exportFileName); //export current visible layer doc.exportFile(exportFileObj, ExportType.PNG24, exportOptions); } //restore visibility of layers setLayersVisibilityByDict(this.doc, layersVisiblityDict); } this.hideAllLayersExceptIndex = function(indexShow) { /* Args: indexShow (int): the index to make visible */ var doc = this.doc; for(var i=0; i<doc.layers.length; i++) { doc.layers[i].visible = (i == indexShow); //google searched tip to shorten code. it gives false most of time except when counter is index } } } function doIt() { //hard coded color colorAllShapes(r=0, g=0, b=255); //exporting layers var layerExporter = new LayerExporter(); layerExporter.doIt(); } doIt();
Thanks for looking