Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SebCorbin
Created April 7, 2015 14:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SebCorbin/4af974231068ed4ab457 to your computer and use it in GitHub Desktop.
Save SebCorbin/4af974231068ed4ab457 to your computer and use it in GitHub Desktop.
Illustrator script - Save selection as SVG
/*
* Export selection to SVG - export_selection_as_SVG
* (Adapted from Layers to SVG 0.1 - export_selection_as_SVG.jsx, by Rhys van der Waerden)
*
* @author SebCorbin
*/
// Variables
var ignoreHidden = true,
svgExportOptions = (function () {
var options = new ExportOptionsSVG();
options.fontSubsetting = SVGFontSubsetting.GLYPHSUSED;
options.embedRasterImages = false;
options.fontType = SVGFontType.OUTLINEFONT;
return options;
}());
// Copy selection to a new document, and save it as an SVG file.
var saveSelection = function (file) {
var objects = app.activeDocument.selection;
if (objects.length > 0) {
// Create temporary document and copy all selected objects.
var doc = app.documents.add(DocumentColorSpace.RGB);
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
copyObjectsTo(objects, doc);
// Resize the artboard to the object
selectAll(doc);
//doc.fitArtboardToSelectedArt(0); // Doesn't work
doc.artboards[0].artboardRect = doc.visibleBounds;
doc.exportFile(file, ExportType.SVG, svgExportOptions);
// Remove everything
doc.activeLayer.pageItems.removeAll();
doc.close(SaveOptions.DONOTSAVECHANGES);
}
};
// Duplicate objects and add them to a document.
var copyObjectsTo = function (objects, destinationDocument) {
for (var i = 0; i < objects.length; i++) {
objects[i].duplicate(destinationDocument.activeLayer, ElementPlacement.PLACEATBEGINNING);
}
};
// Selects all PageItems in the doc
var selectAll = function (doc) {
var pageItems = doc.pageItems,
numPageItems = doc.pageItems.length;
for (var i = 0; i < numPageItems; i += 1) {
pageItems[i].selected = true;
}
};
// Init
(function () {
var file = File.saveDialog('Add SVG file', 'SVG:*.svg');
if (file) {
saveSelection(file);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment