Script To Delete RGB Channels If Present

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Colorguy

Script To Delete RGB Channels If Present

Post by Colorguy »

I have some actions that require the RGB channels to be removed prior to running. I've been trying to write a script that deletes the RGB channels if present, but if they're already deleted, the attempt by the script to remove them will be disregarded and the action will continue to run without error. I can't seem to figure this out. Any help on how this could be done is appreciated. Thanks!
Mike Hale

Script To Delete RGB Channels If Present

Post by Mike Hale »

I think I need more info about what you are trying to do. What color mode is the document? You can't delete the red, green, blue layers in an RGB image.
Colorguy

Script To Delete RGB Channels If Present

Post by Colorguy »

The color mode is RGB. And yes, in this case the RGB channels can be deleted since the file contains many other alpha channels. So deleting the RGB's would just result in a multichannel document.

When end user runs the action, the script I'm trying to do would delete the RGB channels and the action would then continue to run. However, if the RGB channels have already been deleted by the user prior to running the action, the script would ignore the request to delete the RGB's and the action would then run as normal.
Mike Hale

Script To Delete RGB Channels If Present

Post by Mike Hale »

The color mode is RGB
Well, no. Deleting one of the component channels forces Photoshop to convert to multi-channel mode so the image is no longer RGB.

The script could check the document color mode and if it's still RGB it would know to remove 3 channels. Beyond that I guess it could remove by name but that would assume that none of the channels you want to keep would have standard color mode channel names.
Colorguy

Script To Delete RGB Channels If Present

Post by Colorguy »

I found a workaround for this. Just add Image > Mode > Multichannel prior to the script. If the RGB's are there, it creates a Cyan, Magenta and Yellow channel. I then added the below near the start of my script to delete them if present. If the image doesn't contain RGB channels, it doesn't matter.

Code: Select all{
  for(var channelIndex = 0; channelIndex < app.activeDocument.channels.length; channelIndex++)
  {
    var channelRef = app.activeDocument.channels[channelIndex]
    var channelName = channelRef.name;
    if(channelName.search("Cyan")!=-1)
    {
      channelRef.remove();
      channelIndex = channelIndex-1;
    }
  }
}

{
  for(var channelIndex = 0; channelIndex < app.activeDocument.channels.length; channelIndex++)
  {
    var channelRef = app.activeDocument.channels[channelIndex]
    var channelName = channelRef.name;
    if(channelName.search("Magenta")!=-1)
    {
      channelRef.remove();
      channelIndex = channelIndex-1;
    }
  }
}

{
  for(var channelIndex = 0; channelIndex < app.activeDocument.channels.length; channelIndex++)
  {
    var channelRef = app.activeDocument.channels[channelIndex]
    var channelName = channelRef.name;
    if(channelName.search("Yellow")!=-1)
    {
      channelRef.remove();
      channelIndex = channelIndex-1;
    }
  }
}
User avatar
Stephen_A_Marsh
Posts: 30
Joined: Sun Aug 04, 2019 12:37 pm

Re: Script To Delete RGB Channels If Present

Post by Stephen_A_Marsh »

I know that this is an old topic, I stumbled over it when I too was looking for code to remove the component channels.

Here is the function that I created, just in case it helps someone else:

Code: Select all

removeCompChannels();

function removeCompChannels() {
    if (app.activeDocument.mode === DocumentMode.RGB) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var list = new ActionList();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        var reference3 = new ActionReference();
        var reference4 = new ActionReference();
        reference4.putEnumerated(s2t("channel"), s2t("channel"), s2t("RGB"));
        descriptor2.putReference(s2t("null"), reference4);
        executeAction(s2t("select"), descriptor2, DialogModes.NO);
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("blue"));
        list.putReference(reference);
        reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("grain"));
        list.putReference(reference2);
        reference3.putEnumerated(s2t("channel"), s2t("channel"), s2t("red"));
        list.putReference(reference3);
        descriptor.putList(s2t("null"), list);
        executeAction(s2t("delete"), descriptor, DialogModes.NO)

    } else if (app.activeDocument.mode === DocumentMode.CMYK) {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var list = new ActionList();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        var reference3 = new ActionReference();
        var reference4 = new ActionReference();
        var reference5 = new ActionReference();
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("CMYK"));
        descriptor.putReference(s2t("null"), reference);
        executeAction(s2t("select"), descriptor, DialogModes.NO);
        reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("black"));
        list.putReference(reference2);
        reference3.putEnumerated(s2t("channel"), s2t("channel"), s2t("yellow"));
        list.putReference(reference3);
        reference4.putEnumerated(s2t("channel"), s2t("channel"), s2t("magenta"));
        list.putReference(reference4);
        reference5.putEnumerated(s2t("channel"), s2t("channel"), s2t("cyan"));
        list.putReference(reference5);
        descriptor2.putList(s2t("null"), list);
        executeAction(s2t("delete"), descriptor2, DialogModes.NO);

    } else if (app.activeDocument.mode === DocumentMode.LAB) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var list = new ActionList();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        var reference3 = new ActionReference();
        var reference4 = new ActionReference();
        reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("lab"));
        descriptor.putReference(s2t("null"), reference);
        executeAction(s2t("select"), descriptor, DialogModes.NO);
        reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("b"));
        list.putReference(reference2);
        reference3.putEnumerated(s2t("channel"), s2t("channel"), s2t("a"));
        list.putReference(reference3);
        reference4.putEnumerated(s2t("channel"), s2t("channel"), s2t("lightness"));
        list.putReference(reference4);
        descriptor2.putList(s2t("null"), list);
        executeAction(s2t("delete"), descriptor2, DialogModes.NO);

    } else if (app.activeDocument.mode === DocumentMode.GRAYSCALE) {
        function s2t(s) {
            return app.stringIDToTypeID(s);
        }
        var descriptor = new ActionDescriptor();
        var descriptor2 = new ActionDescriptor();
        var reference = new ActionReference();
        var reference2 = new ActionReference();
        reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("black"));
        descriptor2.putReference(s2t("null"), reference2);
        executeAction(s2t("select"), descriptor2, DialogModes.NO);
        reference.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
        descriptor.putReference(s2t("null"), reference);
        executeAction(s2t("delete"), descriptor, DialogModes.NO);
    }
}