Skip to content

Instantly share code, notes, and snippets.

@jz5

jz5/Save as PNGs.jsx

Created Oct 21, 2017
Embed
What would you like to do?
/**********************************************************
Save as PNGs.jsx
DESCRIPTION
This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as PNGs in the user desired destination with the same
file name.
**********************************************************/
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile;
// Select the source folder.
sourceFolder = Folder.selectDialog('Select the folder with Illustrator files you want to convert to PNG', '~');
// 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 PNG 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 PNG
targetFile = getNewName();
// Export as PNG
sourceDoc.exportFile(targetFile, ExportType.PNG24, getPNGOptions());
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
}
alert('Files are saved as PNG 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 = '.png'; // new extension for file
newName = "";
for (var i = 0; docName[i] != "."; i++) {
newName += docName[i];
}
newName += ext; // full name of the file
// Create a file object to save the png
saveInFile = new File(destFolder + '/' + newName);
return saveInFile;
}
/*********************************************************
getPNGOptions: Function to set the PNG saving options of the
files using the ExportOptionsPNG24 object.
**********************************************************/
function getPNGOptions() {
// Create the ExportOptionsPNG24 object to set the PNG options
var opts = new ExportOptionsPNG24();
// Setting ExportOptionsPNG24 properties. Please see the JavaScript Reference
// for a description of these properties.
// Add more properties here if you like
opts.antiAliasing = true;
opts.artBoardClipping = true;
opts.horizontalScale = 100.0;
opts.saveAsHTML = false;
opts.transparency = true;
opts.verticalScale = 100.0;
return opts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment