Copy link to clipboard
Copied
I use a script to export a layer as a png. This png does not contain transparent areas. how can I get the bounds of the png relative to the canvas.
function quickExportPng() {
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
d.putString(stringIDToTypeID("fileType"), "png");
d.putInteger(stringIDToTypeID("quality"), 32);
d.putInteger(stringIDToTypeID("metadata"), 0);
d.putString(stringIDToTypeID("destFolder"), exportDir);
d.putBoolean(stringIDToTypeID("sRGB"), true);
d.putBoolean(stringIDToTypeID("openWindow"), false);
executeAction(stringIDToTypeID("exportSelectionAsFileTypePressed"), d, DialogModes.NO);
}
The red rectangle is the exported png content,The popup box is the bounds of the layer.
Press ctrl+T on this layer to display the actual content and position of this layer.
Copy link to clipboard
Copied
Please post the Script in question, the one you use to get the alert of the Bounds.
Replacing negative values with 0 or values larger than the width/height with those would not be sufficient, so intersecting the transparency with the canvas would seem like an option.
Copy link to clipboard
Copied
The script I posted is just a script for quickly exporting PNG. I don’t have a script that solves the problem.
Copy link to clipboard
Copied
What problem?
The png gets exported correctly, right?
Copy link to clipboard
Copied
png has been exported correctly, I want to get the bounds of png relative to the canvas
Copy link to clipboard
Copied
Please post the Script you used to get the alert of the Bounds (as seen in the first screenshot).
Copy link to clipboard
Copied
if (app.documents.length > 0) {
var doc = app.activeDocument;
if (doc.activeLayer != null) {
var layer = doc.activeLayer;
var bounds = layer.bounds;
var left = bounds[0].as('px');
var top = bounds[1].as('px');
var right = bounds[2].as('px');
var bottom = bounds[3].as('px');
var width = right - left;
var height = bottom - top;
alert("Layer Bounds:\n" +
"Left: " + left + " px\n" +
"Top: " + top + " px\n" +
"Right: " + right + " px\n" +
"Bottom: " + bottom + " px\n" +
"Width: " + width + " px\n" +
"Height: " + height + " px");
} else {
alert("No active layer selected.");
}
} else {
alert("No document open.");
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@li27112570w0z3 – If I am understanding your post (it's not exactly clear to me)...
1) Load layer transparency channel as a selection
2) Select > Inverse selection
3) Select > Inverse selection
4) Get selection bounds/co-ordinates
5) Deselect
Do you also need the bounds of the layer effects/style, such as drop shadow? That would take more work in ExtendScript. I believe that UXP has made advancements in this area though. Perhaps something like:
1) Dupe layer
2) Rasterize layer style
3) Load layer transparency channel as a selection
4) Select > Inverse selection
5) Select > Inverse selection
6) Get selection bounds/co-ordinates
7) Delete the temp layer
8) Deselect
Copy link to clipboard
Copied
This is too complicated. Is there no way to get the bounds in the "Quick Export Png" operation?
Copy link to clipboard
Copied
And the way you suggest it now, the shadow part is not included.
Copy link to clipboard
Copied
And the way you suggest it now, the shadow part is not included.
By @li27112570w0z3
That is why I updated my OP with the steps to rasterize the layer style.
It's possible to get the document width/height of the exported layer from the saved file, either by opening the image in Photoshop or perhaps by using BridgeTalk to do this via Adobe Bridge without opening the file into Photoshop. You have not provided any context on why you wish to get the bounds of the layer as it will export. Is this for naming the exported file?
Copy link to clipboard
Copied
This is too complicated. Is there no way to get the bounds in the "Quick Export Png" operation?
By @li27112570w0z3
Define "too complicated". :]
Each step is easily scripted.
There may be less complicated ways, this is just what I came up with when first looking at your request. ExtendScript has limitations, which sometimes require "complicated" work-arounds. Note that if you were doing this in UXP it would be "less complicated".
AFAIK Quick Export is not currently exposed to any form of scripting. I believe that it is based on UXP when not in legacy mode, so it may open up in the future for UXP (but probably never for ExtendScript).
Copy link to clipboard
Copied
Thank you very much for your reply. Maybe my needs should not be done in Photoshop. I am making an image editor, using psd as a template. After exporting the layer to png, I want to overlay it on the canvas, so I need to get the relative position of the png.
Copy link to clipboard
Copied
A possible work-around:
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var doc = app.activeDocument;
if (doc.activeLayer != null) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theDup = doc.duplicate ("copy", true);
hideOthers();
var bounds = theDup.activeLayer.bounds;
//var bounds = layer.bounds;
var left = bounds[0].as('px');
var top = bounds[1].as('px');
var right = bounds[2].as('px');
var bottom = bounds[3].as('px');
var width = right - left;
var height = bottom - top;
theDup.close(SaveOptions.DONOTSAVECHANGES);
alert("Layer Bounds:\n" +
"Left: " + left + " px\n" +
"Top: " + top + " px\n" +
"Right: " + right + " px\n" +
"Bottom: " + bottom + " px\n" +
"Width: " + width + " px\n" +
"Height: " + height + " px");
app.preferences.rulerUnits = originalRulerUnits;
} else {
alert("No active layer selected.");
}
} else {
alert("No document open.");
};
////// toggle visibility of others //////
function hideOthers () {
var desc10 = new ActionDescriptor();
var list4 = new ActionList();
var ref7 = new ActionReference();
ref7.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
list4.putReference( ref7 );
desc10.putList( charIDToTypeID( "null" ), list4 );
desc10.putBoolean( charIDToTypeID( "TglO" ), true );
executeAction( charIDToTypeID( "Shw " ), desc10, DialogModes.NO );
};