Photoshop: How to get selection from layer mask by Index without activating the layer by script?

  • 1
  • Question
  • Updated 3 months ago
  • (Edited)
I'm trying to get a selection from a layer mask without activating the proper layer.
My starting point is this code, but it's not working properly.
If there is any active layer with mask, the script works, otherwise, it returns an error.
Is this possible?
function getSelectionFromLayerMask(idx) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel")); desc.putReference(charIDToTypeID("null"), ref); var ref1 = new ActionReference(); ref1.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Msk ")); ref1.putIndex(charIDToTypeID("Lyr "), idx); desc.putReference( charIDToTypeID("T "), ref1); executeAction(charIDToTypeID("setd"), desc, DialogModes.NO)}
Photo of Daniel Sian

Daniel Sian

  • 12 Posts
  • 3 Reply Likes

Posted 3 months ago

  • 1
Photo of Daniel Sian

Daniel Sian

  • 12 Posts
  • 3 Reply Likes

The solution I've found so far is getting the layer mask by ID. It's working perfectly without any layer pre-activated.
I'm getting the ID with:

stringIDToTypeID('layerID')


This is my updated working version:

function getSelectionFromLayerMask(ID) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
    desc.putReference(charIDToTypeID("null"), ref);
    var ref1 = new ActionReference();
    ref1.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Msk "));
    ref1.putIdentifier(charIDToTypeID("Lyr "), ID);
    desc.putReference( charIDToTypeID("T   "), ref1);
    executeAction(charIDToTypeID("setd"), desc, DialogModes.NO) 
}