#target photoshop // app.bringToFront(); //
var TYPE_SPRITE = "sprite"; var TYPE_TEXT = "text"; var TYPE_COLLECTION = "collection";
var arts = app.activeDocument.artLayers// var sets = app.activeDocument.layerSets;// var respont = new Object();//. var arr = new Array();//. var u = 0;// . var width_document = app.activeDocument.width.value; var heigth_document = app.activeDocument.height.value;
alert("!"); var defaultInFolder = new Folder ("C:\\"); var jsonFilter ="JSON Text Files:*.json,All Files:*.*"; var inFile = defaultInFolder.saveDlg ("Open JSON text file:", jsonFilter);
if(inFile) main();
function main() { for(var j=0; j<arts.length; j++) {// respont[u] = setLayers(arts[j]); // . u+=1; } for(var i = 0;i<sets.length;i++)// { respont[u] = setSets(sets[i]); // . u+=1; } for(var i = 0;i<arr.length;i++)// { visible(false);// visibleElement(arr[i],true);// saveLayer (arr[i]);// } visible(true);// jamUtils.writeJsonFile (inFile, respont, 4);// . alert ("!"); }
function setSets(set) { var res = new Object(); res["type"] = TYPE_COLLECTION; res["name"] = set.name; for(var j=0; j<set.artLayers.length ; j++) { res[j] = setLayers(set.artLayers[j]); // } return res; }
function setLayers(el) { var res= new Object(); var x; var y; x = el.bounds[0].value; y = el.bounds[1].value; res["x"] = x; res["y"] = y; res["alpha"] = Math.round(el.opacity)/100;// 0-1 res["name"] = el.name.replace(/\s+/g, '');// try{// var textItem = el.textItem res["type"] = TYPE_TEXT;// , , res["color"] = textItem.color.rgb.hexValue; res["font"] = textItem.font; res["size"] = Math.round(textItem.size.value); res["contents"] = textItem.contents; res["width"] = Math.round(textItem.width.value); res["height"] = Math.round(textItem.height.value); }catch(e) { res["type"] = TYPE_SPRITE; } arr.push(el); return res; }
function saveLayer(layer) { try{ var textItem = layer.textItem // , return; }catch(e){ } activeDocument.activeLayer = layer; var o = layer.opacity; // var xx = (layer.bounds[0].value); var yy = (layer.bounds[1].value); var ww = (layer.bounds[2].value); var hh = (layer.bounds[3].value); var shape = [ [xx, yy], [ww,yy], [ww,hh], [xx,hh] ]; app.activeDocument.selection.select(shape);// app.activeDocument.selection.translate(new UnitValue(-xx,"px"),new UnitValue(-yy,"px"));// 0 0 app.activeDocument.selection.deselect();// app.activeDocument.resizeCanvas(new UnitValue(ww-xx, "px"), new UnitValue(hh-yy, "px"), AnchorPosition.TOPLEFT);// activeDocument.activeLayer.opacity = 100;// 100 var saveFile= File(inFile.path+"/"+layer.name.replace(/\s+/g, '')+".png"); SavePNG(saveFile);// activeDocument.activeLayer.opacity = o;// app.activeDocument.resizeCanvas(new UnitValue(width_document, "px"), new UnitValue(heigth_document, "px"), AnchorPosition.TOPLEFT);// app.activeDocument. activeLayer.translate(new UnitValue(xx,"px"),new UnitValue(yy,"px"));// } function SavePNG(saveFile){ var pngOpts = new ExportOptionsSaveForWeb; pngOpts.format = SaveDocumentType.PNG pngOpts.PNG8 = false; pngOpts.transparency = true; pngOpts.interlaced = false; pngOpts.quality = 100; activeDocument.exportDocument(new File(saveFile),ExportType.SAVEFORWEB,pngOpts); }
function visibleElement(sprite,flag) { sprite.visible = flag; } function visible(flag) { for(var i = 0;i<arr.length;i++) { arr[i].visible = flag; } }
{ "0": { "x": 205, "y": 53, "alpha": 1, "name": "magic", "type": "text", "color": "0006FC", "font": "Verdana", "size": 30, "contents": " ", "width": 417, "height": 180 }, "1": { "x": 458, "y": 281, "alpha": 1, "name": "el5", "type": "sprite" }, ... "6": { "type": "collection", "name": "btn", "0": { "x": 300, "y": 537, "alpha": 1, "name": "btn_art", "type": "sprite" } } }
Source: https://habr.com/ru/post/229103/
All Articles