Skip to main content
microbians
Known Participant
October 29, 2021
Question

Photoshop scripting: Real pixel size of a layer

  • October 29, 2021
  • 2 replies
  • 835 views

This function alows to get the real size of a layer, for example if you had several layers inside a group and some are invisible, this will not take in the consideration for the size, or the case of a text paragraph which the text area can have two line of text, but is fill only with one, will return the size of one line, not the complete textarea.

 

function getMinimunBoundOfVisibleLayers(layer, bounds) {

    var merged  = layer.duplicate().merge();
    var bounds  = Object.clone(merged.bounds);
    merged.remove();

    return bounds;

}

 

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
October 29, 2021

I do not understand you seem to be passing your function a layer object (do you means layerset  object)  for all layerset bounds are the document canvas bounds, You also set to be pasting a bounds that is not used in the function?  What  you seem to be after  is the layer  bounds of all the layers and layerset layers within the passed layerset  merged into a single layer from a duplicated layerset that you remove and you return the removed layers bounds.  What do you use that bounds for? I also do not know javascript so I do  not know what "Object.clone()" does

JJMack
microbians
Known Participant
October 29, 2021

If  group of layer include an non visible object is take it in consideration for the size of the bounding, so is resulting in an incorrect size. There is other case, for example when the layer is a text layer but is a Pratagraph Text, imagine one with space for two lines, but only one is used, the bouding will be the size of two lines, but the visual size that covers will be only one.

 

 

Sorry Object.clone is not necesary, I forget to take it out, because is someting I need only for my script. Is a function that duplicates the object so there is not any more the original one, that will be remove after take the size. Just for security will not loose the data.

 

The Object.clone function is this:

 

    Object.clone = function(obj) {
        if (null == obj || "object" != typeof obj) return obj;
        var copy = obj.constructor();
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
        }
        return copy;
    }

 

 

Also another mistake sorry: first line is 

function getMinimunBoundOfVisibleLayers(layer) 

There is no needs to pass the bounds... I had this there because my script needed, my function on my side have more peculiarities for what I using it.

JJMack
Community Expert
Community Expert
October 29, 2021

If the layer visibility  messes up the results you want  duplicate the layerset.  Process the duplicated layerset recursively to turn all  layers involved visibility on then merge the duplicated layerset to your merged layer to get its bounds. You seem the know why your function does not work the way you want it to because  of layers visibility and paragraph text. Fix your function to work the want you want it to work.  What do you need that bounds to for?

JJMack
Kukurykus
Legend
October 29, 2021

This function is unfunctional or I have no idea how to call it.