Skip to main content
sz20044
Known Participant
March 28, 2019
Answered

How to get bounding box of a layer mask?

  • March 28, 2019
  • 1 reply
  • 4076 views

I am trying to get the bounding box of a layer mask, as well as a group containing layer masks... yet no matter what I do, it always returns the full size of the canvas 0,0 to max.  Is there a way to do this via javascript?  I would think Photoshop is smart enough to spit this back, considering I can scale a layer mask and the mask portion is scaled, not the entire canvas size.

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

I appreciate the help.  Sorry guys, I am not an avid Photoshop or Javascript expert here in scripting.

I have tried your selection script and it errors out if I fill in the mask channel name.  I have also tried just RGB and I get the same error:

Error: Illegal Argument

Tried:

selection_from_channel("RGB");

//selection_from_channel("Msk ");

and

selection_from_channel("HL Mask");      <--- Name of the Layer Mask Channel

//selection_from_channel("Msk ");


sz20044 

I appreciate the help.  Sorry guys, I am not an avid Photoshop or Javascript expert here in scripting.

I have tried your selection script and it errors out if I fill in the mask channel name.  I have also tried just RGB and I get the same error:

Error: Illegal Argument

Tried:

selection_from_channel("RGB");

//selection_from_channel("Msk ");

and

selection_from_channel("HL Mask");      <--- Name of the Layer Mask Channel

//selection_from_channel("Msk ");

This is not a channel name, but a four-character CharID for standard channels.

You had to do it that way.

if (selection_from_channel("Msk "))

    {

    try { var b = activeDocument.selection.bounds; } catch(e) { alert("No selection");  }

    alert(b);

    }

function selection_from_channel(ch_name)

    {

    try {

        var d = new ActionDescriptor();

        var r1 = new ActionReference();

        var r2 = new ActionReference();

        r1.putProperty(charIDToTypeID("Chnl" ), charIDToTypeID("fsel"));

        r2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID(ch_name));

        d.putReference(charIDToTypeID("null"), r1);

        d.putReference(charIDToTypeID("T   "), r2);

        executeAction(charIDToTypeID("setd"), d, DialogModes.NO);

        return true;

        }

    catch(e) { alert("No mask"); return false; }

    }


You can rewrite the function to use more understandable StringID

if (selection_from_channel("mask"))

    {

    try { var b = activeDocument.selection.bounds; } catch(e) { alert("No selection");  }

    alert(b);

    }

function selection_from_channel(ch_name)

    {

    try {

        var d = new ActionDescriptor();

        var r1 = new ActionReference();

        var r2 = new ActionReference();

        r1.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));

        r2.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID(ch_name));

        d.putReference(stringIDToTypeID("null"), r1);

        d.putReference(stringIDToTypeID("to"), r2);

        executeAction(stringIDToTypeID("set"), d, DialogModes.NO);

        return true;

        }

    catch(e) { alert("No mask"); return false; }

    }

1 reply

Legend
March 28, 2019

1. Convert Mask channel to selection

2. Use selection.bounds

Legend
March 29, 2019

Selection from any channel: Re: Selecting custom pixels

Legend
March 30, 2019

How do I get the layer_id?  What's the call to get that?


var layer1 = activeDocument.activeLayer

var layer2 = activeDocument.layers[0]

alert(layer1.id)

alert(layer2.id)