Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Photoshop scripting: Real pixel size of a layer

Community Beginner ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

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;

}

 

TOPICS
Actions and scripting

Views

96

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

It's not that, I don't need to do that resourceve process which would indeed be very slow, it's much faster, to create a destroy a merge layer, which results in the correct size. That's why I do it this way, it's much faster. I don't need the invisible layers to be visible, I need them to be as they are, but to be able to take the correct size when some elements within a group are invisible or not. This is because my script (not this function) is much bigger and does more things, basically it has commands that activate or deactivate the visibility of layers according to some rules.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

If your function is processing all the layers in the layerset turning visibility's on and off could is not also note the lowest and highest  x and y bounds vales in the group of layers to produce the four valuses for the bounds.

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

Previewsly I had a function as you say, it recursive going throw the layers making a bounding that grow only taking in consideration the visible layers, but it turns that compared with that functons is 5 time slower.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

Scripts are slow they are not compile into optimized machine code each line needs to be interpreted into machine code. there is no Photoshop script compiler.

JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 29, 2021 Oct 29, 2021

Copy link to clipboard

Copied

LATEST

All we know that

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines