Problem with .push() or for loop?!

Hello again,

I’m currently stuck with a problem with .push() or the for loop, I don’t really get it …

I’m iterating through the channels to get the name of all alpha channels with this code:

async function testfunct(){
	const app = require('photoshop').app;
	const doc = app.activeDocument;
	const alert = require('photoshop').core.showAlert;
	const constants = require('photoshop').constants;
	var channelArray = [];

	for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
	};
	console.log(channelArray);
};

But unfortunately nothing gets logged to the console. But if I put the console.log() in the for loop I get a result – which makes perfect sense to me:

for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
		console.log(channelArray);
	};


This is exactly the result one can expect, but that’s not really what I want. So why does my original code not work? Any Ideas? Thanks!

Not sure what’s happening on your machine, but it works here in console at least (notice the FULL log)
image

BTW, I don’t think you need async in this case

Thanks for testing! Hmm? Good to see it work on your machine, are you on PS 23.1? I wasn’t … I was on 23.0. I’m updating right now and will test it again later.
Oh and you are right, async is just there because this is only a part of a bigger function.

One more question: are you on windows or mac? I don’t know if this is even relevant, but what is strange is that you see blue, green and red in the console. Which means you tried this on an RGB document with no alpha channels right? When I use my code on such a doc a get no result, because for some reason only alpha channels are logged on my machine, which is strange because in ExtendScript I had to explicitly get rid of all the ChannelType.COMPONENT and ChannelType.SPOTCOLOR channels …

Windows 10, v23.1, fresh document without any edits

Thanks! Unfortunately I still cannot reproduce your results. I am now on PS 23.1 (UXP Version 5.5.6), macOS 11.2.3.

With this code …

async function testfunct(){
	const app = require('photoshop').app;
	const doc = app.activeDocument;
	const alert = require('photoshop').core.showAlert;
	const constants = require('photoshop').constants;
	var channelArray = [];

	for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
		console.log("LOOP",channelArray);
	};
	console.log("FULL",channelArray);
};

… I only get this result:

So the “FULL” log fails and the RGB channels aren’t logged too. Really weird … And with both function and async function I get this errors about “Invalid CompositeChannel”.
Maybe one of the Adobe devs could clarify this? @kerrishotts maybe?

Sounds like something’s with channels. Which manifest and API?

API version 2, manifest version 4.

How many channels do you have? Because after the second channel it fails and never finishes the loop. If you add another log before push, I suspect you’d see it, but not after push

Also try console.log(doc.channels) before the loop

I had 2 alpha channels plus the 3 RGB channels (but the never get logged on my side – don’t know why).
Here are the results from your inputs:

I tested it on a completely different machine (again macOS, but 10.15.7).

But what is so strange to me is that I used the same logic of this code in ExtendScript with no problems …

What if you try looping the channels directly? Like

var channelArray = [];

for (let channel of doc.channels) {
  channelArray.push(channel.name);
};

I believe logic itself is fine here. Also, you should not get a Proxy object IMO (see this topic). Maybe that’s the reason, although I don’t see why it would be.

Could you also try just logging each channel? If it logs, see what’s the difference maybe between alpha and RGB :thinking:

for (let channel of doc.channels) {
  console.log(channel);
};

Just a thought, but your error message says: “Invalid CompositeChannel”

So maybe it’s Photoshop’s “Composite” channel that is the problem. Based on some code I’ve used, the Composite channel is NOT counted in the number of channels when Photoshop keeps track of the number of channels.

As such, you might try getting the channel’s index as well as its name to see how these line up logging this all out during the for loop.

You might also look at Alchemist to look at the “numberOfChannels” property and see if that helps. I think you’ll find that an RGB image doesn’t count the Composite channel in the total number of channels.

You’re code also allows for a doc.channels[0].name. Would this be the “Composite” channel? If so, your code might encounter an error at this point if the Composite channel is not able to be included in the array of channel names.

This is all speculation at this point, but if you log out the channel index as well as it’s name, you might get a clearer picture of what’s happening behind the curtain.

Unfortunately both of your suggestions just end with this again:

Hi @AnthonyK , thanks for your inputs!

The number of Channels (according to Alchemist) is 5 in this and all of my other examples. Which is exactly what I expected, because – you’re totally right – the composite channel never got counted.

But that also means that doc.channels[0].name is not the composite, see below (I tested all indexes manually here):


That probably means that 0 = R, 1 = G and 2 = B. But what I don’t get is, when there is no CompositeChannel involved, why is there an error?

As I mentioned before, could you try logging without a name - just a full channel - eg. doc.channels[2]

Log the index instead of the name. It’s possible that the .name value for the Red, Green, and Blue channels are problematic since there names would vary by the language Photoshop is using. See if the index value can be logged successfully. Perhaps only alpha channels can have a .name value.

Exactly my point. That’s why I want to see full channel data

I already tried that, but as stated above I only get an error for the component channels RGB:
Bildschirmfoto 2021-12-21 um 10.59.38

BTW: I tried my old ExtendScript script on the same machine today and it works perfectly fine:


As you can see in the alert, all 5 channels are in the array (Red, Green, Blue and both Alpha channels).
I’m not a developer, but for me that looks like something wrong or at least different in UXP?!

I also tried that, but again I only get the error:
Bildschirmfoto 2021-12-21 um 11.26.51
Also please see my post above.

I still lean to theory, that it has something to do with returned Proxy instead of Channels. I believe proxies deal with references and the error mentions wrong reference :thinking:

@kerrishotts, maybe some insight?