Skip to main content
Participant
June 12, 2022
Answered

How to paste clipboard into a channel using a photoshop script?

  • June 12, 2022
  • 4 replies
  • 1103 views

I'm trying to create a image RGB or CMYK using separate greyscale documents for each channel. My current code copy all from greyscale document and paste in a new document creating a new layer for eachone. I don't want that, I want copy from each document and paste into eachone channels new document, same as manual way, selecting each channel and clicking Edit->paste->paste especial->paste in place.

// Loop to copy each separate channel into a new merged document
for (var doc_index = 0; doc_index < 3; doc_index++ ){
//Switch documents 
app.activeDocument = split_channels[0];

// copy active layer from separate channel document
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy();

// close document whithout save 
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

//select activeDocument
app.activeDocument = mergedDoc;

// reference to whole channels
//var channelIndex = doc_index;
var myChannels = app.activeDocument.channels;

// the channel must be visible to paste greyscale layer
myChannels[doc_index].visible= true;

// turn off the rest of channels    
for (var secondaryIndex = 0; secondaryIndex < myChannels.length; secondaryIndex++) {
    
    if (doc_index != secondaryIndex) {
        
        myChannels[secondaryIndex].visible= false
    }
}

//paste layer for each new document channel (no works correctly)
app.activeDocument.paste();
    
//reset the channel count after copy and close each document
var split_channels = app.documents;
}

thanks a lot for any suggestion.

This topic has been closed for replies.
Correct answer r-bin

Your code doesn't work because it's not complete.

To get what you want change

myChannels[doc_index].visible= true;

with

myChannels[doc_index].visible= true;
app.activeDocument.activeChannels = [myChannels[doc_index]];

4 replies

r-binCorrect answer
Legend
June 13, 2022

Your code doesn't work because it's not complete.

To get what you want change

myChannels[doc_index].visible= true;

with

myChannels[doc_index].visible= true;
app.activeDocument.activeChannels = [myChannels[doc_index]];
Stephen Marsh
Community Expert
Community Expert
June 13, 2022

Here are both RGB and CMYK versions combined into a conditional so that you only need one script:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-paste-clipboard-into-a-channel-using-a-photoshop-script/m-p/13000541
*/

#target photoshop

if (activeDocument.mode === DocumentMode.RGB) {

    var doc = app.documents[0];
    activeDocument = doc;

    apply("Rd  ", app.documents[1].name)
    apply("Grn ", app.documents[2].name)
    apply("Bl  ", app.documents[3].name)

    var idslct = charIDToTypeID("slct");
    var desc29 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref7 = new ActionReference();
    var idChnl = charIDToTypeID("Chnl");
    var idChnl = charIDToTypeID("Chnl");
    var idRGB = charIDToTypeID("RGB ");
    ref7.putEnumerated(idChnl, idChnl, idRGB);
    desc29.putReference(idnull, ref7);
    executeAction(idslct, desc29, DialogModes.NO);

    function apply(ch, layName) {
        var idslct = charIDToTypeID("slct");
        var desc17 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref5 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idBl = charIDToTypeID(ch);
        ref5.putEnumerated(idChnl, idChnl, idBl);
        desc17.putReference(idnull, ref5);
        executeAction(idslct, desc17, DialogModes.NO);

        var idAppI = charIDToTypeID("AppI");
        var desc22 = new ActionDescriptor();
        var idWith = charIDToTypeID("With");
        var desc23 = new ActionDescriptor();
        var idT = charIDToTypeID("T   ");
        var ref6 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idRGB = charIDToTypeID("RGB ");
        ref6.putEnumerated(idChnl, idChnl, idRGB);
        var idLyr = charIDToTypeID("Lyr ");
        var idBckg = charIDToTypeID("Bckg");
        ref6.putProperty(idLyr, idBckg);
        var idDcmn = charIDToTypeID("Dcmn");
        ref6.putName(idDcmn, layName);
        desc23.putReference(idT, ref6);
        var idPrsT = charIDToTypeID("PrsT");
        desc23.putBoolean(idPrsT, true);
        var idClcl = charIDToTypeID("Clcl");
        desc22.putObject(idWith, idClcl, desc23);
        executeAction(idAppI, desc22, DialogModes.NO);
    }

} else if (activeDocument.mode === DocumentMode.CMYK) {

    var doc = app.documents[0];
    activeDocument = doc;

    apply("Cyn ", app.documents[1].name)
    apply("Mgnt", app.documents[2].name)
    apply("Yllw", app.documents[3].name)
    apply("Blck", app.documents[4].name)

    var idslct = charIDToTypeID("slct");
    var desc29 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    var ref7 = new ActionReference();
    var idChnl = charIDToTypeID("Chnl");
    var idChnl = charIDToTypeID("Chnl");
    var idCMYK = charIDToTypeID("CMYK");
    ref7.putEnumerated(idChnl, idChnl, idCMYK);
    desc29.putReference(idnull, ref7);
    executeAction(idslct, desc29, DialogModes.NO);

    function apply(ch, layName) {
        var idslct = charIDToTypeID("slct");
        var desc17 = new ActionDescriptor();
        var idnull = charIDToTypeID("null");
        var ref5 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idBl = charIDToTypeID(ch);
        ref5.putEnumerated(idChnl, idChnl, idBl);
        desc17.putReference(idnull, ref5);
        executeAction(idslct, desc17, DialogModes.NO);

        var idAppI = charIDToTypeID("AppI");
        var desc22 = new ActionDescriptor();
        var idWith = charIDToTypeID("With");
        var desc23 = new ActionDescriptor();
        var idT = charIDToTypeID("T   ");
        var ref6 = new ActionReference();
        var idChnl = charIDToTypeID("Chnl");
        var idChnl = charIDToTypeID("Chnl");
        var idCMYK = charIDToTypeID("CMYK");
        ref6.putEnumerated(idChnl, idChnl, idCMYK);
        var idLyr = charIDToTypeID("Lyr ");
        var idBckg = charIDToTypeID("Bckg");
        ref6.putProperty(idLyr, idBckg);
        var idDcmn = charIDToTypeID("Dcmn");
        ref6.putName(idDcmn, layName);
        desc23.putReference(idT, ref6);
        var idPrsT = charIDToTypeID("PrsT");
        desc23.putBoolean(idPrsT, true);
        var idClcl = charIDToTypeID("Clcl");
        desc22.putObject(idWith, idClcl, desc23);
        executeAction(idAppI, desc22, DialogModes.NO);
    }

} else {
    alert("This script is only intended for RGB or CMYK mode files!");
}

 

Stephen Marsh
Community Expert
Community Expert
June 12, 2022

The beauty of apply image command that Chuck recommends is that it doesn't use the clipboard.

Chuck Uebele
Community Expert
Community Expert
June 12, 2022

Rather than copy and paste, why don't you use apply image? The following script will take 4 open files. In the first open file, it will apply the image (RGB) of the second image to the red channel of the first image, and so on. You must have documents that are the exact same size, for this to work. You will need to adjust it for CYMK.

#target photoshop
var doc = app.documents[0];
activeDocument = doc

apply ("Rd  ", app.documents[1].name)
apply ("Grn ", app.documents[2].name)
apply ("Bl  ", app.documents[3].name)

var idslct = charIDToTypeID( "slct" );
    var desc29 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref7 = new ActionReference();
        var idChnl = charIDToTypeID( "Chnl" );
        var idChnl = charIDToTypeID( "Chnl" );
        var idRGB = charIDToTypeID( "RGB " );
        ref7.putEnumerated( idChnl, idChnl, idRGB );
    desc29.putReference( idnull, ref7 );
executeAction( idslct, desc29, DialogModes.NO );


function apply(ch, layName){
    var idslct = charIDToTypeID( "slct" );
        var desc17 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref5 = new ActionReference();
            var idChnl = charIDToTypeID( "Chnl" );
            var idChnl = charIDToTypeID( "Chnl" );
            var idBl = charIDToTypeID( ch );
            ref5.putEnumerated( idChnl, idChnl, idBl );
        desc17.putReference( idnull, ref5 );
    executeAction( idslct, desc17, DialogModes.NO );

    var idAppI = charIDToTypeID( "AppI" );
        var desc22 = new ActionDescriptor();
        var idWith = charIDToTypeID( "With" );
            var desc23 = new ActionDescriptor();
            var idT = charIDToTypeID( "T   " );
                var ref6 = new ActionReference();
                var idChnl = charIDToTypeID( "Chnl" );
                var idChnl = charIDToTypeID( "Chnl" );
                var idRGB = charIDToTypeID( "RGB " );
                ref6.putEnumerated( idChnl, idChnl, idRGB );
                var idLyr = charIDToTypeID( "Lyr " );
                var idBckg = charIDToTypeID( "Bckg" );
                ref6.putProperty( idLyr, idBckg );
                var idDcmn = charIDToTypeID( "Dcmn" );
                ref6.putName( idDcmn, layName );
            desc23.putReference( idT, ref6 );
            var idPrsT = charIDToTypeID( "PrsT" );
            desc23.putBoolean( idPrsT, true );
        var idClcl = charIDToTypeID( "Clcl" );
        desc22.putObject( idWith, idClcl, desc23 );
    executeAction( idAppI, desc22, DialogModes.NO );
   
    }
Participant
June 12, 2022

Hi @Chuck Uebele, thank you for your help.  I wanna request your help for explain me, how it work for CMYK documents?.  I did try to adjust, but, i don't have a solid grasp in JavaScript. 

I did test for RGB files, and, it's that just i need.

Chuck Uebele
Community Expert
Community Expert
June 12, 2022

You would want to use scriptListener to get the channel names for CYMK and substitute them in the function calls. And, of course, you will need to add 4 function calls, rather than 3.