Skip to main content
Inspiring
September 21, 2024
Answered

Script to add mask of current layer to selection

  • September 21, 2024
  • 2 replies
  • 1218 views

I have a recorded action where parts of it is running scripts in order to achieve what I need.

I now need a piece of script that will add the mask of the current layer to selection. I have tried and tried (with the help of Claude.ai 😁), but cannot get it to work. Can you help me?

This topic has been closed for replies.
Correct answer Stephen Marsh

@Chris201Chris 

 

Notice the difference in your action recording vs mine:

 

 

Record the action as follows:

 

Ensure that the layer with the mask is active/targeted/selected. With a selection active, hold down the Shift key and the Command or Ctrl key and click on the layer mask icon in the Layers panel with the mouse. Stop recording.

 

2 replies

Stephen Marsh
Community Expert
Community Expert
September 21, 2024

@Chris201Chris 

 

As @c.pfaffenbichler writes, an action can do this. Record the action as follows.... With a selection active, hold down the shift key and the cmd or ctrl key and click on the layer mask with the mouse. Stop recording.

 

If you need ExtendScript code, then this should do the job:

 

if (hasLayerMask() == true) {
	addLayerMaskSelToSel();
} else {
	alert("The active layer doesn't have a raster layer mask!");
}


// c.pfaffenbichler from discussions with Mike Hale
function hasLayerMask () {
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
  var desc = executeActionGet(ref);
  return desc.hasKey(charIDToTypeID("UsrM"));
}

function addLayerMaskSelToSel() {
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "add" ), descriptor, DialogModes.NO );
}

 

 

c.pfaffenbichler
Community Expert
Community Expert
September 21, 2024

I assume things might improve but so far when people come to this Forum with ai-generated code it often appears to be pretty bad. 

 

Could you please post screenshots taken at View > 100% with the pertinent Panels (Toolbar, Layers, Channels, Options Bar, …) visible to illustrate what exactly you are trying to automate? 

As far as I can tell loading transparency as a Selection should work even in Actions as should loading the active Layer’s Layer Mask as a Selection. 

Inspiring
September 22, 2024

 I can get it to work with a recorded action, but then I have to manually press ok in this pop-up every time I run my action.

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 22, 2024

@Chris201Chris 

 

Notice the difference in your action recording vs mine:

 

 

Record the action as follows:

 

Ensure that the layer with the mask is active/targeted/selected. With a selection active, hold down the Shift key and the Command or Ctrl key and click on the layer mask icon in the Layers panel with the mouse. Stop recording.