Problem with .push() or for loop?!

Now I encounter the same strange things.

Bildschirmfoto 2022-01-22 um 13.06.52

Check the id’s. What the??

Bildschirmfoto 2022-01-22 um 13.09.55

I cannot replicate this on my machine w/ api level 2 on PS 23.1.0 (macOS Monterey 12.1):

Note that you can more easily convert the app.documents proxy to an array using:

Array.from(app.documents)
/* or */
[...app.documents]

Functionally there should be no difference here, but worth seeing if you do see a difference.

This problem resurfaced for one of my beta testers. He sees this error as the beta version uses showAlert() to show errors to the user instead of console.log() to log them to the console. He’s on Windows, as am I. He sees the error. I don’t.

Here’s the code that I think generates the error for the beta tester as I think it’s quite similar to @Dominik_S’s code. No push() involved. Just a for() loop.

let a;
for (a = channels.length - 1; a > -1; a--) {
    console.log(a + " = " + channels[a].name)
    if (channels[a].name === "name1" || channels[a].name === "name2" || channels[a].name === "name3" || channels[a].name === "name4" || channels[a].name === "name5") {
        await batchPlay([ 
            //Delete channel with specified name
            {"_target": [{"_ref": "channel","_name": channels[a].name} ], "_isCommand": true, "_obj": "delete"},
        ],{}).then(result=>console.log(result), error => console.log(error));
    }
}

No errors are logged for me, so I can’t be entirely sure this is the problem, but the error is the same one @Dominik_S mentions (Invalid CompositChannel referenced in PSChannel) and the code is so similar that I’m guessing it’s here. Not being able to replicate it makes troubleshooting almost impossible.

The beta tester is using the French version of Ps whereas I’m using the English version. The plugin is API-2 and manifest version 4. Photoshop is 23.4.1.

Starting to wonder if this might just be in the non-English version of Photoshop. @Dominik_S appears to be using the German version and my beta tester uses the French version.

@Dominik_S What happens if you change

for (var c = doc.channels.length-1; c >= 0; c--)

to

for (var c = doc.channels.length-1; c >= 3; c--)

This should ignore the Red, Green, and Blue component channels if you are in RGB Color mode and just create an array of your alpha channels. That way, if there is some confusion with the German names of the components channel not matching in Photoshop using the

doc.channels[c].name

variable, they will be ignored. This would also explain why some people see this error (non-English versions of Ps) and others don’t see it (English-language version of Ps).

Addendum:

For my code I changed

for (a = channels.length - 1; a > -1; a--)

to

for (a = channels.length - 1; a > 2; a--)

in order to avoid name-testing the Red, Green, and Blue component channels using the

channels[a].name

variable, and the French beta tester reports that this fixed the problem.

So, it appears that simply trying to access the .name attribute for component channels in non-English versions of Photoshop can create errors.

You can also try this:

let alphaChannelNames = Array.from(photoshop.app.activeDocument.channels)
      .filter((channel, index) => index > 2)
      .map((channel) => channel.name);

And here is a complete example. How it can work on multiple document modes:

import photoshop from "photoshop";
import React from "react";

const defaultChannels = {
  RGBColorMode: 3,
  labColorMode: 3,
  CMYKColorMode: 4,
  grayscaleMode: 1,
};

const App = () => {
  const handleClick = () => {
    if (photoshop.app.activeDocument) {
      const colorMode = photoshop.app.activeDocument.mode;
      let alphaChannelNames = [...photoshop.app.activeDocument.channels]
        .filter((channel, index) => index > defaultChannels[colorMode] - 1)
        .map((channel) => channel.name);
      console.log(alphaChannelNames);
    }
  };

  return <sp-button onClick={handleClick}>Get Alpha Channel Names</sp-button>;
};

export default App;