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

Community Beginner ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

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.

TOPICS
Actions and scripting , Windows

Views

125

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Adobe Community Professional , Jun 11, 2022 Jun 11, 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....

Likes

Translate

Translate
Adobe Community Professional , Jun 12, 2022 Jun 12, 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) ...

Likes

Translate

Translate
Adobe Community Professional , Jun 12, 2022 Jun 12, 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]];

Likes

Translate

Translate
Adobe Community Professional ,
Jun 11, 2022 Jun 11, 2022

Copy link to clipboard

Copied

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 );
   
    }

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

I did try to follw your suggestions but maybe something is wrong.  I did replace while RGB references by CMYK and add a new function call.  When i trying to run, the scritp just do nothing.

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

apply ("Cyan  ", app.documents[1].name)
apply ("Magenta  ", app.documents[2].name)
apply ("Yellow  ", app.documents[3].name)
apply ("Black  ", 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 );
   
    }

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

@marlon248285867nip – It's not that simple:

 

apply ("Cyan  ", app.documents[1].name)
apply ("Magenta  ", app.documents[2].name)
apply ("Yellow  ", app.documents[3].name)
apply ("Black  ", app.documents[4].name)

 

CharID codes are four characters:

 

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

Hi Chuck, I really like your code, mine would have been much more "linear" and literal...

 

Here it is for CMYK. Scripting Listener didn't record the required four-character CharID codes for the CMYK channels, so I used a script I had previously created to convert CharID to StringID / StringID to CharID.

 

Anyway, here it is for CMYK:

 

#target photoshop

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 );

}

 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

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!");
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 12, 2022 Jun 12, 2022

Copy link to clipboard

Copied

LATEST

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]];

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines