Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
You may post any question and code here so other can also benefit.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Matias,
Thanks for the assistant,
what is the best way to upload a psd file here?
Copy link to clipboard
Copied
You can not upload a PSD to this site. You need to upload it to a file sharing site like dropbox and then post a link here to the uploaded file's download link.
// 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 + "X," + LB[1].value + "Y Bottom Right " + LB[2].value + "X," + LB[3].value
+ "Y\nWidth " + LWidth + "px Height " + LHeight +"px"
+ "\nLayer center relative to canvas " + (LB[0].value + LWidth/2) + "X," + (LB[1].value + LHeight/2) +"Y" );
// Return the app preferences
app.preferences.rulerUnits = startRulerUnits;