0 Replies Latest reply on Apr 5, 2013 6:07 AM by reshe

    Exporting layers to SVG files

    reshe

      I am trying to modify a script that exports layers to SVG files. Can someone help me, I don't know how to dublicate (or paste in place) the layers content to the new file that I am creating.

       

      /*

      * Layers to SVG - layers_export.jsx

      * @version 0.1

      * Improved PageItem selection, which fixed centering

      *

      * @author Anton Ball

      * Exports all layers to SVG Files

      * I didn't want every layer in the SVG file so it first creates a new document

      * and one by one copies each layer to that new document while exporting it out

      * as an SVG.

      *

      * TODO:

      * 1. More of an interface wouldn't hurt. Prefix option and progress bar of some description.

      *

      */

       

       

      // Variables

      var docRef = app.activeDocument,

                docPath = docRef.path,

          artBoardSize = docRef.artboards[0].artboardRect, // get Rectangle

                ignoreHidden = true,

                svgExportOptions = (function(){

                          var options = new ExportOptionsSVG();

                          options.fontSubsetting = SVGFontSubsetting.GLYPHSUSED;

                          options.embedRasterImages = false;

                          options.fontType = SVGFontType.OUTLINEFONT;

              options.artBoardClipping = true;

                          return options;

                }()),

                destination, holderDoc;

          alert(artBoardSize);

       

       

      // Functions

      var stepThroughAndExportLayers = function(layers){

                var layer,

                          numLayers = layers.length;

       

       

                for (var i = 0; i < numLayers; i += 1){

                          layer = layers[i];

                          if (ignoreHidden && !layer.visible){continue;}

                          copyLayerTo(layer, holderDoc);

                          selectAll(holderDoc);

                          exportAsSVG(validateLayerName(layer.name, '-'), holderDoc);

                          // Remove everything

                          holderDoc.activeLayer.pageItems.removeAll();

                }

       

       

                holderDoc.close(SaveOptions.DONOTSAVECHANGES);

      },

      // Copies the layer to the doc

      copyLayerTo = function(layer, doc){

                var pageItem,

                          numPageItems = layer.pageItems.length;

                for (var i = 0; i < numPageItems; i++){

                          pageItem = layer.pageItems[i];

                          pageItem.duplicate(holderDoc.activeLayer, ElementPlacement.PLACEATEND);

                }

      },

      // Selects all PageItems in the doc

      selectAll = function(doc){

                var pageItems = doc.pageItems,

                          numPageItems = doc.pageItems.length;

                for (var i = 0; i < numPageItems; i += 1){

                          pageItems[i].selected = true;

                }

      },

      // Exports the doc to the destination saving it as name

      exportAsSVG = function(name, doc){

                var file = new File(destination + '/' + name + '.svg');

                holderDoc.exportFile(file, ExportType.SVG, svgExportOptions);

      },

      // Makes sure the name is lowercase and no spaces

      validateLayerName = function(value, separator){

                separator = separator || '_';

                return value.toLowerCase().replace(/\s/, separator);

      };

       

       

      // Init

      (function(){

                destination = Folder.selectDialog('Select folder for SVG files.', docPath);

                if (!destination){return;}

                holderDoc = app.documents.add(DocumentColorSpace.RGB);

          holderDoc.artboards[0].artboardRect = artBoardSize;

                stepThroughAndExportLayers(docRef.layers);

      }());