Skip to content

Instantly share code, notes, and snippets.

@mxl
Last active January 1, 2016 22:39
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 mxl/8211617 to your computer and use it in GitHub Desktop.
Save mxl/8211617 to your computer and use it in GitHub Desktop.
Exports first-level layers and layer groups to png images for iPad and iPad with Retina display.
#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" : "") + "~ipad.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