//@target illustrator
(function f() {
var doc;
var fileType, targetFile, targetFile1, jpgExportOpts, epsSaveOpts;
var docName;
var pathName;
var destFolder;
var statusMsg = "\n";
var userInteract = userInteractionLevel;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
if (!app.documents.length) {
alert("There are no documents opened. Nothing has been done.");
return;
}
doc = activeDocument;
doc.save();
docName = doc.name;
// check if the filename has extension
if (docName.indexOf('.')) {
var token = docName.lastIndexOf('.');
docName = docName.substring(0, token);
}
pathName = doc.fullName + ''; // convert pathName to string. Otherwise lastIndexOf does not work
token = pathName.lastIndexOf('/');
destFolder = pathName.substring(0, token);
///////////////////////////////////////////////////////
//
// Export JPGs
//
///////////////////////////////////////////////////////
for (var i = 0; i < doc.artboards.length; i++) {
var artb = doc.artboards[i];
doc.artboards.setActiveArtboardIndex(i);
fileType = ExportType.JPEG;
var artbWidth = artb.artboardRect[2] - artb.artboardRect[0];
var artbHeight = artb.artboardRect[1] - artb.artboardRect[3];
jpgExportOpts = getJPGOptions(4000, artbWidth, artbHeight)
// Call function getJPGOptions get the ExportOptionsJPEG for the files
if (jpgExportOpts) {
// Export as a big jpg
if (i < 9) {
targetFile = new File(destFolder + '/' + docName + '-Ai10-0' + (i + 1));
} else {
targetFile = new File(destFolder + '/' + docName + '-Ai10-' + (i + 1));
}
doc.exportFile(targetFile, fileType, jpgExportOpts);
statusMsg += "4000 px JPG\t...\tOK\n"
} else {
statusMsg += "4000 px JPG\t...\tFailed\n"
}
}
///////////////////////////////////////////////////////
//
// Save as EPS
//
///////////////////////////////////////////////////////
// Call function getESPOptions get the EPSSaveOptions for the files
epsSaveOpts = getEPSOptions("10");
targetFile = new File(destFolder + '/' + docName + "-Ai10.eps");
doc.saveAs(targetFile, epsSaveOpts);
statusMsg += "EPS Ai10\t\t...\tOK\n"
// Now EPS file is active. Reopen Ai file
doc.close();
targetFile1 = new File(destFolder + '/' + docName + ".ai");
app.open(targetFile1);
statusMsg += "\nFor updates subscribe: http://oksancia.com/feed"
alert('SaveForStocks-10\nThe following files were saved to:\n' + destFolder + '\n' + statusMsg);
userInteractionLevel = userInteract;
/*********************************************************
getEPSOptions: Function to set the EPS saving options of the
files using the EPSSaveOptions object.
**********************************************************/
function getEPSOptions(compatibility) {
// Create the EPSSaveOptions object to set the PDF options
var epsSaveOpts = new EPSSaveOptions();
// Setting EPSSaveOptions properties. Please see the JavaScript Reference
// for a description of these properties.
if (compatibility == "10") {
epsSaveOpts.compatibility = Compatibility.ILLUSTRATOR10;
} else {
epsSaveOpts.compatibility = Compatibility.ILLUSTRATOR8;
}
epsSaveOpts.includeDocumentThumbnails = true;
epsSaveOpts.postScript = EPSPostScriptLevelEnum.LEVEL2;
epsSaveOpts.preview = EPSPreview.COLORTIFF; //default
epsSaveOpts.saveMultipleArtboards = true;
return epsSaveOpts;
}
/*********************************************************
getJPGOptions: Function to set the JPG saving options of the
files using the EPSSaveOptions object.
**********************************************************/
function getJPGOptions(maxSize, width, height) {
var scaling;
// Create the ExportOptionsJPEG object to set the JPG options
var jpgExportOpts = new ExportOptionsJPEG();
// Setting ExportOptionsJPEG properties. Please see the JavaScript Reference
// for a description of these properties.
jpgExportOpts.antiAliasing = true;
jpgExportOpts.optimization = true;
jpgExportOpts.qualitySetting = 100
jpgExportOpts.artBoardClipping = true;
if (width > height) {
scaling = maxSize / width * 100;
} else {
scaling = maxSize / height * 100;
}
if (scaling > 776.19) {
var needed = Math.round(scaling / 776.19 * 10) / 10;
alert("Warning!\n\nThe image is too small and due to the Illustrator limitations could not be resized to " + maxSize + "px.\n" + "Please scale the image by at least " + needed + " times.");
return 0;
}
jpgExportOpts.horizontalScale = scaling;
jpgExportOpts.verticalScale = scaling;
return jpgExportOpts;
}
}());