Copy link to clipboard
Copied
I have some script which change image mode to multichannel and select all channels, code works fine, but when it is par of some function i get some error alert. What could be a problem?
////// change to multichanel/////
var idCnvM = charIDToTypeID( "CnvM" );
var desc1639 = new ActionDescriptor();
var idT = charIDToTypeID( "T " );
var idMltC = charIDToTypeID( "MltC" );
desc1639.putClass( idT, idMltC );
executeAction( idCnvM, desc1639, DialogModes.NO );
/////////// SELECT ALL CHANNELS///////
var myActiveDoc = app.activeDocument;
var myChannels = myActiveDoc.channels;
var total = myChannels.length;
var ac = new Array();
run();
function run() {
selectChannels();
app.activeDocument.activeChannels = this.ac;
}
function selectChannels() {
for(var i = (total-1); i >= 0; i--){
this.ac.push(myActiveDoc.channels[i]);
}
}
I'm not at the computer to check, but it seems when you paste this code inside another function the scope of "this.ac" changes. Remove keyword "this." everywhere. In this case the "ac" variable will be scoped to the parent function and will be available to the run () and selectChannels () subfunctions
Copy link to clipboard
Copied
Your code ran OK for me.
Simply converting to MC mode results in all colour channels being selected, so isn't the second part of the code redundant? Or are there spot channels or alpha channels that also need to selected after the MC mode conversion.
Copy link to clipboard
Copied
Your code also works fine, but if i put in simple function like this, i got the same error as I mentioned in post.
selectchannels()
function selectchannels(){
////// change to multichanel/////
var idCnvM = charIDToTypeID("CnvM");
var desc1639 = new ActionDescriptor();
var idT = charIDToTypeID("T ");
var idMltC = charIDToTypeID("MltC");
desc1639.putClass(idT, idMltC);
executeAction(idCnvM, desc1639, DialogModes.NO);
/////////// SELECT ALL CHANNELS///////
var myActiveDoc = app.activeDocument;
var myChannels = myActiveDoc.channels;
var total = myChannels.length;
var ac = [];
selectChannels();
app.activeDocument.activeChannels = this.ac;
function selectChannels() {
for (var i = (total - 1); i >= 0; i--) {
this.ac.push(myActiveDoc.channels[i]);
}
}
}
Copy link to clipboard
Copied
RGB or CMYK to MC mode results in all channels being selected.
I removed that previous code as I found that it didn't work in isolation, even if no error was triggered.
So again, why the second half of the code, if the first half results in all previous colour channels being selected?
Copy link to clipboard
Copied
Because I have to choose all spot colors. If I change only the mode, only CMYK channels are selected, not spot colors. sorry for skipping to answer that question earlier.
Copy link to clipboard
Copied
I have been looking for examples of scripts to select all spot channels, however they don't appear to work in CC 2021:
how select all spot channel by javascript
Copy link to clipboard
Copied
This does not work in my PS ☹️ and i stil have doubt why my code does not work in function?
Copy link to clipboard
Copied
I'm not at the computer to check, but it seems when you paste this code inside another function the scope of "this.ac" changes. Remove keyword "this." everywhere. In this case the "ac" variable will be scoped to the parent function and will be available to the run () and selectChannels () subfunctions
Copy link to clipboard
Copied
Great! Thanks!
Copy link to clipboard
Copied
@jazz-y – thank you and great job as usual!
Copy link to clipboard
Copied
Where have you found this script that is uses 'this'?
Copy link to clipboard
Copied
@Kukurykus You can found on the second link posted by @Stephen_A_Marsh
I think i also found on Git earlier