Forked from tomekc/ps-export-layers-to-png.jsx
Last active
September 11, 2018 06:50
-
-
Save piotr-jaworski/f86a3aee8da462e8f114798ff26c5a77 to your computer and use it in GitHub Desktop.
Added export fonts information
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
#target photoshop | |
// $.level = 2; | |
/* | |
* Script by Tomek Cejner (tomek (at) japko dot info) | |
* based on work of Damien van Holten: | |
* http://www.damienvanholten.com/blog/export-groups-to-files-photoshop/ | |
* | |
* My version adds support of nested layer groups, | |
* and exports single layers in addition to groups. | |
* | |
* added by Piotr Jaworski | |
* export fonts information to css file (not proper css but to easy close/open nodes in notepad++ ) | |
*/ | |
var fonts_family = {}; | |
var fonts_size = {}; | |
var fonts_color = {}; | |
var fonts_color_error = {errors:[]}; | |
var oldPath = activeDocument.path; | |
var outputFontsStat = new File(oldPath + "/out/" + activeDocument.name + "fonts_stat.css" ); | |
outputFontsStat.open("w"); | |
function main(){ | |
if(!documents.length) return; | |
var doc = activeDocument; | |
var outFolder = new Folder(oldPath + "/out"); | |
if (!outFolder.exists) { | |
outFolder.create(); | |
} | |
var outputFonts = new File(oldPath + "/out/" + activeDocument.name + "fonts.css" ); | |
outputFonts.open("w"); | |
scanLayerSets(doc); | |
function scanLayerSets(el) { | |
// find layer groups | |
for(var a=0;a<el.layerSets.length;a++){ | |
var lname = el.layerSets[a].name; | |
if (lname.substr(-4) == ".png") { | |
saveLayer(el.layers.getByName(lname), lname, oldPath, true); | |
} else { | |
outputFonts.write( | |
"\n## START " + lname + "##{\n"); | |
// recursive | |
scanLayerSets(el.layerSets[a]); | |
outputFonts.write( | |
"\n}## END " + lname + "##\n"); | |
} | |
} | |
// find plain layers in current group whose names end with .png | |
for(var j=0; j<el.artLayers.length; j++) { | |
var name = el.artLayers[j].name; | |
if (name.substr(-4) == ".png") { | |
saveLayer(el.layers.getByName(name), name, oldPath, false); | |
} | |
doc.activeLayer = el.layers[j] | |
if (doc.activeLayer.kind == LayerKind.TEXT) | |
{ | |
try { | |
if(typeof doc.activeLayer.textItem.color == 'undefined'){ | |
var colorTmp = "#undefined"; | |
}else{ | |
var colorTmp = "#" + doc.activeLayer.textItem.color.rgb.hexValue; | |
} | |
var error = false; | |
} | |
catch(err) { | |
var colorTmp = "#error"; | |
var error = "color error:" + err.name + " " + err.message; | |
fonts_color_error.errors.push(error); | |
} | |
outputFonts.write( | |
"\n/* " + | |
doc.activeLayer.textItem.contents + " */{\n" + | |
"font-family: " + doc.activeLayer.textItem.font +";\n" + | |
"font-size: " + doc.activeLayer.textItem.size +";\n" + | |
"color: " + colorTmp + ";\n}\n"); | |
if(error){ | |
outputFonts.write( | |
"\n/* ERROR */{\n" + | |
error + "\n}\n"); | |
} | |
if(typeof fonts_family[doc.activeLayer.textItem.font] == 'undefined'){ | |
fonts_family[doc.activeLayer.textItem.font] = 0; | |
} | |
fonts_family[doc.activeLayer.textItem.font]++; | |
if(typeof fonts_size[doc.activeLayer.textItem.size] == 'undefined'){ | |
fonts_size[doc.activeLayer.textItem.size] = 0; | |
} | |
fonts_size[doc.activeLayer.textItem.size]++; | |
if(typeof fonts_color[colorTmp] == 'undefined'){ | |
fonts_color[colorTmp] = 0; | |
} | |
fonts_color[colorTmp]++; | |
} | |
} | |
} | |
function saveLayer(layer, lname, path, shouldMerge) { | |
activeDocument.activeLayer = layer; | |
dupLayers(); | |
if (shouldMerge === undefined || shouldMerge === true) { | |
activeDocument.mergeVisibleLayers(); | |
} | |
activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true); | |
var saveFile= File(path +"/out/"+lname); | |
SavePNG(saveFile); | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
} | |
}; | |
main(); | |
for (var key in fonts_family) { | |
if (fonts_family.hasOwnProperty(key)){ | |
outputFontsStat.write("\n" + key + ": " + fonts_family[key]); | |
} | |
} | |
outputFontsStat.write("\n"); | |
for (var key in fonts_size) { | |
if (fonts_size.hasOwnProperty(key)){ | |
outputFontsStat.write("\n" + key + ": " + fonts_size[key]); | |
} | |
} | |
outputFontsStat.write("\n"); | |
for (var key in fonts_color) { | |
if (fonts_color.hasOwnProperty(key)){ | |
outputFontsStat.write("\n" + key + ": " + fonts_color[key]); | |
} | |
} | |
outputFontsStat.write("\n"); | |
for (var key in fonts_color_error.errors) { | |
if (fonts_color_error.errors.hasOwnProperty(key)){ | |
outputFontsStat.write("\n" + key + ": " + fonts_color_error.errors[key]); | |
} | |
} | |
function dupLayers() { | |
var desc143 = new ActionDescriptor(); | |
var ref73 = new ActionReference(); | |
ref73.putClass( charIDToTypeID('Dcmn') ); | |
desc143.putReference( charIDToTypeID('null'), ref73 ); | |
desc143.putString( charIDToTypeID('Nm '), activeDocument.activeLayer.name ); | |
var ref74 = new ActionReference(); | |
ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); | |
desc143.putReference( charIDToTypeID('Usng'), ref74 ); | |
executeAction( charIDToTypeID('Mk '), desc143, DialogModes.NO ); | |
}; | |
function SavePNG(saveFile){ | |
var pngOpts = new PNGSaveOptions; | |
pngOpts.compression = 7; | |
pngOpts.interlaced = false; | |
activeDocument.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment