Skip to content

Instantly share code, notes, and snippets.

@geekgonecrazy
Last active December 14, 2015 16:08
Show Gist options
  • Save geekgonecrazy/5112511 to your computer and use it in GitHub Desktop.
Save geekgonecrazy/5112511 to your computer and use it in GitHub Desktop.
Exports whats on artboard to png. illustrator CS5 only
/*
@author: Aaron Ogle
Script to Export Illustrator files as PNG's then make a thumbnail.
Runs in CS5 only
*/
//Disable Alerts. We want this to be as unobtrusive as possible.
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
main();
function main() {
// Grab the current doc
var sourceDoc = app.activeDocument;
var docName = sourceDoc.name;
var srcFolder = app.activeDocument.path;
//sourceDoc.selection = null; // deselect everything
sourceDoc.artboards.setActiveArtboardIndex (0); // activate the artboard.
//sourceDoc.selectObjectsOnActiveArtboard(); // select all in artboard
sel = sourceDoc.selection[0]; // Get selection
var vB = sel.visibleBounds;
var gB = sel.geometricBounds;
//sourceDoc.fitArtboardToSelectedArt(0); //Resize artboard to the art.
sourceDoc.artboards[0].artboardRect = [gB[0] -15, gB[1] +15,gB[2] +15, gB[3] -15];
// Call function getNewName to get the name and file to save the pdf
targetFile = getNewName(docName, srcFolder, 300);
// Call function getPNGOptions get the PNGExportOptions for the files
pngExportOpts = getPNGOptions();
// Export as PNG
app.activeDocument.exportFile( targetFile, ExportType.PNG24, pngExportOpts );
alert(sourceDoc.artboards[0].artboardRect);
// Fire up photoshop for creating thumbnail image
btMessaging(targetFile, getNewName(docName, srcFolder, 145));
}
// Function to be executed in Photoshop for resizing. Comments in this function mess up the BridgeTalk message.
function createThumb(file, saveFile) {
psDoc = app.open(file);
if(psDoc.height > psDoc.width) {
psDoc.resizeImage(null,UnitValue(145,'px'),null,ResampleMethod.BICUBIC);
} else {
psDoc.resizeImage(UnitValue(145,'px'),null,null,ResampleMethod.BICUBIC);
}
var pngOpts = new ExportOptionsSaveForWeb;
pngOpts.format = SaveDocumentType.PNG;
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.quality = 100;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB,pngOpts);
psDoc.close(SaveOptions.DONOTSAVECHANGES);
}
//BridgeTalk function
function btMessaging(file, savepath) {
//Create BridgeTalk message
var bt = new BridgeTalk();
bt.target = 'photoshop-12.032';
bt.body = createThumb.toSource() + "(" + file.toSource()+ ","+savepath.toSource()+");";
bt.onError = function( btObj ){};
bt.onResult = function( inBT ){};
bt.send(30);
}
//Function to create the new file name.
function getNewName(doc, dstFolder, size) {
var ext, docName, newName, saveInFile, docName;
docName = doc;
ext = '.png'; // new extension for png file
newName = "";
for ( var i = 0 ; docName[i] != "." ; i++ ) {
newName += docName[i];
}
// If -[Converted] shows up remove it.
newName = newName.replace (/.\[Converted\]/g, "");
newName += ext; // full png name of the file
// Destination folder, with image size dir
dstFolder = dstFolder +'/'+size+'px/';
// If Folder isn't there create it
if(!Folder(dstFolder).exists){
Folder(dstFolder).create();
}
// Create a file object to save the png
saveInFile = new File( dstFolder + newName );
return saveInFile;
}
//Function setting PNG options for Illustrator
function getPNGOptions() {
// Create the PDFSaveOptions object to set the PDF options
var pngExportOpts = new ExportOptionsPNG24();
pngExportOpts.antiAliasing = true;
pngExportOpts.artBoardClipping = true;
pngExportOpts.horizontalScale = 100.0;
pngExportOpts.transparency = true;
pngExportOpts.verticalScale = 100.0;
return pngExportOpts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment