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

Script to add mask of current layer to selection

Contributor ,
Sep 21, 2024 Sep 21, 2024

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting , Windows

Views

446

Translate

Translate

Report

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 2 Correct answers

Community Expert , Sep 21, 2024 Sep 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 hasLayerM
...

Votes

Translate

Translate
Community Expert , Sep 22, 2024 Sep 22, 2024

@Chris201Chris 

 

Notice the difference in your action recording vs mine:

 

sel.png

 

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.

 

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 21, 2024 Sep 21, 2024

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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 22, 2024 Sep 22, 2024

Copy link to clipboard

Copied

 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.

photoshop-layers.JPG

 

Votes

Translate

Translate

Report

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 22, 2024 Sep 22, 2024

Copy link to clipboard

Copied

@Chris201Chris 

 

Notice the difference in your action recording vs mine:

 

sel.png

 

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.

 

Votes

Translate

Translate

Report

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 22, 2024 Sep 22, 2024

Copy link to clipboard

Copied

Many thanks!

 

Votes

Translate

Translate

Report

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 22, 2024 Sep 22, 2024

Copy link to clipboard

Copied

PS - do you know of a "good list" to use for all these shortcuts (where e.g. the shortcut works better than using the menues)? I use them so rarely so keep on forgetting that they exist.... 😊

Votes

Translate

Translate

Report

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 22, 2024 Sep 22, 2024

Copy link to clipboard

Copied

LATEST
quote

PS - do you know of a "good list" to use for all these shortcuts (where e.g. the shortcut works better than using the menues)? I use them so rarely so keep on forgetting that they exist.... 😊


By @Chris201Chris

 

Offhand, no.

 

The Shift key is a commonly used modifier key used to continuously add to an existing selection, not just marching ant selections, but selecting multiple layers in the layers panel as another example.

 

The other common modifier keys are Option/Alt and Command/Ctrl on Mac or Win.

 

 

Votes

Translate

Translate

Report

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 21, 2024 Sep 21, 2024

Copy link to clipboard

Copied

@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 );
}

 

 

Votes

Translate

Translate

Report

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