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

How to get the dimensions of multiple selected layers?

Contributor ,
Sep 12, 2025 Sep 12, 2025

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, everyone1.jpg

TOPICS
Actions and scripting
191
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 , Sep 12, 2025 Sep 12, 2025

Screenshot 2025-09-12 at 16.11.21.png

// 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
...
Translate
Adobe
Community Expert ,
Sep 12, 2025 Sep 12, 2025

Screenshot 2025-09-12 at 16.11.21.png

// 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;
};
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 ,
Sep 12, 2025 Sep 12, 2025

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

22.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 ,
Sep 12, 2025 Sep 12, 2025

Sorry for the misunderstanding, I edited the code in the previous post. 

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 ,
Sep 12, 2025 Sep 12, 2025

Still, thank you for the code you provided

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 ,
Sep 12, 2025 Sep 12, 2025

@w30290083o9nn 

 

Does the updated code from @c.pfaffenbichler now provide the correct answer?

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 ,
Sep 12, 2025 Sep 12, 2025

Sorry, I don't have it yet. I am still looking for a solution, 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 ,
Sep 12, 2025 Sep 12, 2025

I edited the code, please test the new version.

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 ,
Sep 12, 2025 Sep 12, 2025
LATEST
Thank you very much. The code worked successfully and effectively. I appreciate your selfless dedication and salute you. 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