Last active
January 1, 2016 22:39
-
-
Save mxl/8211646 to your computer and use it in GitHub Desktop.
Exports first-level layers and layer groups to png images for iPhone and iPhone with Retina display.
This file contains hidden or 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
#target photoshop | |
function main(){ | |
function scanLayers(el, path, isRetina) { | |
new Folder(path).create(); | |
// process layer sets (groups) | |
for(var i = 0; i < el.layerSets.length; i++){ | |
var layer = el.layerSets[i]; | |
if (!layer.visible) | |
continue; | |
// merge layer set sub-layers and export | |
saveLayer(layer, path, true, isRetina); | |
} | |
// process plain layers | |
for(var j = 0; j < el.artLayers.length; j++) { | |
var layer = el.artLayers[j]; | |
if (!layer.visible) | |
continue; | |
saveLayer(layer, path, false, isRetina); | |
} | |
}; | |
function saveLayer(layer, path, shouldMerge, isRetina) { | |
var workingDoc = app.documents.add(originalDoc.width, originalDoc.height, originalDoc.resolution, originalDoc.name + " copy", NewDocumentMode.RGB, DocumentFill.TRANSPARENT); | |
try { | |
// duplicate work only for active document | |
app.activeDocument = originalDoc; | |
layer.duplicate(workingDoc); | |
//merge work only for active document | |
app.activeDocument = workingDoc; | |
workingDoc.trim(TrimType.TRANSPARENT,true,true,true,true); | |
if (shouldMerge === undefined || shouldMerge === true) { | |
workingDoc.mergeVisibleLayers(); | |
} | |
// save file to folder with parent layer name | |
var saveFile= File(path + "/" + layer.name + (isRetina ? "@2x" : "") + ".png"); | |
savePNG(workingDoc, saveFile); | |
} | |
catch(e) { | |
alert(e); | |
} | |
// close temporary document | |
workingDoc.close(SaveOptions.DONOTSAVECHANGES); | |
}; | |
function savePNG(workingDoc, saveFile){ | |
var pngOpts = new ExportOptionsSaveForWeb; | |
pngOpts.format = SaveDocumentType.PNG | |
pngOpts.PNG8 = false; | |
pngOpts.transparency = true; | |
pngOpts.interlaced = false; | |
pngOpts.quality = 100; | |
workingDoc.exportDocument(new File(saveFile), ExportType.SAVEFORWEB, pngOpts); | |
}; | |
if(!documents.length) | |
return; | |
// suppress all dialogs | |
app.displayDialogs = DialogModes.NO; | |
var originalDoc = app.activeDocument; | |
var docPath = originalDoc.path; | |
// get file name without extension | |
var docName = originalDoc.name.replace(/\.[^\.]+$/, ''); | |
var docFullName = originalDoc.fullName; | |
scanLayers(originalDoc, docPath + "/" + docName, true); | |
originalDoc.resizeImage(originalDoc.width / 2, originalDoc.height / 2, undefined, ResampleMethod.BICUBIC); | |
scanLayers(originalDoc, docPath + "/" + docName, false); | |
originalDoc.close(SaveOptions.DONOTSAVECHANGES); | |
app.open(File(docFullName)); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment