Search code examples
javascriptpngbatch-processingsymbolsadobe-illustrator

Batch Export Symbols to PNG - Illustrator


I was originally going to post this at https://graphicdesign.stackexchange.com/, but since it's a code question it seems more appropriate here.

I want to export all the symbols in an open Illustrator document to seperate pngs.

The following javascript (.jsx) script is really close, but the pngs being exported are empty, which tells me there's still something substantial missing.

What could the missing element be?

var doc = app.activeDocument;  
var symbolCount = doc.symbols.length;  

$.writeln(symbolCount + " symbols"); // log symbol count

for(var i = 0; i < doc.symbols.length; i++) {
    $.writeln(doc.symbols[i].name); // log symbol names

   var dir = doc.path; // save to document's folder
   dir.changePath(doc.symbols[i].name + '.png');
   savePNG(dir);
}

// Save PNG file

function savePNG(file) {
   // export SAVE-FOR-WEB options
   var exp = new ExportOptionsPNG24();
   exp.transparency = true;

   // export as SAVE-FOR-WEB
   doc.exportFile(file, ExportType.PNG24, exp);
}

PS. Here's a link for the Illustrator scripting documentation: http://adobe.ly/1JxLlUK


Solution

  • For anybody else needing this, here's what I turned the script into:

    Thanks to Jongware for the help.

    /*
     * Export Symbols as PNGs - Illustrator
     * --------------------------------------
     * Created By Shane Parsons - 30PT Design Inc.
     * http://30ptdesign.com/
     */
    
    var doc = app.activeDocument;
    var symbolCount = doc.symbols.length;
    
    if (symbolCount >= 1) {
    
        if (confirm("Are all your layers hidden?")) {
    
            // choose directory
            var dest = Folder(doc.path).selectDlg();
    
            // folder chosen
            if (dest) {
    
                // create temp layer
                doc.layers.add();
    
                // loop through symbols
                for (var i = 0; i < doc.symbols.length; i++) {
    
                    // place a symbol instance - temp
                    var symbol = doc.symbolItems.add(doc.symbols[i]);
    
                    // assign name
                    var filename = (doc.symbols[i].name)
    
                    // export symbols
                    savePNG(dest, filename);
    
                    // delete temp symbol instance
                    symbol.remove();
                }
                // remove temp layer
                doc.layers[0].remove();
            }
        }
    
        function savePNG(dest, filename) {
            // save options
            var type = ExportType.PNG24;
            var options = new ExportOptionsPNG24();
            options.transparency = true;
    
            // file
            var file = new File(dest + "/" + filename);
    
            // export
            doc.exportFile(file, type, options);
        }
    
    } else {
        alert("You don't have any symbols in this document");
    }