Batch Image Processing Illustrator Files With Javascript | Accella Accella

Batch Image Processing Illustrator Files With Javascript

August 29th, 2012

We recently received a large set of .eps files that needed to be scaled, optimized and converted to .png format to use in an iOS and Android project. Here is a simple script you can use with Illustrator to process these. Save the code below as a javascript file. Then in Illustrator navigate to choose File > Scripts > Other Script and select the script.

[code lang=”javascript”]
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// 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 pdf
targetFile = getNewName();

// Call function getPNGOptions get the PNGExportOptions for the files
pngExportOpts = getPNGOptions();

// Export as PNG
sourceDoc.exportFile( targetFile, ExportType.PNG24, pngExportOpts );

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 png file
newName = "";

for ( var i = 0 ; docName[i] != "." ; i++ )
{
newName += docName[i];
}
newName += ext; // full png 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 PDFSaveOptions object to set the PDF options
var pngExportOpts = new ExportOptionsPNG24();

// Setting PNGExportOptions properties. Please see the JavaScript Reference
// for a description of these properties.
// Add more properties here if you like
pngExportOpts.antiAliasing = true;
pngExportOpts.artBoardClipping = false;
pngExportOpts.horizontalScale = 350.0; // scaling to 350%
pngExportOpts.saveAsHTML = false;
pngExportOpts.transparency = false;
pngExportOpts.verticalScale = 350.0; // scaling to 350%

return pngExportOpts;
}
[/code]

Jacob Haskins

Jacob has always had an interest in learning and problem solving. Whether working with Objective C, PHP, Actionscript, FLEX, JavaScript or any other language, he finds that there is always something new to learn. He enjoys development projects most when getting to use new technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Mobile App Tech Alerts

We let you know when mobile changes and advancements happen and how they impact your business.

SIGN UP

Tags