Search code examples
javascriptsvgbatch-processingsymbolsadobe-illustrator

Batch Export Symbols to SVG - Illustrator


Question:

I've created a script that iterates through all symbols in an Illustrator document and exports them as PNGs.

I need it to work for SVGs as well, however, it's not as simple as just changing the file-type.

Because of Illustrator's default behaviour of making a recently saved svg the app.activeDocument, the for loop nests new directories for each symbol.

e.g.

exports/
  symbol01.svg
  exports/
    symbol02.svg
      exports/
        symbol03.svg
        etc..

I'm pretty sure the problem lays between //create directory and //choose directory, but I can't for the life of me figure it out.

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

if (symbolCount >= 1) {

   if (confirm("Are all your layers hidden?")) {

      // create temp layer
      doc.layers.add();

      for (var i = 0; i < doc.symbols.length; i++) {

         // place a symbol instance - temp
         var s = doc.symbolItems.add(doc.symbols[i]);

         // create directory 
         var dest = new Folder(doc.path + "/exports"); 
         if (!dest.exists) dest.create();

         // choose directory
         dest.changePath(doc.symbols[i].name);

         // export symbols
         saveSVG(dest);

         // delete temp symbol instance
         s.remove();
      }
      // remove temp layer
      doc.layers[0].remove();
   }

   function saveSVG(file) {
      // save options
      var type = ExportType.SVG;
      var options = new ExportOptionsSVG();

      // export
      doc.exportFile(file, type, options);
   }

} else {

   alert("You don't have any symbols in this document");

}

Having a way to store the initial app.activeDocument would probably fix the issue, but I can't figure out how to do that.. if that's even the best way?

Bonus:

Another problem with this script is the artboard doesn't resize to the symbols, so I found a function fitArtboardToSelectedArt() which I've tried implementing with no success.. can anyone can explain how it should work?

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


Solution

  • Figured it out :)

    Feel free to use the following script if you need to do the same.

    /*
     * Export Symbols as SVGs - 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();
    
                // create temp artboard
                doc.artboards.add(doc.artboards[0].artboardRect);
    
                // get temp artboard
                var tempAB = doc.artboards.getActiveArtboardIndex();
    
                // 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]);
    
                    // resize artboard
                    doc.artboards[tempAB].artboardRect = doc.visibleBounds;
                    app.redraw();
    
                    // choose directory
                    var filename = doc.symbols[i].name;
    
                    // export symbols
                    saveSVG(dest, filename);
    
                    // delete temp symbol instance
                    symbol.remove();
                }
                // remove temp layer
                doc.layers[0].remove();
    
                // remove temp artboard
                doc.artboards[tempAB].remove();
            }
        }
    
        function saveSVG(dest, filename) {
            // save options
            var type = ExportType.SVG;
            var options = new ExportOptionsSVG();
    
            // file
            var file = new File(dest + "/" + filename);
    
            // export
            doc.exportFile(file, type, options);
        }
    
    } else {
        alert("You don't have any symbols in this document");
    }