Skip to content

Instantly share code, notes, and snippets.

@jpsim
Created September 7, 2012 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpsim/3667131 to your computer and use it in GitHub Desktop.
Save jpsim/3667131 to your computer and use it in GitHub Desktop.
ExtendScript for Android export
// This script makes a copy merged based on selection (or entire canvas),
// pastes it into a new doc, then exports it for Android in 320dpi, 240dpi, 160dpi, 120dpi
//
// Written by August 19, 2012 by JP Simard, Magnetic Bear Studios Inc.
exportForAndroid();
function exportForAndroid() {
// save document state
var initialState = activeDocument.activeHistoryState;
// select all if there's no current selection
try {
var calcWidth = activeDocument.selection.bounds[2] - activeDocument.selection.bounds[0];
} catch(e) {
activeDocument.selection.selectAll();
}
// get the width and height of selection
var calcWidth = activeDocument.selection.bounds[2] - activeDocument.selection.bounds[0];
var calcHeight = activeDocument.selection.bounds[3] - activeDocument.selection.bounds[1];
// get the document resolution
var docResolution = activeDocument.resolution;
// copy merged
activeDocument.selection.copy(true);
// reset document state
activeDocument.activeHistoryState = initialState;
// create a new doc based on selection size
var myNewDoc = documents.add(calcWidth, calcHeight, docResolution, "Android Export", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
// paste in from clipboard
myNewDoc.paste();
// crop document to layer size
myNewDoc.crop(myNewDoc.activeLayer.bounds);
// redraw document
app.refresh();
// read doc width and height
var newDocWidth = myNewDoc.width;
var newDocHeight = myNewDoc.height;
// make layer width and height even
if (newDocWidth%2 != 0 || newDocHeight%2 != 0 ) {
var anchorPoint = getAnchorPoint();
newDocWidth += newDocWidth%2;
newDocHeight += newDocHeight%2;
myNewDoc.resizeCanvas(newDocWidth, newDocHeight, anchorPoint);
}
// redraw document
app.refresh();
// name the exported element
var elementName = prompt("Name your element","");
if (!elementName) {
// close temporary document
myNewDoc.close(SaveOptions.DONOTSAVECHANGES);
return;
}
// select target folder
var baseFolder = Folder.selectDialog();
if (!baseFolder) {
// close temporary document
myNewDoc.close(SaveOptions.DONOTSAVECHANGES);
return;
}
// save all DPI versions
saveForWebPNG(Folder(baseFolder + "/320dpi"),elementName);
myNewDoc.resizeImage(myNewDoc.width*.75, myNewDoc.height*.75);
saveForWebPNG(Folder(baseFolder + "/240dpi"),elementName);
myNewDoc.resizeImage(myNewDoc.width/1.5, myNewDoc.height/1.5);
saveForWebPNG(Folder(baseFolder + "/160dpi"),elementName);
myNewDoc.resizeImage(myNewDoc.width*.75, myNewDoc.height*.75);
saveForWebPNG(Folder(baseFolder + "/120dpi"),elementName);
// close temporary document
myNewDoc.close(SaveOptions.DONOTSAVECHANGES);
}
// prompt the user for which anchor position to use when resizing the canvas
function getAnchorPoint() {
// enumerate possible anchor positions
var anchorOptions = [];
anchorOptions[0] = AnchorPosition.BOTTOMCENTER;
anchorOptions[1] = AnchorPosition.BOTTOMLEFT;
anchorOptions[2] = AnchorPosition.BOTTOMRIGHT;
anchorOptions[3] = AnchorPosition.MIDDLECENTER;
anchorOptions[4] = AnchorPosition.MIDDLELEFT;
anchorOptions[5] = AnchorPosition.MIDDLERIGHT;
anchorOptions[6] = AnchorPosition.TOPCENTER;
anchorOptions[7] = AnchorPosition.TOPLEFT;
anchorOptions[8] = AnchorPosition.TOPRIGHT;
// initialize anchorPoint
var anchorPoint = AnchorPosition.TOPLEFT;
// create dialog with anchorOptions dropdown
var dlg = new Window ('dialog', 'Pick an anchor point');
dlg.dropdownlist = dlg.add("dropdownlist", undefined,"AnchorPoint");
var item
for (var i=0,len=anchorOptions.length;i<len;i++)
{item = dlg.dropdownlist.add ('item', "" + anchorOptions[i]);
};
dlg.dropdownlist.onChange = function() {
// save the selected value for return
anchorPoint = anchorOptions[parseInt(this.selection)];
this.parent.close(0);
};
dlg.orientation = 'column';
dlg.center();
dlg.show();
return anchorPoint;
}
// export image as PNG to specified folder
function saveForWebPNG(folder, filename)
{
var opts, file;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.quality = 100;
// check if folder exists, if not create it.
if(!folder.exists) folder.create();
file = new File(folder + "/" + filename + ".png");
activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment