Skip to main content
Participant
August 12, 2014
Question

splitting layer into RGB layers in a group - newbie

  • August 12, 2014
  • 1 reply
  • 373 views

I'm a newbie trying to split a selected layer into multiple layers, each with only one channel enabled. What I mean into the script below, I’d like to do that same thing I do manually by going into Layer Style, in Advanced blending where it has the Channels R checkbox, G checkbox, B checkbox, for my R layer uncheck the G & B.

I've got a start on grouping, copying the layers, renaming them as I'd like, but I don't know how to do the equivalent of what I described above.

What I have so far...

var doc = app.activeDocument,
      currentLayer = doc.activeLayer;

var group = doc.layerSets.add();
      group.name = currentLayer.name;

var bLayer = currentLayer.duplicate(); // duplicate layer in target doc
      bLayer.name = 'Blue:' + currentLayer.name;
         currentLayer.visible =false;
//somehow here  I’d like to do that same thing I do manually in
//Layer Style, in Advanced blending it has
//Channels R checkbox, G checkbox, B checkbox
//for my R layer uncheck the G & B
      bLayer.move( group, ElementPlacement.PLACEATEND ); // move it

      doc.activeLayer = bLayer;

var gLayer = bLayer.duplicate();
      gLayer.name = 'Green:' + currentLayer.name;
      gLayer.blendMode = BlendMode.NORMAL;
//somehow here  I’d like to do that same thing I do manually in
//Layer Style, in Advanced blending it has
//Channels R checkbox, G checkbox, B checkbox
//for my G layer uncheck the R & B
      gLayer.opacity = 100.0;

      doc.activeLayer = gLayer;

var rLayer = gLayer.duplicate();
      rLayer.name = 'Red:' + currentLayer.name;
      rLayer.blendMode = BlendMode.NORMAL;
//somehow here  I’d like to do that same thing I do manually in
//Layer Style, in Advanced blending it has
//Channels R checkbox, G checkbox, B checkbox
//for my R layer uncheck the G & B
      rLayer.opacity = 100.0;

      doc.activeLayer = rLayer;
This topic has been closed for replies.

1 reply

bcar3Author
Participant
August 14, 2014

I looked around and finally did this by creating actions and then calling the actions.  I then added a loop that loops around all selected layers, using what I found at:

https://forums.adobe.com/thread/1536677

So with more research I answered my question.  Below is my script if others find it useful...

#target photoshop

alert("copying each layer in group with R, G, B layers");

runMulti();

// run();   

function runMulti(){

    var selLay = getSelectedLayersIdx();

    if( selLay.constructor != Array ) selLay = [ selLay ];

    for( var i=0; i<selLay.length; i++){

        multiSelectByIDx(selLay);

        run();

    }

}

function run (){

var doc = app.activeDocument,

        currentLayer = doc.activeLayer;

      

// test script: what I really want loop around all of the layers in the doc, not just the active one

// so I want to create a group for each of the original layers

var group = doc.layerSets.add();

        group.name = currentLayer.name;

var bLayer = currentLayer.duplicate(); // duplicate layer in target doc

        bLayer.name = 'Blue:' + currentLayer.name;

         currentLayer.visible =false;

        bLayer.move( group, ElementPlacement.PLACEATEND ); // move it

        doc.activeLayer = bLayer;

//what the action "blue" does is in

//Layer Style, in Advanced blending it has

//Channels R checkbox, G checkbox, B checkbox

//for my B layer uncheck the R & G

          app.doAction("blue", "Default Actions");

var gLayer = bLayer.duplicate();

        gLayer.name = 'Green:' + currentLayer.name;

        gLayer.blendMode = BlendMode.NORMAL;

        gLayer.opacity = 100.0;

        doc.activeLayer = gLayer;

//what the action green does is in

//Layer Style, in Advanced blending it has

//Channels R checkbox, G checkbox, B checkbox

//for my G layer uncheck the R & B     

         app.doAction("green", "Default Actions");

var rLayer = gLayer.duplicate();

        rLayer.name = 'Red:' + currentLayer.name;       

        rLayer.blendMode = BlendMode.NORMAL;

        rLayer.opacity = 100.0;

        doc.activeLayer = rLayer;

//what the action red does is in

//Layer Style, in Advanced blending it has

//Channels R checkbox, G checkbox, B checkbox

//for my R layer uncheck the G & B  

          app.doAction("red", "Default Actions");

}

function hasBackground(){// function to check if there is a background layer

    var res = undefined;

    try{

        var ref = new ActionReference();

        ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID("Nm  "));

        ref.putIndex( charIDToTypeID("Lyr "), 0 );

        executeActionGet(ref).getString(charIDToTypeID("Nm  ") );

        res = true;

    }catch(e){ res = false}

    return res;

}

function getSelectedLayersIdx(){// get the selected layers index( positon in layer editor)

     var selectedLayers = new Array;

     var ref = new ActionReference();

     ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

     var desc = executeActionGet(ref);

     var add = 1;

     if(hasBackground()){add = 0}

     if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

          desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

          var c = desc.count

          var selectedLayers = new Array();

          for(var i=0;i<c;i++){

               selectedLayers.push(  (desc.getReference( i ).getIndex()) + add);

          }

     }else{

          var ref = new ActionReference();

          ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));

          ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

          srs = hasBackground()?executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ))-1:executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' ));

          selectedLayers.push( srs);

     }

     return selectedLayers;

}

function multiSelectByIDx(idx) {

  if( idx.constructor != Array ) idx = [ idx ];

    var layers = new Array();

    var id54 = charIDToTypeID( "slct" );

    var desc12 = new ActionDescriptor();

    var id55 = charIDToTypeID( "null" );

    var ref9 = new ActionReference();

    for (var i = 0; i < idx.length; i++) {

          layers = charIDToTypeID( "Lyr " );

          ref9.putIndex(layers, idx);

    }

    desc12.putReference( id55, ref9 );

    var id58 = charIDToTypeID( "MkVs" );

    desc12.putBoolean( id58, false );

    executeAction( id54, desc12, DialogModes.NO );

}