Last active
March 5, 2021 13:09
-
-
Save lorenzoromagnoli/2698ef8f5957ee80f9b0e4c835a9862c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This script loops trough the artboard and exports each artboard to svg. | |
The destination folder depends on the artboard width. | |
!!! This script doesn't currently creates the necessary folder so you need to create the folder structure before exporting. | |
The destination will be relative to the illustration location: /export/svg/[icon-width]/[artboard name].svg | |
Artboards with a name starting with - will not be exported | |
*/ | |
var doc = app.activeDocument;;//Gets the active document | |
var numArtboards = doc.artboards.length;//returns the number of artboards in the document | |
var filePath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/'); | |
var fleName=app.activeDocument.name; | |
$.writeln("fleName= ", fleName) | |
$.writeln("numArtboards= ", numArtboards) | |
$.writeln("filePath= ", filePath); | |
var exportOptions = new ExportOptionsSVG(); | |
for (var i = 0; i < numArtboards; i++) { | |
doc.artboards.setActiveArtboardIndex(i); | |
//var abActive = doc.artboards[doc.artboards.getActiveArtboardIndex()] | |
// we are going to export one at a time | |
exportOptions.saveMultipleArtboards = true | |
exportOptions.artboardRange = "" + (i + 1); | |
exportOptions.preserveEditability = false; | |
//exportOptions.cssProperties = SVGCSSPropertyLocation.PRESENTATIONATTRIBUTES; | |
exportOptions.svgFontType = SVGFontType.SVGFONT; | |
//exportOptions.slices=true; | |
var artboardSize = doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect[2] - doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect[0] | |
$.writeln(" artboardSize= ", artboardSize); | |
var artboardName = doc.artboards[doc.artboards.getActiveArtboardIndex()].name; | |
$.writeln("artboardName= ", artboardName); | |
if (artboardName[0] != '-') { | |
saveSvg(doc,filePath+"/exported/svg/"+artboardSize,exportOptions,i,artboardName); | |
} | |
} | |
function saveSvg(doc, filePath, options, artboardIndex, artboardName) { | |
//Create a temporary folder | |
var tmpFolder = Folder(Folder.temp + "/" + artboardName); | |
tmpFolder.create(); | |
//Create a reference to the file. Naming isn't really important as Illustrator will ignore naming | |
//as we asked for saveMultipleArtboards : true | |
var tmpFile = new File(tmpFolder.fsName + "/" + artboardName + ".svg"); | |
$.writeln("tmpFile= ", tmpFile); | |
destFile = new File(filePath + "/" + artboardName + ".svg"); | |
tmpFile.exists && tmpFile.remove(); | |
//Exporting to temp file | |
doc.exportFile(tmpFile, ExportType.SVG, options); | |
//We know the file has been exported to tmpFolder | |
//But we may not be sure of the naming | |
//However there should be only one svg file there | |
//So we presume it's our recently exported svg | |
tmpFile = tmpFolder.getFiles("*.svg"); | |
if (!tmpFile.length) { | |
alert("Couldn't export files sorry"); | |
tmpFolder.remove(); | |
} | |
//Now we copy temp file to final destination with correct naming | |
//And remove temp stuff | |
tmpFile = tmpFile[0]; | |
tmpFile.copy(destFile); | |
tmpFile.remove(); | |
tmpFolder.remove(); | |
svgOptimizer(destFile) | |
} | |
function svgOptimizer(f){ | |
var re = [ | |
/^<\?xml.+?>\s*/, /<!--.+-->\s*/, /version="1\.1"\s*/, | |
/id=".+"\s*/, /style=".+?"\s*/, /xml:space=".+?"\s*/, | |
/xmlns:xlink.+xlink"\s*/, /x=".+?px"\s*/, /y=".+?px"\s*/, | |
/width=".*?px"\s*/, /height=".*?px"\s*/, /<style.+>\s*/, /<\/style>\s*/]; | |
var re1 = /\.(.+?)\{(fill):(#[0-9A-Fa-f]{6});\}/g; | |
var width; | |
var height; | |
var sizeString="" | |
var sty = []; | |
if (f.open("r")){ | |
var svgStr = f.read(); | |
//replace all the regexp in the first array with " | |
for (var i=0;i<re.length;i++){ | |
svgStr = svgStr.replace(re[i], ""); | |
} | |
sty = svgStr.match (re1); | |
//remove all the colors | |
if (sty!=null){ | |
for (i=0;i<sty.length;i++){ | |
sty[i].match(re1); | |
svgStr = svgStr.replace('class="'+RegExp.$1+'"', RegExp.$2+'="'+RegExp.$3+'"'); | |
} | |
for (i=0;i<sty.length;i++){ | |
svgStr = svgStr.replace(re1, ""); | |
} | |
} | |
//remove all the spaces | |
svgStr =svgStr.replace(/[\n\t]+/g, ""); | |
//remove all the clippath | |
svgStr =svgStr.replace(/<clipPath(.*?)<\/clipPath>/, ""); | |
//remove all the use xlink | |
svgStr =svgStr.replace(/<use xlink:(.*?)\/>/, ""); | |
//remove all path which have a opacity defined - | |
//if you use extra layers for guides or padding set the opacity to somethign different than one and those elements won't be exported | |
svgStr =svgStr.replace(/<path style="opacity:0.(.*?)\/>/, ""); | |
//remove all the groups | |
svgStr =svgStr.replace(/<g>/g, ""); | |
svgStr =svgStr.replace(/<g >/g, ""); | |
svgStr =svgStr.replace(/<\/g >/g, ""); | |
svgStr =svgStr.replace(/<\/g>/g, ""); | |
svgStr =svgStr.replace(/<g id=(.*?)">/g, ""); | |
//get viewbox size | |
var viewBoxSizeStringRegexp= /viewBox="(.*?)"/g; | |
var viewBoxSizeStr=viewBoxSizeStringRegexp.exec(svgStr); | |
// just make sure the viewbox actually exist | |
if (viewBoxSizeStr.length>1){ | |
viewBoxSizeStr=viewBoxSizeStr[1] | |
var sizesArray=viewBoxSizeStr.split(" "); | |
width=sizesArray[2] | |
height=sizesArray[3] | |
sizeString="width=\""+width+"\" height=\""+height+"\""; | |
svgStr=svgStr.replace(/<svg/g, "<svg "+sizeString); | |
} | |
f.close(); | |
//$.writeln(svgStr); | |
f.open("w"); | |
f.write(svgStr); | |
f.close(); | |
$.writeln("saved",f ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment