Export Layer Coordinates to XML

Upload photoshop scripts, download photoshop scripts, discussion and support of photoshop scripts

Moderator: PS-Moderators

Export Layer Coordinates to XML

Postby pattesdours » Mon Oct 13, 2008 5:32 pm

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.)
Attachments
ExportLayerCoordinatesToXML.jsx.zip
(2.21 KiB) Downloaded 4119 times
pattesdours
 
Posts: 97
Joined: Wed Jun 04, 2008 3:50 pm

Postby pivanet@gmail.com » Mon Nov 24, 2008 5:08 pm

thanks a lot!!! You saved me so much time!!!
pivanet@gmail.com
 
Posts: 1
Joined: Mon Nov 24, 2008 4:37 pm

Postby pattesdours » Wed Nov 26, 2008 3:05 am

glad to be of help!
pattesdours
 
Posts: 97
Joined: Wed Jun 04, 2008 3:50 pm

Postby FumarMata » Wed Nov 18, 2009 9:01 pm

Thanks!

You saved me so much time too! :D

Some keywords to help the next guy googling:

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

:wink:
FumarMata
 
Posts: 1
Joined: Wed Nov 18, 2009 7:03 pm

Exporting Documents Data to Single XML

Postby fabiantheblind » Fri Dec 11, 2009 5:28 pm

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 all
function 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[i] +"\"> </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
Last edited by fabiantheblind on Fri Apr 08, 2011 8:42 am, edited 1 time in total.
// fabiantheblind
// http://www.the-moron.net/
fabiantheblind
 
Posts: 6
Joined: Thu Dec 10, 2009 7:49 pm

Postby Paul MR » Fri Dec 11, 2009 6:25 pm

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.
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

thnx

Postby fabiantheblind » Fri Dec 11, 2009 8:45 pm

Super. Thanx for the quick help…
// fabiantheblind
// http://www.the-moron.net/
fabiantheblind
 
Posts: 6
Joined: Thu Dec 10, 2009 7:49 pm

Re: Export Layer Coordinates to XML

Postby TZ1 » Thu Apr 07, 2011 3:41 pm

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.
TZ1
 
Posts: 2
Joined: Thu Apr 07, 2011 2:35 pm

Re: Export Layer Coordinates to XML

Postby fabiantheblind » Fri Apr 08, 2011 8:40 am

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/ )
[url]
http://morris-photographics.com/photosh ... 01-8-1.jsx[/url]

Especially the functions

Code: Select all
function 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 all
checkForLayerset();

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],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
// fabiantheblind
// http://www.the-moron.net/
fabiantheblind
 
Posts: 6
Joined: Thu Dec 10, 2009 7:49 pm

Re: Export Layer Coordinates to XML

Postby TZ1 » Fri Apr 08, 2011 6:57 pm

Thanks, will give this a try!
TZ1
 
Posts: 2
Joined: Thu Apr 07, 2011 2:35 pm

Re: Export Layer Coordinates to XML

Postby charliebgostudios » Tue Aug 05, 2014 3:55 pm

Great script, I modified it a bit to export layer sets as well as layer names to a json format instead :

Code: Select all
{
   "layout":[
      {
         "group" : "UI",
         "contents" : [
            {
               "name" : "messageBox",
               "position" : "493, 572",
               "size" : "326, 19"
            },{
               "group" : "buttons",
               "contents" : [
                  {
                     "name" : "GoButton",
                     "position" : "337, 572",
                     "size" : "154, 19"
                  },{
                     "name" : "StopButton",
                     "position" : "250, 572",
                     "size" : "82, 19"
                  },{
                     "name" : "QuitButton",
                     "position" : "826, 596",
                     "size" : "129, 68"
                  },{
                     "name" : "DanceSpinButton",
                     "position" : "826, 572",
                     "size" : "130, 19"
                  }
               ]
            }
         ]
      }
   ]
}


It nests layers into groups based on layer sets which may or may not be useful!
Attachments
ExportLayerCoordinatesToJSON.zip
(2.15 KiB) Downloaded 43 times
charliebgostudios
 
Posts: 1
Joined: Mon Aug 04, 2014 1:47 pm

Re: Export Layer Coordinates to XML

Postby mraj32 » Tue Sep 09, 2014 5:34 am

Hello

I try the script but not able to getting true height and width.
When i try to extract the layer position i am not getting extract position from psd. can you help me getting correct data from psd. attached is the value expected'default text layer' as 43, 60.77 but i am getting different position. some can help

Thanks
Rajesh
mraj32
 
Posts: 2
Joined: Mon Sep 08, 2014 10:42 am


Return to Photoshop Scripts

Who is online

Users browsing this forum: No registered users and 2 guests