Skip to content

Instantly share code, notes, and snippets.

@moritzschaefer
Created August 24, 2020 15:39
Show Gist options
  • Save moritzschaefer/7aa4c5a446fa85d59b9ed5066ded2bef to your computer and use it in GitHub Desktop.
Save moritzschaefer/7aa4c5a446fa85d59b9ed5066ded2bef to your computer and use it in GitHub Desktop.
Adobe illustrator script to batch process SVG files: 1. open SVG 2. resize to DIN A4 3. save as ai files
function resize() {
if (app.documents.length > 0) {
var docRef = app.activeDocument;
if (docRef.artboards.length > 1)
{
alert('Need exactly one artboard');
quit;
}
// Found 1 artboard
var current = docRef.artboards[0].artboardRect;
current[2] = current[0] + 595.2756;
current[3] = current[1] - 841.8898;
docRef.artboards[0].artboardRect = current;
}
else {
alert('Open a document before running this script', 'Error running FitArtboardToArt.jsx');
}
}
var sourceFolder = Folder("C:\\Users\\Moritz\\Syncbox\\svgs");
var destFolder = Folder("C:\\Users\\Moritz\\Syncbox\\ais"); // optional: destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted SVG files.', '~' );
function getNewName(sourceDoc)
{
var ext, docName, newName, saveInFile, docName;
docName = sourceDoc.name;
ext = '.ai'; // new extension
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;
}
if ( sourceFolder != null )
{
var files = new Array();
//What kind of file in the source folder are you seeking?
var fileType = "*.svg";
// Get all files matching that file type
files = sourceFolder.getFiles( fileType );
if ( files.length > 0 )
{
for ( i = 0; i < files.length; i++ )
{
//Open a file
var sourceDoc = app.open(files[i]);
resize();
app.activeDocument.saveAs(getNewName(sourceDoc));
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
alert( 'Complete' );
}
else
{
alert( 'No matching files found' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment