Skip to main content
marior90336416
Inspiring
December 10, 2019
Answered

Exporting layers with coordinates from layer center

  • December 10, 2019
  • 4 replies
  • 3196 views

Hello, 

I've found a couple scripts that works exporting layers with coordinates. They work great, but with layer 0,0. I've found this topic here 

https://community.adobe.com/t5/photoshop/export-layer-coordinates-layer-width-and-height/td-p/9182055

and implemented the variables 

var LB = activeDocument.activeLayer.bounds;

var LWidth = (LB[2].value) - (LB[0].value);

var LHeight = (LB[3].value) - (LB[1].value)

But it looks like it gets the bottom corner of the layer. How to get it from the center? I'm testing a 512x512 image with two 128x128 image side by side. It should return 64x64 from the first square.

 

here is the code I'm using

 

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;


// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;


// Define varibles for x and y of layers
var LB = activeDocument.activeLayer.bounds;
var x = (LB[2].value) - (LB[0].value);
var y = (LB[3].value) - (LB[1].value);
var coords = "";

// Loop to iterate through all layers

function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    x = (LB[2].value) - (LB[0].value);
    y = (LB[3].value) - (LB[1].value);
    coords += layerRef.name + ": " + x + "x" + "," + y + "y" + "\n";

//test if it's a layer set

    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set

function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to

var FPath = Folder.selectDialog("Save exported coordinates to");

// Detect line feed type

if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt file

function writeFile(info) {
  try {
    var f = new File(FPath + "/" + docName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions

recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits;

// Set preferences back to user's defaults

writeFile(coords);

// Show results

if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + 
docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}

. any help is greatly appreciated. Thanks 

This topic has been closed for replies.
Correct answer Chuck Uebele

Okay, I finally had a chance to look at your script, and you forgot to put in the parentheses. you have:

var x = layerRef.bounds[0]+layerRef.bounds[2]/2;

When you should have:

var x = (layerRef.bounds[0]+layerRef.bounds[2])/2;

So you need to divide the sum of the two layer bounds by 2. You're just dividing the last layer bounds coordinate by 2. You did this on all the reference points, not jus the one I listed here.

4 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
December 26, 2019

Okay, I finally had a chance to look at your script, and you forgot to put in the parentheses. you have:

var x = layerRef.bounds[0]+layerRef.bounds[2]/2;

When you should have:

var x = (layerRef.bounds[0]+layerRef.bounds[2])/2;

So you need to divide the sum of the two layer bounds by 2. You're just dividing the last layer bounds coordinate by 2. You did this on all the reference points, not jus the one I listed here.

marior90336416
Inspiring
December 26, 2019

Works perfectly, thanks!

Chuck Uebele
Community Expert
Community Expert
December 24, 2019

Is the orange square the only thing on that layer?

marior90336416
Inspiring
December 24, 2019

Yes, I Ctrl+T'ed the layer, the boundaries are just around the shape.

marior90336416
Inspiring
December 10, 2019

Many thanks it works! I just had to change layer.bounds to layerRef.bound 

x = layerRef.bounds[0]+layerRef.bounds[2]/2;
y = layerRef.bounds[1]+layerRef.bounds[3]/2;
Chuck Uebele
Community Expert
Community Expert
December 10, 2019

Yea, typing on my phone, which is hard with code. Made it simple.

marior90336416
Inspiring
December 24, 2019

Sorry to reopen, it looks like the script isn't getting it right on this case. I'm using a PSD of 1366 by 768px.

So the script is writing down that the orange square coordinates are 640x448 (where the white crosshair is), instead of the center of the square (which is located at 448x320). The code is below:

 

// Bring application forward
app.bringToFront();

// Set active Document variable and decode name for output
var docRef = app.activeDocument;
var docName = decodeURI(activeDocument.name);

// Define pixels as unit of measurement
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// Define variable for the number of layers in the active document
var layerNum = app.activeDocument.artLayers.length;


// Define variable for the active layer in the active document
var layerRef = app.activeDocument.activeLayer;


// Define varibles for x and y of layers
var LB = activeDocument.activeLayer.bounds;
var x = layerRef.bounds[0]+layerRef.bounds[2]/2;
var y = layerRef.bounds[1]+layerRef.bounds[3]/2;
var coords = "";

// Loop to iterate through all layers

function recurseLayers(currLayers) {
  for ( var i = 0; i < currLayers.layers.length; i++ ) {
    layerRef = currLayers.layers[i];
    x = layerRef.bounds[0]+layerRef.bounds[2]/2;
    y = layerRef.bounds[1]+layerRef.bounds[3]/2;
    coords += layerRef.name + ": " + x + "x" + "," + y + "y" + "\n";

//test if it's a layer set

    if ( isLayerSet(currLayers.layers[i]) ) {
      recurseLayers(currLayers.layers[i]);
    }
  }
}

//a test for a layer set

function isLayerSet(layer) {
  try {
    if ( layer.layers.length > 0 ) {
      return true;
    }
  }

  catch(err) {
    return false;
  }
}

// Ask the user for the folder to export to

var FPath = Folder.selectDialog("Save exported coordinates to");

// Detect line feed type

if ( $.os.search(/windows/i) !== -1 ) {
  fileLineFeed = "Windows";
}
else {
  fileLineFeed = "Macintosh";
}

// Export to txt file

function writeFile(info) {
  try {
    var f = new File(FPath + "/" + docName + ".txt");
    f.remove();
    f.open('a');
    f.lineFeed = fileLineFeed;
    f.write(info);
    f.close();
  }
  catch(e){}
}

// Run the functions

recurseLayers(docRef);
preferences.rulerUnits = defaultRulerUnits;

// Set preferences back to user's defaults

writeFile(coords);

// Show results

if ( FPath == null ) {
  alert("Export aborted", "Canceled");
}
else {
  alert("Exported " + layerNum + " layer's coordinates to " + FPath + "/" + 
docName + ".txt " + "using " + fileLineFeed + " line feeds.", "Success!");
}
Chuck Uebele
Community Expert
Community Expert
December 10, 2019

To get the center:

x=(layer.bounds[0]+layer.bound[2])/2;

y=(layer.bounds[1]+layer.bound[3])/2;