Skip to content

Instantly share code, notes, and snippets.

@MarshySwamp
Created August 15, 2021 01:24
Show Gist options
  • Save MarshySwamp/d6f5f0ef4a8b344a6d15c88e3c59621a to your computer and use it in GitHub Desktop.
Save MarshySwamp/d6f5f0ef4a8b344a6d15c88e3c59621a to your computer and use it in GitHub Desktop.
Various snippets for selecting Photoshop channels
/*
NOTES:
The channels array is zero indexed, channels[0] is at the top/head of the channels panel
The top-level "primary/composite" of the ChannelType.COMPONENT is not included in the individual layer count
*/
// Save the current active channels
var savedChannelState = app.activeDocument.activeChannels;
/***************************************************************************************************/
// Select/target/activate the top/first channel
selectFirstChannel();
function selectFirstChannel() {
var theChannels = new Array(app.activeDocument.channels[0]);
app.activeDocument.activeChannels = theChannels;
alert("Top/first channel selected");
};
/***************************************************************************************************/
// Select/target/activate the 2nd and 4th channels
selectTwoChannels();
function selectTwoChannels() {
var theChannels = new Array(app.activeDocument.channels[1], app.activeDocument.channels[3]);
app.activeDocument.activeChannels = theChannels;
alert("2nd & 4th channels selected");
};
/***************************************************************************************************/
// Select/target/activate the bottom/last channel
selectLastChannel();
function selectLastChannel() {
// var theChannels = new Array(app.activeDocument.channels[app.activeDocument.channels.length - 1]);
var lastChannel = app.activeDocument.channels.length - 1;
var theChannels = new Array(app.activeDocument.channels[lastChannel]);
app.activeDocument.activeChannels = theChannels;
alert("Bottom/last channel selected");
};
/***************************************************************************************************/
// Select/target/activate the componentChannels
app.activeDocument.activeChannels = app.activeDocument.componentChannels;
alert("componentChannels selected");
/***************************************************************************************************/
// Select/target/activate channel by name
selectChannelByName("Spot Color 1");
function selectChannelByName(channelName) {
var channelByName = new Array(app.activeDocument.channels[channelName]);
app.activeDocument.activeChannels = channelByName;
alert(channelByName + " selected");
};
/***************************************************************************************************/
selectAllSpotChannels();
function selectAllSpotChannels() {
var totCha = app.activeDocument.channels.length;
var actCha = new Array();
for (var i = (totCha - 1); i >= 0; i--) {
if (app.activeDocument.channels[i].kind === ChannelType.SPOTCOLOR) {
actCha.push(app.activeDocument.channels[i]);
}
}
app.activeDocument.activeChannels = actCha;
alert("All spot channels selected");
};
/***************************************************************************************************/
selectAllSELECTEDAREAalphaChannels();
function selectAllSELECTEDAREAalphaChannels() {
var totCha = app.activeDocument.channels.length;
var actCha = new Array();
for (var i = (totCha - 1); i >= 0; i--) {
if (app.activeDocument.channels[i].kind === ChannelType.SELECTEDAREA) {
actCha.push(app.activeDocument.channels[i]);
}
}
app.activeDocument.activeChannels = actCha;
alert("All SELECTEDAREA alpha channels selected");
};
/***************************************************************************************************/
selectAllMASKEDAREAalphaChannels();
function selectAllMASKEDAREAalphaChannels() {
var totCha = app.activeDocument.channels.length;
var actCha = new Array();
for (var i = (totCha - 1); i >= 0; i--) {
if (app.activeDocument.channels[i].kind === ChannelType.MASKEDAREA) {
actCha.push(app.activeDocument.channels[i]);
}
}
app.activeDocument.activeChannels = actCha;
alert("All MASKEDAREA alpha channels selected");
};
/***************************************************************************************************/
selectAllalphaChannels();
function selectAllalphaChannels() {
var totCha = app.activeDocument.channels.length;
var actCha = new Array();
for (var i = (totCha - 1); i >= 0; i--) {
if (app.activeDocument.channels[i].kind === ChannelType.SELECTEDAREA || app.activeDocument.channels[i].kind === ChannelType.MASKEDAREA) {
actCha.push(app.activeDocument.channels[i]);
}
}
app.activeDocument.activeChannels = actCha;
alert("All alpha channels selected");
};
/***************************************************************************************************/
// Select/target/activate all channels
selectAllChannels();
function selectAllChannels() {
var totCha = app.activeDocument.channels.length;
var actCha = new Array();
for (var i = (totCha - 1); i >= 0; i--) {
actCha.push(app.activeDocument.channels[i]);
}
app.activeDocument.activeChannels = actCha;
alert("All channels selected");
};
/***************************************************************************************************/
// Restore the saved active channels
app.activeDocument.activeChannels = savedChannelState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment