Copy link to clipboard
Copied
How can I get the width and height of multiple selected layers? For example, the width and height visible in the Info panel during Free Transform.Could you please advise? Thank you, everyone
// edited
// 2025, use it at your own risk;
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theLayers = collectSelectedLayersBounds ();
var x1 = theLayers[0][2][0];
var x2 = theLayers[0][2][2];
var y1 = theLayers[0][2][1];
var y2 = theLayers[0][2][3];
for (var m = 1; m < theLayers.length; m++) {
var x1 = Math.min(x1, theLayers[m][2][0]);
var x2 = Math.max(x2, theLayers[m][2][2]);
var y1 = Math.min(y1, theLayer
...
Copy link to clipboard
Copied
// edited
// 2025, use it at your own risk;
if (app.documents.length > 0) {
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theLayers = collectSelectedLayersBounds ();
var x1 = theLayers[0][2][0];
var x2 = theLayers[0][2][2];
var y1 = theLayers[0][2][1];
var y2 = theLayers[0][2][3];
for (var m = 1; m < theLayers.length; m++) {
var x1 = Math.min(x1, theLayers[m][2][0]);
var x2 = Math.max(x2, theLayers[m][2][2]);
var y1 = Math.min(y1, theLayers[m][2][1]);
var y2 = Math.max(y2, theLayers[m][2][3]);
};
alert ("selected layers\n"+(x2-x1)+"\n"+(y2-y1));
app.preferences.rulerUnits = originalRulerUnits;
};
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers.push([theName, theIdentifier, theseBounds]);
//selectedLayers.push([theName, theseBounds[2] - theseBounds[0], theseBounds[3] - theseBounds[1]])
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers = [[theName, theIdentifier, theseBounds]]
//selectedLayers = [[theName, theseBounds[2] - theseBounds[0], theseBounds[3] - theseBounds[1]]]
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};
Copy link to clipboard
Copied
Thank you for your reply. I don't need to iterate through the dimensions of all the selected layers, but rather the total width and height of the currently selected layers
Copy link to clipboard
Copied
Sorry for the misunderstanding, I edited the code in the previous post.
Copy link to clipboard
Copied
Still, thank you for the code you provided
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Sorry, I don't have it yet. I am still looking for a solution, thank you.
Copy link to clipboard
Copied
I edited the code, please test the new version.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now