Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get the real size of the current layer

Contributor ,
Oct 02, 2025 Oct 02, 2025
var layer = app.activeDocument.activeLayer;
var Width = layer.bounds[2] - layer.bounds[0];
var Height = layer.bounds[3] - layer.bounds[1];
alert(Width + "\n" + Height)

The dimensions obtained by the above code are 207 and 765, but the actual dimensions are 567 and 850, because the image contains transparent areas. How can I get the actual dimensions? Thank you, everyone
01.jpg

 

TOPICS
Actions and scripting
205
Translate
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

correct answers 1 Correct answer

Community Expert , Oct 02, 2025 Oct 02, 2025
quote

Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.


By @w30290083o9nn

 

Having an embedded PNG complicates the layer or channel based hacks that I would employ when using a PSD/PSB.

 

...
Translate
Adobe
Community Expert ,
Oct 02, 2025 Oct 02, 2025

Is the smart object raster? If so, when you edit it - what is the canvas size?

 

P.S. You should also explicitly work in pixels, either directly or temporarily setting ruler units before getting the bounds, just in case the current ruler units change.

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

The current layer is a smart object layer, not rasterized.

The ruler is in pixel units

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025
quote

The current layer is a smart object layer, not rasterized.

 

I can see that, but is it a raster object that Photoshop can edit/open, or is it a vector object that would be opened in an external editor such as Illustrator?

 

 

quote

The ruler is in pixel units


By @w30290083o9nn

 

My point was that this shouldn't be assumed. I don't know if you only posted a snippet of code, are you accounting for this or not?

 

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

It is the width and height displayed when the current layer is in free transform.

00.jpg

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025
quote

Is the smart object raster? If so, when you edit it - what is the canvas size?


By @Stephen Marsh

 

 

I was referring to something like this:

 

#target photoshop

if (app.documents.length) {
    var lyr = app.activeDocument.activeLayer;
    if (lyr.kind == LayerKind.SMARTOBJECT) {
        var result = getSmartObjectCanvasBounds(lyr);
        alert("Smart object canvas size: " + result.width + " - " + result.height + " px");
    } else {
        alert("Active layer is not a smart object.");
    }
}

function getSmartObjectCanvasBounds(layer) {
    if (layer.kind != LayerKind.SMARTOBJECT) {
        throw new Error("Selected layer is not a smart object");
    }

    // Remember current doc
    var mainDoc = app.activeDocument;

    // Edit contents (opens PSB)
    executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO);
    smartObjDoc = app.activeDocument;

    // Get the full canvas bounds, explicitly in pixels
    var w = smartObjDoc.width.as("px");
    var h = smartObjDoc.height.as("px");

    // Close without saving, return to main doc
    smartObjDoc.close(SaveOptions.DONOTSAVECHANGES);
    app.activeDocument = mainDoc;

    return { width: w, height: h, bounds: [0, 0, w, h] };
}

 

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

I want to get the current smart object layer's size in the current document, not the original size inside the smart object. If it's about getting the size inside the smart object, the code below can do that.Still, thank you for providing the code, but it's not what I was looking for. I really appreciate it

#target photoshop
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
var activeBounds = app.activeDocument.activeLayer.bounds;
app.activeDocument.close();
alert(activeBounds);

 

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025
quote

I want to get the current smart object layer's size in the current document, not the original size inside the smart object. If it's about getting the size inside the smart object, the code below can do that.Still, thank you for providing the code, but it's not what I was looking for. I really appreciate it

#target photoshop
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
var activeBounds = app.activeDocument.activeLayer.bounds;
app.activeDocument.close();
alert(activeBounds);

 


By @w30290083o9nn

 

This code incorrectly gets the layer bounds, excluding the transparency and doesn't take SO scaling into account. 

 

Presuming that the SO has not been scaled,  to get the canvas size you need to do what my previous script shows, which is the same size as what the transform of the smart object shows.

 

However, if the smart object has been scaled, then I believe that you need to do something else.

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025
quote

Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.


By @w30290083o9nn

 

Having an embedded PNG complicates the layer or channel based hacks that I would employ when using a PSD/PSB.

 

AFAIK, there are only hacks, as this isn't covered by the DOM and I am unaware of AM alternatives.

 

Good luck!

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

Okay, thank you

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025

If you're willing to use the script to convert the embedded PNG to PSB, then the hacks I have in mind can be used.

Translate
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
Contributor ,
Oct 02, 2025 Oct 02, 2025

In fact, my current document is already a PSB file. Since the embedded PNG contains transparent pixels, if I rasterize it, the free transform will cause blurring. Therefore, I have to look for a way to get the actual dimensions of the smart object layer as displayed in the current document.

Translate
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 Expert ,
Oct 02, 2025 Oct 02, 2025
quote

In fact, my current document is already a PSB file. Since the embedded PNG contains transparent pixels, if I rasterize it, the free transform will cause blurring. Therefore, I have to look for a way to get the actual dimensions of the smart object layer as displayed in the current document.


By @w30290083o9nn

 

You misunderstood.

 

As PNG can't use layers (or alpha channels), the hack that I have in mind can't be used.

 

The previous linked topic was to a script that I wrote that will automatically create an embedded PSB smart object from a JPEG, PNG etc. It is not rastrerized or otherwise changed from the original apart from the file format now being PSB.

 

The hack that I have in mind would go like:

... Your existing code ...

1) Select embedded PNG layer

2) Convert to embedded PSB via my code

3) Edit the PSB, add a temporary solid fill layer (1% opacity), save, close

4) Get the layer bounds, which now contain no transparency and will now report the correct bounds, store this as a variable

... Your code to resize the layer ...

5) Edit the PSB, remove the temorary solid fill layer, save, close

... Your code carries on doing what it does ...

 

Or something like that.

Translate
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
Contributor ,
Oct 03, 2025 Oct 03, 2025

I roughly understand your point. Editing the PSB, adding a temporary solid fill layer, saving, closing, and then getting the current layer's size is obviously feasible. However, after getting the size, it would require entering the smart object again to delete the temporary fill layer and then save the smart object, which significantly increases the script's runtime. Nevertheless, the method is feasible, and I really appreciate your suggestion. Thank you.

Translate
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 Expert ,
Oct 03, 2025 Oct 03, 2025

Yes, it's all a hack, it's all we have unless someone has a better idea or esoteric action manager code.

Translate
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 Expert ,
Oct 03, 2025 Oct 03, 2025

@w30290083o9nn 

 

The edited canvas size of the original smart object, including transparency padding (500x500px):

edited-so.png

 

Using transform, the scaled placed size, now 259px rather than the original 500px total size:

scaled-so-transform.png

 

The script correctly reports the same scaled 259 total pixel size, not 500px:

script-report.png

 

I think this is code better than my previous suggestions, you were right to look at layer bounds, but not by opening the SO, just checking the bounds of the active layer. This includes transparency and includes the scaled size. So much simpler than my previous suggestions.

 

#target photoshop

// Get the currently selected layer
var layer = app.activeDocument.activeLayer;

// Check if the layer is a Smart Object
if (layer.kind == LayerKind.SMARTOBJECT) {
    // Get the bounds of the Smart Object layer (left, top, right, bottom)
    var layerBounds = layer.bounds;

    // Calculate the width and height of the Smart Object layer
    var layerWidth = layerBounds[2].as("px") - layerBounds[0].as("px"); // right - left
    var layerHeight = layerBounds[3].as("px") - layerBounds[1].as("px"); // bottom - top

    // Display the dimensions
    alert("Smart Object Dimensions (including transparency):\nWidth: " + layerWidth + " px" + " \nHeight: " + layerHeight + " px");
} else {
    alert("The selected layer is not a Smart Object.");
}

 

Translate
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
Contributor ,
Oct 03, 2025 Oct 03, 2025
LATEST

Thank you for providing the code. Why is the size not accurate when I use this code? Under free transform, the width of this layer is 567 and the height is 850, but the script retrieves the width as 207 and the height as 765002.jpg001.jpg

Translate
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