-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNGExport.jsx
105 lines (84 loc) · 2.81 KB
/
PNGExport.jsx
1
// v1.2// Set Resolution herevar width = 1920;// Enter export prefix if you dont want to be promptedvar exportName = "";// Default Prompt Valuevar defaultExportPrefix = "Export";// Function to set the export to PNGfunction exportFileToPNG(dest, artBoardIndex, scaleTo){ var exportOptions = new ExportOptionsPNG24(); // or ExportOptionsPNG8 var type = ExportType.PNG24; // or ExportType.PNG8 var file = new File(dest + ".png"); exportOptions.artBoardClipping = true; exportOptions.antiAliasing = true; exportOptions.transparency = true; exportOptions.verticalScale = scaleTo; exportOptions.horizontalScale = scaleTo; exportOptions.qualitySetting = 72; exportOptions.saveMultipleArtboards = false; exportOptions.artboardRange = "" + artBoardIndex; app.activeDocument.exportFile( file, type, exportOptions );}// Main Functionfunction execute(){ if (app.documents.length == 0) { alert('No document open', 'Error'); return; } if (app.activeDocument.selection.length == 0) { alert('Nothing selected', 'Error'); return; } // determine destination var docRef = app.activeDocument; var destFolder = docRef.path; if(destFolder == ""){ destFolder = Folder.selectDialog('Select the folder to export to:'); } if (exportName == "") { exportName = prompt("Please enter target file name prefix", defaultExportPrefix); }; // Loop through the selection and export selected for (var i=0; i< app.activeDocument.selection.length; i++) { // Set selected stuff for easy naming var selectedStuff = app.activeDocument.selection[i]; // Remember original position var oldPosition = selectedStuff.position; // snap position to pixels selectedStuff.position = [ Math.round(selectedStuff.position[0]), Math.round(selectedStuff.position[1]) ]; // create temporary artboad for exporting try { var rect = selectedStuff.visibleBounds; docRef.artboards.add(rect); var scaleTo = width / (rect[2] - rect[0]) * 100; } catch(e) { alert(e); alert('Could not create Artboard as step of export.', 'Failure'); //Restore old position selectedStuff.position = oldPosition return; } if(destFolder) { try { exportFileToPNG(destFolder + "/" + exportName + "_" + docRef.name + "_" + i, docRef.artboards.length, scaleTo); } catch(e) {} } //Restore position selectedStuff.position = oldPosition // delete temp-artboard docRef.artboards.remove(docRef.artboards.length - 1); }}execute();