Skip to content

Instantly share code, notes, and snippets.

@iconifyit
Created January 20, 2020 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iconifyit/32f05ad2bc4564478c3e05859b65cc42 to your computer and use it in GitHub Desktop.
Save iconifyit/32f05ad2bc4564478c3e05859b65cc42 to your computer and use it in GitHub Desktop.
JavaScript Extension class for exporting a selection in Illustrator as SVG.
/*
* Usage :
*
* new SelectionExporter(
* app.activeDocument.selection[0],
* '/tmp/somefolder/tempfile.ai',
* 'My File'
* );
*/
(function (global) {
/**
* FileListError class.
* @param message
* @constructor
*/
var SelectionExporterError = function(message) {
this.name = "SelectionExporterError";
this.message = message || "Unknown SelectionExporterError";
};
SelectionExporterError.prototype = Error.prototype;
/**
* @param {PageItem} selection The PageItem (or derivative) in the form doc.selection[0].
* @param {string} tmpFilePath Path to where to save the file.
* @param {string} name The name of the PageItem.
* @constructor
*
* Usage:
*
* new SelectionExporter(
* app.activeDocument.selection[0],
* '/tmp/file/path.ai',
* 'My Page Item'
* );
*/
var Instance = function(selection, tmpFilePath, name) {
this.sourceDoc = null;
this.exportDoc = null;
this.itemName = name;
this.tmpFilePath = tmpFilePath;
var module = this;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
try {
if (selection) {
// *********************************************************
var item;
this.sourceDoc = app.activeDocument;
this.exportDoc = app.documents.add( DocumentColorSpace.RGB );
app.activeDocument = module.sourceDoc;
try {
item = selection;
item.name = module.itemName;
module.exportItem(item);
// redraw();
}
catch(e) { throw new SelectionExporterError(e); }
// *********************************************************
}
else{
throw new Error('There are no documents open. Open a document and try again.');
}
}
catch(e) {
throw new SelectionExporterError(e);
}
}
/**
* Exports the selection item.
* @param item
*/
Instance.prototype.exportItem = function(item) {
try {
var newItem,
module = this;
newItem = item.duplicate( module.exportDoc, ElementPlacement.PLACEATEND );
newItem.hidden = false;
newItem.name = item.name;
app.activeDocument = module.exportDoc;
redraw();
module.exportSVG(
module.exportDoc,
item.name,
item.visibleBounds
);
}
catch(e) { throw new SelectionExporterError(e); }
}
/**
* Export SVG.
* @param doc
* @param name
* @param bounds
*/
Instance.prototype.exportSVG = function(doc, name, bounds) {
try {
var module = this;
doc.artboards[0].artboardRect = bounds;
doc.exportFile(
new File( module.tmpFilePath ),
ExportType.SVG,
module.getExportOptions()
);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
catch(e) { throw new SelectionExporterError(e); }
}
/**
* Get export options.
*/
Instance.prototype.getExportOptions = function() {
try {
svgOptions = new ExportOptionsSVG();
svgOptions.embedRasterImages = false;
svgOptions.cssProperties = SVGCSSPropertyLocation.PRESENTATIONATTRIBUTES;
svgOptions.fontSubsetting = SVGFontSubsetting.None;
svgOptions.documentEncoding = SVGDocumentEncoding.UTF8;
svgOptions.coordinatePrecision = 4;
return svgOptions;
}
catch(e) { throw new SelectionExporterError(e); }
}
global.SelectionExporter = Instance;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment