Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save piotr-jaworski/cc7754611f3896db33bf181b00eb4300 to your computer and use it in GitHub Desktop.
Save piotr-jaworski/cc7754611f3896db33bf181b00eb4300 to your computer and use it in GitHub Desktop.
#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 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)
{
outputFonts.write(
"\n/* " +
doc.activeLayer.textItem.contents + " */{\n" +
"font-family: " + doc.activeLayer.textItem.font +";\n" +
"font-size: " + doc.activeLayer.textItem.size +";\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]++;
}
}
}
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]);
}
}
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