Skip to main content
Participant
September 17, 2018
Answered

Photoshop artboard sizes

  • September 17, 2018
  • 2 replies
  • 2297 views

Hi there,

I have multiple artboards within a PSD file, and I need to get the width and height of all the artboards programmatically.

It seems artboards in Photoshop are generic LayerSets, but I can't seem to find a straight-forward way to get the width and height of individual layerset, so I tried doing calculations using the bounds like so:

artboardWidth = artboard.bounds[2] - artboard.bounds[0];

artboardHeight = artboard.bounds[3] - artboard.bounds[1];

However, it seems the bounds array doesn't work well when there are large images that extend beyond the size of the artboard, even with clipping mask - i.e. bounds calculate the max width and height of all elements inside the artboard.

Can anyone advise how I could get the artboard dimensions?

Thanks so much!

This topic has been closed for replies.
Correct answer r-bin

Actually r-bin , your solution works! But the order of the bound array elements seems to be out of sort. :/

Originally, you suggested doing something like this:

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top"));

bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left"));

bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));

bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));

artboardWidth = bounds[2]-bounds[0];  

But the calculation should be:

artboardWidth = bounds[2]-bounds[1];  
artboardHeight = bounds[3] - bounds[0];

Thanks so much!


Yes, I was wrong. Did on the fast.
Fix it everywhere

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top")); 
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left")); 

to

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left")); 
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top")); 


2 replies

Legend
September 18, 2018

var layer = activeDocument.layers[0];

//var layer = activeDocument.activeLayer;

var lb = layer_bounds(layer);

var ab = artboard_rectangle(layer);

alert("DOM width = " + (activeDocument.activeLayer.bounds[2]-activeDocument.activeLayer.bounds[0]) + "\n\n"+

      "AM layer width in px = " + (lb[2]-lb[0]) + "\n\n"+

      "AM artboardr width in px = " + (ab[2]-ab[0]));

/////////////////////////////////////////////////////////////////////////////////

function layer_bounds(layer, no_effects)

    {

    try {       

        var prop = stringIDToTypeID("bounds");

        if (no_effects == true)  prop = stringIDToTypeID("boundsNoEffects");

        if (no_effects == false) prop = stringIDToTypeID("boundsNoMask");

        var r = new ActionReference();   

        r.putProperty(stringIDToTypeID("property"), prop);

        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);

        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

   

        var d = executeActionGet(r).getObjectValue(prop);

   

        var bounds = new Array();

   

        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top"));

        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left"));

        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));

        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));

       

        return bounds;

        }

    catch(e) { alert(e); }

    }

/////////////////////////////////////////////////////////////////////////////////

function artboard_rectangle(layer)

    {

    try {       

        var r    = new ActionReference();   

        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("artboard"));

        if (layer) r.putIdentifier(stringIDToTypeID("layer"), layer.id);

        else       r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        var d = executeActionGet(r).getObjectValue(stringIDToTypeID("artboard")).getObjectValue(stringIDToTypeID("artboardRect"));

        var bounds = new Array();

        bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top"));

        bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left"));

        bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));

        bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));

                                 

        return bounds;

        }

    catch(e) { alert(e); }

    }

Participant
September 18, 2018

Thanks r-bin - I tried the functions but unfortunately, they are not calculating the correct values either :/

I tried both passing in:

1) app.activeDocument.layerSets[0]

2) app.activeDocument.layers[0]

Into both the artboard_rectangle and layer_bounds functions, but the numbers returned from the subsequent bounds array calculations are not correct.

r-binCorrect answer
Legend
September 18, 2018

Actually r-bin , your solution works! But the order of the bound array elements seems to be out of sort. :/

Originally, you suggested doing something like this:

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top"));

bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left"));

bounds[2] = d.getUnitDoubleValue(stringIDToTypeID("right"));

bounds[3] = d.getUnitDoubleValue(stringIDToTypeID("bottom"));

artboardWidth = bounds[2]-bounds[0];  

But the calculation should be:

artboardWidth = bounds[2]-bounds[1];  
artboardHeight = bounds[3] - bounds[0];

Thanks so much!


Yes, I was wrong. Did on the fast.
Fix it everywhere

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("top")); 
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("left")); 

to

bounds[0] = d.getUnitDoubleValue(stringIDToTypeID("left")); 
bounds[1] = d.getUnitDoubleValue(stringIDToTypeID("top")); 


Inspiring
September 18, 2018

A very dirty and long way could be to use the clipboard and make a temporary new document for each document's layer.

Something like:

for each layer

     if ((thisLayer.typename != 'LayerSet') and (this layer's kind is "NORMAL")) then // only "regular" artLayer

          select this layer

          copy this layer

          make new document

          paste

          store the new document dimensions

          close the new document without saving

     end if

end loop

Hope some jsx' masters will find a better solution

Participant
September 18, 2018

Thanks fredericm80874937! Yea I probably need a bit of a cleaner solution because I'm distributing the script to designers and would want it to be as lean as possible so it won't explode their computers