Skip to content

Instantly share code, notes, and snippets.

@savage69kr
Forked from bebensiganteng/source
Created April 4, 2014 14:42
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 savage69kr/9976099 to your computer and use it in GitHub Desktop.
Save savage69kr/9976099 to your computer and use it in GitHub Desktop.
#target photoshop
/*--------------------------------------------------------
Config
-------------------------------------------------------*/
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
/*--------------------------------------------------------
Functions
-------------------------------------------------------*/
function doCheck() {
if(!documents.length) {
alert('There are no documents open.');
return;
}
}
function traverseLayer(doc) {
var layers = doc.layers;
// not very efficient for multilevel layers
for(var i = 0; i < layers.length; i++) {
// if group
if(layers[i].typename == "LayerSet") {
var name = layers[i].name;
// filter by group name
if(name.indexOf("@") > 0) {
for(var j = 0; j < layers[i].layers.length; j++) {
getLayerAttribute(layers[i].layers[j], name);
}
}
}
}
}
function getLayerAttribute(layer, name) {
// write down the attributes to XML
var s = "\t<pos name=\"" + name.substring(0, name.length - 1) + ".png\" x=\"" + layer.bounds[0].value + "\" y=\"" + layer.bounds[1].value + "\"></pos>\n";
str += s.toString();
}
/*--------------------------------------------------------
Start
--------------------------------------------------------*/
var doc = app.activeDocument;
var docWidth = doc.width.value;
var docHeight = doc.height.value;
var docFilePath = doc.fullName.path + "/";
var docFileName = doc.name.match(/([^\.]+)/)[1];
var docXMLName = docFilePath.toString().match(/([^\.]+)/)[1] + docFileName + ".xml";
var str = "<item btn=\"btn" + docFileName +".png\" cover=\""+ docFileName + ".png\">\n";
doCheck();
traverseLayer (doc);
// create a reference to a file for output
var csvFile = new File(docXMLName);
// open the file, write the data, then close the file
csvFile.open('w');
csvFile.writeln(str + "</item>");
csvFile.close();
preferences.rulerUnits = originalRulerUnits;
// Confirm that operation has completed
alert("XML Complete!""\n" + docXMLName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment