Created
August 19, 2012 22:16
-
-
Save jpsim/3398148 to your computer and use it in GitHub Desktop.
ExtendScript iOS export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script makes a copy merged based on selection (or entire canvas), | |
// creates a new doc based on rounding the merged size to even dimensions, | |
// pastes it into a new doc, then exports it for both retina and non-retina iOS devices. | |
// | |
// Written on August 19, 2012 by JP Simard, Magnetic Bear Studios Inc. | |
exportForIOS(); | |
function exportForIOS() { | |
// 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, "iOS 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 retina and non-retina versions | |
saveForWebPNG(Folder(baseFolder + "/elements@2x"),elementName+"@2x"); | |
myNewDoc.resizeImage(myNewDoc.width/2, myNewDoc.height/2); | |
saveForWebPNG(Folder(baseFolder + "/elements@1x"),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