Better way to see if a mask is active

I’m trying to see if a layer mask is active or not - that is to say, whether the currently active thing we’re editing is a layer mask or not (rather than whether a mask is enabled or present).

So far I’ve got this, which looks at app.activeDocument.activeChannels, which returns an error if a mask is active, but that seems hackish, and I’m hoping to get on the app store, so I’m wondering if there’s a better way that I’ve missed!

async function isMaskActive() {
    try {
        const app = require("photoshop").app;
        // This will throw an error if a mask is active
        app.activeDocument.activeChannels;
        return false;
    } catch (error) {
        // If we get the "Unknown or unsupported active channels" error,
        // it means we're in a mask
        if (error.message.includes("Unknown or unsupported active channels")) {
            return true;
        }
        // For any other error, we should probably rethrow it
        throw error;
    }
}

In Alchemist, there is a “targetChannels” property that appears to change depending on whether or not the layer mask is the active canvas. You might have to write a getter to access it, though. I’m not sure if it’s present in the DOM or not.

2 Likes