/**********************************************************

ADOBE SYSTEMS INCORPORATED 
Copyright 2005 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. 

*********************************************************/

/**********************************************************
 
SaveDocsAsSVG.js

DESCRIPTION

This sample saves every open document as a SVG in a user specified location.
 
**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var i, sourceDoc, targetFile;

var destFolder = null;
// Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the exported files.', '~' );

if (destFolder != null) {
	for ( i = 0; i < app.documents.length; i++ ) {
		sourceDoc = app.documents[i]; // returns the document object
								
		// Call function getNewName to get the file
		targetFile = getNewName(sourceDoc, destFolder);
		
		// set export options
		// although this uses the doc.exportFile method,
		// it is actually a save as...
		// i.e. the document in Illustrator will change to the result of this operation
		var opt = new ExportOptionsSVG();
		opt.embedRasterImages = true;
		
		// Export
		sourceDoc.exportFile(targetFile, ExportType.SVG,opt);
	}
	alert( 'Files are saved as SVG in ' + destFolder );
}



/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

function getNewName(sourceDoc, destFolder) {
	var docName = sourceDoc.name;
	var ext = '.svg'; // new extension for pdf file
	var newName = "";

	// if name has no dot (and hence no extension,
	// just append the extension
	if (docName.indexOf('.') < 0) {
		newName = docName + ext;
	} else {
		var dot = docName.lastIndexOf('.');
		newName += docName.substring(0, dot);
		newName += ext;
	}
	
	// Create a file object to save the pdf
	saveInFile = new File( destFolder + '/' + newName );
	return saveInFile;
}



