Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick little script to batch-convert Illustrator .ai's to .svg's, based on Adobe's sample 'save to pdf's' script. Save it to a jsx, and load it under illustrator. Intended for Illustrator CS6.
/**********************************************************
ADOBE SYSTEMS INCORPORATED
Copyright 2005-2010 Adobe Systems Incorporated
All Rights Reserved
NOTICE: Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.
*********************************************************/
/**********************************************************
Save as SVGs.jsx
DESCRIPTION
This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as SVGs in the user desired destination with the same
file name.
Based on Adobe's "Save as PDFs" sample script, intended for Illustrator CS6
**********************************************************/
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, svgSaveOpts;
// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to SVG', '~' );
// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
// Get all files matching the pattern
files = sourceFolder.getFiles( fileType );
if ( files.length > 0 )
{
// Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted SVG files.', '~' );
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files[i]); // returns the document object
// Call function getNewName to get the name and file to save the SVG
targetFile = getNewName();
// Call function getSVGOptions get the SVGSaveOptions for the files
svgSaveOpts = getSVGOptions( );
// Save as svg
sourceDoc.exportFile(targetFile, ExportType.SVG, svgSaveOpts );
sourceDoc.close();
}
alert( 'Files are saved as SVG in ' + destFolder );
}
else
{
alert( 'No matching files found' );
}
}
/*********************************************************
getNewName: Function to get the new file name. The primary
name is the same as the source file.
**********************************************************/
function getNewName()
{
var ext, docName, newName, saveInFile, docName;
docName = sourceDoc.name;
ext = '.svg'; // new extension for svg file
newName = "";
for ( var i = 0 ; docName[i] != "." ; i++ )
{
newName += docName[i];
}
newName += ext; // full svg name of the file
// Create a file object to save the svg
saveInFile = new File( destFolder + '/' + newName );
return saveInFile;
}
function getSVGOptions()
{
var svgSaveOpts = new ExportOptionsSVG();
//just using defaults aside from what's written below
//see http://cssdk.host.adobe.com/sdk/1.0/docs/WebHelp/references/csawlib/com/adobe/illustrator/ExportOptionsSVG.html
svgSaveOpts.embedRasterImages = true;
return svgSaveOpts;
}
@tannerhodges

This comment has been minimized.

Copy link

commented Jun 16, 2015

@seltzered Hey man, thanks a ton for this. Just made my life a whole lot easier.

@HaNdTriX

This comment has been minimized.

Copy link

commented Sep 3, 2015

This is awesome! Thanks!

@uditunpluged

This comment has been minimized.

Copy link

commented Dec 12, 2015

this isn't working in Adobe Illustrator CC 2014

@aaronreimann

This comment has been minimized.

Copy link

commented Feb 18, 2016

This saved me a ton of time. Thanks

@HelloDeadline

This comment has been minimized.

Copy link

commented Jun 16, 2016

It doesn't work in subfolders.

I tried " * / * .ai" as file name mask, but no result.

@Chand1er

This comment has been minimized.

Copy link

commented Nov 14, 2016

To get this script working with CC2015, replace line 46 with this (and don't input anything when prompted, just hit enter):
fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: .ai', '.ai' );

@MarkoDeeJay

This comment has been minimized.

Copy link

commented Feb 28, 2017

This was just great. It saved me a LOT of time. Thank you!

@ghost

This comment has been minimized.

Copy link

commented Sep 7, 2017

Thank you! It saved me a lot of time and nerves.

@Nate-Gee

This comment has been minimized.

Copy link

commented Jan 16, 2018

Any chance this works for cc 2018?

@Blaapje

This comment has been minimized.

Copy link

commented Feb 27, 2018

@sqwear I can confirm this works, good stuff!

@Expl4Life

This comment has been minimized.

Copy link

commented Jun 6, 2018

Extended script with options for optimizing svg

function getSVGOptions() { var svgSaveOpts = new ExportOptionsSVG(); svgSaveOpts.embedRasterImages = false; svgSaveOpts.cssProperties = SVGCSSPropertyLocation.PRESENTATIONATTRIBUTES; svgSaveOpts.coordinatePrecision = 3; svgSaveOpts.fontType = SVGFontType.SVGFONT; svgSaveOpts.documentEncoding = SVGDocumentEncoding.UTF8; svgSaveOpts.fontSubsetting = SVGFontSubsetting.None; return svgSaveOpts; }

@fakepilot

This comment has been minimized.

Copy link

commented Jul 5, 2018

Tested with some Illustrator files, which are a bit older version, I'm getting a "Convert to Artboards" pop-up for every file.
Is it possible to remove it? Or to only choose one time for all the files?
The options that I'm seeing are "Create artboards for: 1) Legacy Artboards 2) Crop area(s) 3) Artwork Bounding Box.
Would be great to only have Crop area(s) checked... All though I can surely understand if you don't have time for this. :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.