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

how to programmatically check if the layer or the layer mask is selected?

Contributor ,
Jun 09, 2025 Jun 09, 2025

question says it all. I was working on a script to toggle between the layer and the layer mask selection, but I was unable to find a way to check which one is currently selected.

TOPICS
Actions and scripting
255
Translate
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 1 Correct answer

Community Expert , Jun 09, 2025 Jun 09, 2025

@Flowgun – Try this (raster mask):

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/detect-if-mask-or-layer-is-selected-with-js-script/m-p/11153109 */

// Return true for layer mask selected, or false for composite channel

layerMaskIsSelected();

function layerMaskIsSelected() {
	s2t = stringIDToTypeID;

	(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
	r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	if (executeActionGet(r).g
...
Translate
Adobe
Community Expert ,
Jun 09, 2025 Jun 09, 2025

@Flowgun – Try this (raster mask):

/* https://community.adobe.com/t5/photoshop-ecosystem-discussions/detect-if-mask-or-layer-is-selected-with-js-script/m-p/11153109 */

// Return true for layer mask selected, or false for composite channel

layerMaskIsSelected();

function layerMaskIsSelected() {
	s2t = stringIDToTypeID;

	(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
	r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	if (executeActionGet(r).getBoolean(p)) {

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
	    (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	    layerName = executeActionGet(r).getString(p);

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('channelName'));
	    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
	    channelName = executeActionGet(r).getString(p);

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('alphaChannelOptions'));
	    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
	    alphaChannel = executeActionGet(r).hasKey(p);

		// return (channelName.indexOf(layerName) == 0 && !alphaChannel);
        alert(channelName.indexOf(layerName) == 0 && !alphaChannel);
	}
}

 

Edit: Perhaps more usable:

 

layerMaskIsSelected();

function layerMaskIsSelected() {
    s2t = stringIDToTypeID;

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
    r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    if (executeActionGet(r).getBoolean(p)) {

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
        (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
        layerName = executeActionGet(r).getString(p);

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('channelName'));
        r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
        channelName = executeActionGet(r).getString(p);

        (r = new ActionReference()).putProperty(s2t('property'), p = s2t('alphaChannelOptions'));
        r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
        alphaChannel = executeActionGet(r).hasKey(p);

        if (channelName.indexOf(layerName) === 0 && !alphaChannel) {
            alert("Layer mask is selected.");
        } else {
            alert("Layer mask is NOT selected.");
        }
    }
}

 

Translate
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 ,
Jun 09, 2025 Jun 09, 2025

Thank you. that worked.

Translate
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 ,
Jun 09, 2025 Jun 09, 2025
LATEST

You're welcome!

Translate
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