Check kind of selected channel using javascript

Engaged ,
Aug 15, 2022 Aug 15, 2022

Copy link to clipboard

Copied

I try to create script which will check what is the kind of selected channel and based of that alert user, but i always got same message. Could someone help me with this code?

var myChannel = app.activeDocument.activeChannels;
if (myChannel.kind == "ChannelType.MASKEDAREA"||myChannel.kind  == "ChannelType.SELECTEDAREA"||myChannel.kind  == "ChannelType.SPOTCOLOR") {
alert("It is not CMYK")
}
else{
alert("It is CMYK")
}

Screen Shot 2022-08-16 at 08.52.04.png

TOPICS
Actions and scripting

Views

114

Likes

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 , Aug 16, 2022 Aug 16, 2022

The plural »activeChannels« should be a give-away. 

Try inserting 

alert (myChannel.constructor);

to see what myChannel actually is. 

Likes

Translate

Translate
Community Expert , Aug 16, 2022 Aug 16, 2022

Yes, as rightly noted @c.pfaffenbichler  app.activeDocument.activeChannels is an array. It contains one or more elements (depending on the number of dedicated channels). That is, you need to either perform a check on all elements of this array in a loop, or (if you are sure that only one channel is selected) you can use it like this:

var myChannel = app.activeDocument.activeChannels.shift();

 

Likes

Translate

Translate
Community Expert ,
Aug 16, 2022 Aug 16, 2022

Copy link to clipboard

Copied

The plural »activeChannels« should be a give-away. 

Try inserting 

alert (myChannel.constructor);

to see what myChannel actually is. 

Likes

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 ,
Aug 16, 2022 Aug 16, 2022

Copy link to clipboard

Copied

Yes, as rightly noted @c.pfaffenbichler  app.activeDocument.activeChannels is an array. It contains one or more elements (depending on the number of dedicated channels). That is, you need to either perform a check on all elements of this array in a loop, or (if you are sure that only one channel is selected) you can use it like this:

var myChannel = app.activeDocument.activeChannels.shift();

 

Likes

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
Engaged ,
Aug 16, 2022 Aug 16, 2022

Copy link to clipboard

Copied

Thanks guys

Likes

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
Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Dear @c.pfaffenbichler @jazz-y is it possible somehow (with some if statement) to check is it currently selected one or multiply channels? THanks in advance

Likes

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 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Check whether the length of 

app.activeDocument.activeChannels

is equal to 1 with an if-clause.  

Likes

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
Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Thanks, i tried this and it works

var myActiveDoc = app.activeDocument;
var myChannels = myActiveDoc.activeChannels;
var total = myChannels.length;
if(total>1){   
    alert("multiply channels are selected")
}
else {
    alert("single channel is selected")
}

Likes

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
Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

But now i stuck again, when i selected some mask which is applied on some adjustment layer (curves, channel mixer,..) and when i run this script i got an error. Is it possible to improve this?

Likes

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 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

You could wrap the whole thing in a try-clause and in the catch alert that a Layer Mask is selected. 

To me at least no other reason comes to mind for activeChannels erroring, it seeems impossible to have 0 Channels selected in an open document. 

Likes

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
Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

LATEST

You are right, i resolved with try/catch clause, thanks for idea 

Likes

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