Copy link to clipboard
Copied
Hello everyone,
i often use a script to convert AI files to SVG files
This works really like a charm, but the script gets garbled if the source filenames contain dots in their names (e.g. "File1.2.ai").
I think that some of the code needs to be adapted, but despite a lot of reading and trying I can't get it right...
Here is the complete script code:
/**********************************************************
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( 'Quell-Verzeichnis mit AI-Dateien auswählen', '~' );
// 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( 'Ziel-Verzeichnis für die SVGs auswählen.', '~' );
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();
svgSaveOpts.DTD = SVGDTDVersion.SVG1_1;
svgSaveOpts.embedRasterImages = false;
svgSaveOpts.cssProperties = SVGCSSPropertyLocation.STYLEATTRIBUTES;
svgSaveOpts.coordinatePrecision = 3;
svgSaveOpts.fontType = SVGFontType.SVGFONT;
svgSaveOpts.documentEncoding = SVGDocumentEncoding.UTF8;
svgSaveOpts.fontSubsetting = SVGFontSubsetting.None;
svgSaveOpts.response = false;
svgSaveOpts.sVGTextOnPath = true;
svgSaveOpts.slices = false;
svgSaveOpts.preserveEditability = false;
svgSaveOpts.sVGAutoKerning = false;
return svgSaveOpts; }
Here is an example what happen, when the source files have a . in the name:
Output file then:
It should create 3x SVG files (900.10.svg for example), but unfortunately only the one file with the wrong name is created.
Can anyone help me with this?
I think the code need some adjustment, but I am not capable of that...
Thanks a lot and best regards,
Sebastian
Change
newName = "";
for ( var i = 0 ; docName[i] != "." ; i++ )
{
newName += docName[i];
}
To:
newName = docName.replace(/\.[^\.]+$/, "");
Copy link to clipboard
Copied
Change
newName = "";
for ( var i = 0 ; docName[i] != "." ; i++ )
{
newName += docName[i];
}
To:
newName = docName.replace(/\.[^\.]+$/, "");
Copy link to clipboard
Copied