Export Layer Coordinates to XML

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

pattesdours

Export Layer Coordinates to XML

Post by pattesdours »

Okay, So I've come an even longer way with this script. I needed to use XML instead of CSV, so I modified it accordingly. It will build an extended set of information for all visible layers in your PSD, and save it as an XML file in the same location as the PSD was saved to disk. Example:
Code: Select all<psd filename="layer_comps_mockup.psd" path="~/Desktop/" width="640" height="480">
   <layer name="Background" stack="0" position="0, 0" layerwidth="640" layerheight="480" transformpoint="center">Background.png</layer>
   <layer name="red_square" stack="1" position="0, 0" layerwidth="190" layerheight="190" transformpoint="center">red_square.png</layer>
   <layer name="yellow_circle" stack="2" position="31, 268" layerwidth="190" layerheight="190" transformpoint="center">yellow_circle.png</layer>
   <layer name="white_star" stack="3" position="221, 94" layerwidth="218" layerheight="207" transformpoint="center">white_star.png</layer>
   <layer name="green_pentagon" stack="4" position="426, 29" layerwidth="192" layerheight="192" transformpoint="center">green_pentagon.png</layer>
   <layer name="blue_triangle" stack="5" position="255, 301" layerwidth="168" layerheight="155" transformpoint="center">blue_triangle.png</layer>
   <layer name="black_rectangle" stack="6" position="493, 242" layerwidth="106" layerheight="216" transformpoint="center">black_rectangle.png</layer>
</psd>
This is quite useful for automatically re-building a layout using cropped PNGs in an environment where elements have to be updated frequently (I could not use the import PSD layers function in flash cs3 because of that)

Worth noting maybe, I no longer have to get rid of the ' px' string on returned data, since i'm now using 'bounds[x].value' (wish I had come across that info before.)
pivanet@gmail.com

Export Layer Coordinates to XML

Post by pivanet@gmail.com »

thanks a lot!!! You saved me so much time!!!
pattesdours

Export Layer Coordinates to XML

Post by pattesdours »

glad to be of help!
FumarMata

Export Layer Coordinates to XML

Post by FumarMata »

Thanks!

You saved me so much time too!

Some keywords to help the next guy googling:

export photoshop layer size, automatic layer size photoshop, export layer position photoshop, export photoshop metadata

fabiantheblind

Export Layer Coordinates to XML

Post by fabiantheblind »

Hi, i'm trying to build on your script my own. It should export documents data like width and height and so on into a single xml file that can be used as database. My Problem is right now ti read the xml file first and then add the "analysis" the existung content.

My Code looks like this:
Code: Select allfunction docCheck() {
    // ensure that there is at least one document open
    if (!documents.length) {
        alert('There are no documents open.');
        return; // quit
    }
}

docCheck();

var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

var docRef = activeDocument;
var docWidth = docRef.width.value;
var docHeight = docRef.height.value;
var mySourceFilePath = activeDocument.fullName.path + "/";
var docRes = docRef.resolution.value;


var string01 = "<file filename=\""+ docRef.name
+ "\" path=\"" + mySourceFilePath
+ "\" width=\"" + docWidth
+ "\" height=\"" + docHeight
+ "\"resolution=\"" + docRes + "\" >\r"
+ "<histogramArray>\r";
   var histoStr01 = "";
   var counter =0;

 
for (var i =docRef.histogram.length-1;i>=0; i-- ){
histoStr01 = histoStr01+"<histogram value"+i+" = \"" +docRef.histogram +"\"> </histogram>\r"  ;
   }

string01 = string01+histoStr01.toString();
string01 = string01+ "</histogramArray>\r</file>";
//var BPC = docRef.bitsPerChannel();

// create a reference to a file for output
    var csvFile = File(mySourceFilePath.toString() +"Test02.xml");
// open the file, write the data, then close the file
csvFile.open('w');
var fileContent = csvFile.read();
csvFile.writeln(fileContent+string01);
csvFile.close();
preferences.rulerUnits = originalRulerUnits;


// end of code

Why doesn't it read the file first?

I'm happy for every response

Cheers

Fab
Paul MR

Export Layer Coordinates to XML

Post by Paul MR »

csvFile.open('w');
var fileContent = csvFile.read();

You have opened the file for write hense NO data will be in the file, it would need to be opened in read or append mode to get any data.
fabiantheblind

Export Layer Coordinates to XML

Post by fabiantheblind »

Super. Thanx for the quick help…
TZ1

Export Layer Coordinates to XML

Post by TZ1 »

Hi,

This script is great!

I wonder if it is possible to export this layer information to XML grouped according to the layer groups (folders) that exist within my Photshop file?

Any assistance would be greatly appreciated - sorry for the newbie question.
fabiantheblind

Export Layer Coordinates to XML

Post by fabiantheblind »

Hi, to loop thru the whole ps document with all its Layersets and layers you should check out this script by Trevor Morris ( http://morris-photographics.com/ )
http://morris-photographics.com/photos ... 01-8-1.jsx

Especially the functions

Code: Select allfunction renameLayers(ref, prefs)

function rename()

He has a really smart way to loop thru the whole document and check if it is a layer or a layerset.

it is sometinhg like this:

Code: Select allcheckForLayerset();

function checkForLayerset()
{
   
    // the active frontmost document
    var doc = app.activeDocument;

check_Layers(doc);


}

function check_Layers(ref) {
   // declare local variables
   var len = ref.layers.length;

   // check layers top to bottom
      for (var i = 0; i < len; i++) {
         the_check(ref.layers,i);   
   }
}

   function the_check(inLayer,count) {


      var layer = inLayer;

      // check for groups
      if (layer.typename == 'LayerSet') {
         
         alert( "i am a layerSet called " + layer.name);
//**************
// build a container xml-element here
//**************
         check_Layers(layer);
      }
      // check layer
      else {
//**************
// build a layer info xml-element here
//**************         
         alert( "i am a layer called " + layer.name);

      }
   }

Best
:F
TZ1

Export Layer Coordinates to XML

Post by TZ1 »

Thanks, will give this a try!