Just tried and it works but only if I do it in the same document.
I found where was the problem, let me explain:
If you have 2 PSD open:
const docA = app.documents[0];
const docB = app.documents[1];
And if you try to get a channel from docA when docA is infrot ( activeDocument ) it works.
const channel = docA.channels.getByName("OC_Layer1");
// This logs the channel object without problem
BUT If you try to use the same code when the docB
is the activeDocument
it will get null.
So what I did was create a functiuon to bring to the front the document I want to get the channel from and run the getByName() function so the duplicate() function will work. With the Alchemist plugin I got a code for batchPlay to bring into the front the document I want but seems it needs an offset number form the activeDocument to make another document active.
This is the code from Alchemist:
// The 'offset' is the number of documents that the activeDocument needs to displace, e.g. -2 or 2.
await batchPlay(
[
{
"_obj": "select",
"_target": [
{
"_ref": "document",
"_offset": offset
}
]
}
], {});
So I made this function to make it work:
async function setActiveDocument(doc) {
let offset = 0;
const activeDoc = await getDocIdx(app.activeDocument);
const docIdx = await getDocIdx(doc);
if (activeDoc < docIdx) {
offset = docIdx - activeDoc;
} else {
offset = docIdx - activeDoc;
}
await batchPlay(
[
{
"_obj": "select",
"_target": [
{
"_ref": "document",
"_offset": offset
}
]
}
], {});
}
async function getDocIdx(inputDoc) {
for (let i = 0; i < app.documents.length; i++) {
const doc = app.documents[i];
if (doc.name === inputDoc.name) return i;
}
}
I couldn’t find a way to make it work with the DOM something like the code bellow, so that’s why I made the function.
app.documents[2].bringToFront();
In resume:
The problem I found is that channels from another PSD cannot be called or get unless it is the activeDocument where the channels are being called, does this makes sense?
Hope this makes sense.