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/8211658 to your computer and use it in GitHub Desktop.
Save mxl/8211658 to your computer and use it in GitHub Desktop.
Exports first-level layers and layer groups to png images for Android (ldpi, mdpi, hdpi, xhdpi).
#target photoshop
function main(){
if(!documents.length)
return;
// suppress all dialogs
app.displayDialogs = DialogModes.NO;
var mainDoc = app.activeDocument;
var docFullName = mainDoc.fullName;
var docPath = mainDoc.path;
// get file name without extension
var docName = mainDoc.name.replace(/\.[^\.]+$/, '');
var dpiNames = ["ldpi", "mdpi", "hdpi", "xhdpi"];
var dpiCoefs = [0.375, 0.5, 0.75, 1];
//var dpiCoefs = [0.666666, 1, 1.5, 2];
for (var i = 0; i < dpiNames.length; i++) {
mainDoc.resizeImage(mainDoc.width * dpiCoefs[i], mainDoc.height * dpiCoefs[i], undefined, ResampleMethod.BICUBIC);
var dpiName = dpiNames[i];
var workingPath = docPath + "/" + docName + "/" + dpiName;
new Folder(workingPath).create();
//savePSD(mainDoc, workingPath + "/" + dpiName + "-" + mainDoc.name);
exportLayersToPSD(mainDoc, workingPath);
mainDoc.close(SaveOptions.DONOTSAVECHANGES);
mainDoc = app.open(File(docFullName));
}
};
function savePSD(workingDoc, saveFile){
var psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
workingDoc.saveAs(new File(saveFile), psdOpts, false, Extension.LOWERCASE);
};
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);
};
function exportLayersToPSD(originalDoc, path) {
function scanLayers(el, path) {
// process layer sets (groups)
for(var i = 0; i < el.layerSets.length; i++){
var layer = el.layerSets[i];
if (!layer.visible)
continue;
saveLayer(layer, path);
}
// process plain layers
for(var j = 0; j < el.artLayers.length; j++) {
var layer = el.artLayers[j];
if (!layer.visible)
continue;
saveLayer(layer, path);
}
};
function saveLayer(layer, path) {
// create temporary document with same size as original and transparent fill
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);
workingDoc.mergeVisibleLayers();
workingDoc.trim(TrimType.TRANSPARENT,true,true,true,true);
savePNG(workingDoc, File(path + "/" + layer.name + ".png"));
}
catch(e) {
}
// close temporary document
workingDoc.close(SaveOptions.DONOTSAVECHANGES);
};
scanLayers(originalDoc, path);
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment