Skip to main content
PerrowPhoto
Participating Frequently
November 27, 2020
Answered

How to get the width/height of a layer in a script

  • November 27, 2020
  • 3 replies
  • 5371 views

Hello all, I am trying to make a small script that resizes a layer (shrinking) to fit the canvas/image size... I am trying to do this by querying both the document width/height then comparing it to the layer width/height, but I cannot find any way to find the layer width/height... any suggestions? I have tried the code below but it brings back an 'undefined'.

app.preferences.rulerUnits = Units.PIXELS;
var docHeight = app.activeDocument.height;
//alert(docHeight);

var docWidth = app.activeDocument.width;
//alert(docWidth);

var layerWidth = app.activeDocument.activeLayer.width;
alert(layerWidth);
//this line above returns the 'undefined"

 How can I get the activeLayer width and height?

 

Thanks!

This topic has been closed for replies.
Correct answer c.pfaffenbichler
var theBounds = app.activeDocument.activeLayer.bounds;
var layerWidth = theBounds[2] - theBounds[0];
var layerHeight = theBounds[3] - theBounds[1];

3 replies

Stephen Marsh
Community Expert
Community Expert
November 27, 2020

 

 

+ - - - - - - - 1 - - - - - - - +
|                               |
|                               |
|                               |
0                               2
|                               |
|                               |
|                               |
+ - - - - - - - 3 - - - - - - - +

 

 

 

Thanks SuperMerlin!

 

SuperMerlin
Inspiring
November 27, 2020

It's Left, Top, Right and Bottom

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
November 27, 2020
var theBounds = app.activeDocument.activeLayer.bounds;
var layerWidth = theBounds[2] - theBounds[0];
var layerHeight = theBounds[3] - theBounds[1];
Mylenium
Legend
November 27, 2020

It's layer.bounds. I'm not into PS scripting, but I know at least that much. Hope it helps.

 

Mylenium