Skip to main content
March 25, 2015
Question

layer.bounds error

  • March 25, 2015
  • 2 replies
  • 657 views

Hi all,

I'm trying to export layer centers to an xml.

the following code works fine for all cases, except one.

var curr_layer = app.activeDocument.activeLayer;

var layer_bounds_centerX = curr_layer.bounds[0].value;

var layer_bounds_centerY = curr_layer.bounds[1].value;           

alert("Layer Bounds  : (" + layer_bounds_centerX + " ," + layer_bounds_centerY + " )");

in that special case the bounds are upside down in the y direction.

meaning : doc size is 6000x4500

instead of having the layer_bounds_centerY at around 4000 (bottom down texture)

it shows  layer_bounds_centerY to be 500.  (it should be 4500 - 500 )

the layer_bounds_centerX is correct.

anyone know why this happen?

Thanks!

p.s :

how to attach the psd file?

thanks!

This topic has been closed for replies.

2 replies

matias.kiviniemi
Legend
March 27, 2015

Can you post a PSD that replicates issue? Like JJ said, bounds is an array of left, top, right, bottom, you have to calculate center (your original script just used [0], [1]). I have not experienced issues with bounds showing wrong values, but that is not to say it could not happen as a per-PSD issue. One thing to try is to create new PSD and copy content and see if that changes result. Another thing you can try is using explicit .value.as("px"), there were some situations where setting .rulerUnits = Units.PIXELS did not fix the default conversion.

JJMack
Community Expert
Community Expert
March 27, 2015

To get the center use my script cut the width and height in half and add the values to the top left x and y.  The result may not be visible over the canvas in my example the blue layer is off canvas.

JJMack
JJMack
Community Expert
Community Expert
March 25, 2015

Photoshop supports any size layer. Layers may well not be canvas size.  A layered document may have many layers with different sizes and aspect ratios.  Perhaps the will help. You may be after the document size the canvas width and height the doc width and height.

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits;

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

var LB = activeDocument.activeLayer.bounds;

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

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

alert("Top Left " +  LB[0].value + " , " +  LB[1].value + " Bottom Right "  +  LB[2].value + " , " +  LB[3].value );

// Return the app preferences

app.preferences.rulerUnits = startRulerUnits;

JJMack
March 26, 2015

Hi JJMack,

Thanks for the quick reply!

I'm already doing :

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

May I send you the PS file somehow?

JJMack
Community Expert
Community Expert
March 26, 2015

Ok,

So my current situation is that I have a psd file that doesn't respond to layer.bounds[0].value as others do.

instead it returns a correct value for a upside down version in the y-axis.

Thanks for any help.


I find that hard to believe.  I added the layer name to my alert.  Bounds is an array of four numbers I have always see numbers positive negative and zero.  The  documents upper left corner is always x,y is always 0,0  anr the canvas bottom right is width,heigh.

I created a PSD with various size layers one being a background layer so that one will be canvas size.  I ran my script when each layer was the active layer and captured the screen.   I only see numbers for LB[0] the top left x value.

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// Save the current preferences

var startRulerUnits = app.preferences.rulerUnits;

// Set Photoshop to use pixels

app.preferences.rulerUnits = Units.PIXELS;

var LB = activeDocument.activeLayer.bounds;

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

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

alert("'" + activeDocument.activeLayer.name + "' Layer Bounds\nTop Left " +  LB[0].value + "," +  LB[1].value + "   Bottom Right "  +  LB[2].value + "," +  LB[3].value  + "\nWidth " +  LWidth + "px   Height " + LHeight +"px" );

// Return the app preferences

app.preferences.rulerUnits = startRulerUnits;

JJMack