5 Replies Latest reply on Apr 5, 2011 1:56 PM by Michael L Hale

    Using Channels, how to duplicate an alpha?

    arcandi0

      Hey again, everypeoples.

       

       

              var alphaChannel = channels[5];

              var newChannel = alphaChannel.duplicate(currentFile, ElementPlacement.PLACEATBEGINNING);

       

       

      I'm sure I'm not doing this right, but what it keeps telling me is that "channels" is undefined on the first line. I've tried using "Channels" but that didn't work either.

       

      What I'm trying to do is duplicate an alpha channel from one document to another. Am I going about this correctly?

        • 1. Re: Using Channels, how to duplicate an alpha?
          JJMack Most Valuable Participant

           

                  var alphaChannel = channels[5];

                  var newChannel = alphaChannel.duplicate(currentFile, ElementPlacement.PLACEATBEGINNING);

           

           

          Where is the rest of your script where you created a new aray named channels and set its contents.  I don't know java but it looks like your trying to set a var named alphaChannel to the value of the sixth element in an aray named channels.

          var channels = new Array()

          for(var channelIndex = 0; channelIndex < activeDocument.channels.length; channelIndex++) {

          channels[channelIndex] = activeDocument.channels[channelIndex]

          }

          alert("channels = " + channels );

          • 2. Re: Using Channels, how to duplicate an alpha?
            Paul Riggott Level 6

            Please try this...

             

            activeDocument.activeChannel = activeDocument.channels.getByName ('My Alpha');//Amend to suit
            //Copy to whatever document you want
            activeDocument.activeChannel.duplicate(app.documents[1]);
            
            1 person found this helpful
            • 3. Re: Using Channels, how to duplicate an alpha?
              Michael L Hale Level 5

              I see two problems with your code.

               

              var alphaChannel = channels[5];

               

              should be

               

              var alphaChannel = currentFile.channels[5];

               

              and channel.duplicate does not have a second argument. You can't order channels like you can layers.

               

              var currentFile = app.activeDocument;
              var alphaChannel = currentFile.channels[5];
              var newChannel = alphaChannel.duplicate(currentFile);
              

               

              JJMack, with javascript Photoshop collections like documents, layers, channels, etc can be referenced by index without the need of arrays.

              1 person found this helpful
              • 4. Re: Using Channels, how to duplicate an alpha?
                arcandi0 Level 1

                Thanks a bunch so far guys!

                 

                Let me further elaborate and we can see if anyone can help me USE the alpha now that I have it.

                 

                The goal of the script is to open two files that have alpha and add the contents of file B on top of the contents of file A, while adding the alpha channels of the two files together. I also have to use the Alpha from file B to mask off the layer I copied from file B so that B only covers A where the Alpha is opaque.

                 

                Right now I'm trying to figure out how to get the alpha channel converted into a layer mask on the topmost layer. I see that there are several types of channels, but I'm not sure how to link a maskedarea channel to a particular layer. figuring how to make a selection from the channel (like ctrl copy in windows PS) would be another huge step in the right direction.

                 

                Thanks again for all your help, guys!

                • 5. Re: Using Channels, how to duplicate an alpha?
                  Michael L Hale Level 5
                  // assumes the layer you want to mask is active and doesn't already have a channel mask
                  var docRef = app.activeDocument;
                  var channel = docRef.channels[5];
                  docRef.selection.load(channel);
                  makeMaskFromSelection();
                  
                  function makeMaskFromSelection(blHide){
                     blHide ? blHide = "HdSl":blHide =  "RvlS";
                      var desc = new ActionDescriptor();
                      desc.putClass( charIDToTypeID( "Nw  " ), charIDToTypeID( "Chnl" ) );
                          var ref = new ActionReference();
                          ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Msk "  ));
                      desc.putReference( charIDToTypeID( "At  " ), ref );
                      desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "UsrM" ), charIDToTypeID( "RvlS" ) );
                       executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
                  }