exportadobe-illustrator

How do I export all open files into PNGs and have every window close in the end?


Last year, I had help with making an Illustrator script that hides the text layer of all open files and saves every one, but I am wondering how to add code that exports all the files into PNGs (to the size of the artboard) and close all the open windows in the end.

I have been doing this just by using a Batch PNG export action in Illustrator and then manually closing everything, but I figured it would be more efficient to have it all included in one script. Thank you anyone in advance for helping out!

for(var i=0;i<app.documents.length;i++)
{
        var oDoc=app.documents[i];
        for(var j=0;j<oDoc.layers.length; j++)
            {
                if(oDoc.layers[j].name=="text")
                {
                    oDoc.layers[j].visible=false;
                 }
                 if(oDoc.layers[j].name=="text2")
                {
                    oDoc.layers[j].visible=false;
                 }
           }

}

#target illustrator

function test(){

 

  for(var i=0; i<app.documents.length; i++){

    app.documents[i].activate();

    app.activeDocument.save();

  };

 

};

test();

I have been looking for a solution but I haven't seen anything.


Solution

  • I merged the script that coolshrimp suggested with another script that applied the script to an entire directory and it worked smoothly! This is the script I merged it with -> Running a script on all .ai files in a directory

        #target illustrator
    
    
    
    // Function to unlock all locked layers in the document
    function unlockAllLayers(doc) {
        function unlockLayers(layer) {
            layer.locked = false;
            for (var i = 0; i < layer.layers.length; i++) {
                unlockLayers(layer.layers[i]);
            }
        }
    
        for (var j = 0; j < doc.layers.length; j++) {
            unlockLayers(doc.layers[j]);
        }
    }
    
    // Function to remove all layers containing text items
    function removeTextLayers(doc) {
        for (var j = doc.layers.length - 1; j >= 0; j--) {
            var layer = doc.layers[j];
            var textItems = layer.textFrames;
            
            if (textItems.length > 0) {
                layer.remove();
            }
        }
    }
    
    // Function to export the active document as a PNG with the same name
    function exportPNG(doc) {
        var pngOptions = new ExportOptionsPNG24();
        pngOptions.artBoardClipping = true;
        pngOptions.antiAliasing = true;
        pngOptions.transparency = true;
    
        var file = new File(doc.path + "/" + doc.name.replace(/\.[^\.]*$/, '') + ".png");
        doc.exportFile(file, ExportType.PNG24, pngOptions);
    }
    
    function main() {
    
        var folder = Folder.selectDialog('Please, select the folder');
        if (!(folder instanceof Folder)) return;
    
        var files = folder.getFiles('*.ai');
        if (files.length == 0) {
            alert('Folder doesn\'t content AI files');
            return;
        }
    
        var i = files.length;
        while (i--) {
            var doc = app.open(files[i]);
            unlockAllLayers(doc); // Add this line to unlock all layers
            removeTextLayers(doc);
            exportPNG(doc);
            doc.close(SaveOptions.DONOTSAVECHANGES);
        }
    
        alert(files.length + ' files were processed');
    }
    
    main();
    

    I also made an alternate version using the script I was given last year that hides text layers with specific layer names. This replaces the part that starts with "// Function to remove all layers containing text items"

    // Function to remove all layers containing text items
    function removeTextLayers(doc) {
        for (var j = doc.layers.length - 1; j >= 0; j--) {
            var layer = doc.layers[j];
            var textItems = layer.textFrames;
            
                {
                    if(doc.layers[j].name=="text1")
                    {
                        doc.layers[j].visible=false;
                     }
                     if(doc.layers[j].name=="text2")
                    {
                        doc.layers[j].visible=false;
                     }
                     if(doc.layers[j].name=="text3")
                    {
                        doc.layers[j].visible=false;
                     }
               }
        }
    }