2 Replies Latest reply on May 6, 2011 8:07 AM by davescripter

    Turning greyscale alphas into group masks

    davescripter

      Hi

       

      I'm new to ps scripting and could do with a little help. I want to take a folder of greyscale alpha files and turn them into a single psd where each alpha layer is converted into a group mask with the filename as the group name. The script below is one I found at ps scripts and modified slightly. It works ok but turns the alpha layers into layer masks as opposed to group masks. If anyone can help me achieve this or point me in the direction of other codes that create groups and apply masks that I can study I would be really grateful.

       

      var baseDoc = app.activeDocument;
      var baseLayer = baseDoc.activeLayer;
      var baseDocFolder = Folder(baseDoc.path);
      var alphaSaveFolder = new Folder(baseDocFolder+'/fromAlphas');
      if(!alphaSaveFolder.exists) alphaSaveFolder.create();
      var alphaFolder = baseDocFolder.selectDlg('Select the aplha folder');
      var alphaFiles = alphaFolder.getFiles(IsFileOneOfThese(['PSD','TIF','JPG']));// edit arguements to match your alpha files extension
      for(var a = 0; a < alphaFiles.length; a++ ){
         var alpha = app.open(alphaFiles[a]);
         var alphaName = alpha.name.match(/(.*)(\.[^\.]+)/)[1];
         var tempAlpha = alpha.channels[0].duplicate(baseDoc);
         alpha.close(SaveOptions.DONOTSAVECHANGES);
        // var newLayer = baseLayer.duplicate(); Old duplicate string
         var newLayer = baseDoc.artLayers.add();
         newLayer.name = alphaName;
         baseDoc.activeLayer = newLayer;
         createChannelMask();
         applyAlphaAsMask( tempAlpha );
         deleteChannelMask(true);
         tempAlpha.remove();
      }
      
      
      ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
      function createChannelMask() {
         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('RvlA') );
          executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );
      };
      function applyAlphaAsMask( alpha ) {
          var desc = new ActionDescriptor();
              var ref = new ActionReference();
              ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
          desc.putReference( charIDToTypeID('null'), ref );
          desc.putBoolean( charIDToTypeID('MkVs'), false );
          executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
          var desc = new ActionDescriptor();
              var desc1 = new ActionDescriptor();
                  var ref = new ActionReference();
                  ref.putName( charIDToTypeID('Chnl'), alpha.name );
              desc1.putReference( charIDToTypeID('T   '), ref );
              desc1.putBoolean( charIDToTypeID('PrsT'), true );
          desc.putObject( charIDToTypeID('With'), charIDToTypeID('Clcl'), desc1 );
          executeAction( charIDToTypeID('AppI'), desc, DialogModes.NO );
          //selectComponetChannel();
      };
      function deleteChannelMask(apply){
         var desc = new ActionDescriptor();
          var ref = new ActionReference();
          ref.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Msk "));
          desc.putReference(charIDToTypeID("null"), ref);
      };
      function newDocFromLayer(docName,layerName){
         docName == undefined ? docName = "Untitled":docName;
         layerName == undefined ? layerName = activeDocument.activeLayer.name:layerName;
          var desc = new ActionDescriptor();
              var ref = new ActionReference();
              ref.putClass( charIDToTypeID( "Dcmn" ) );
          desc.putReference( charIDToTypeID( "null" ), ref );
          desc.putString( charIDToTypeID( "Nm  " ), docName );
             var ref1 = new ActionReference();
              ref1.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
          desc.putReference( charIDToTypeID( "Usng" ), ref1 );
          desc.putString( charIDToTypeID( "LyrN" ), layerName );
         executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
         return app.activeDocument;
      };
      // from Image Processor.jsx
      // given a file name and a list of extensions
      // determine if this file is in the list of extensions
      function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {
         var lastDot = inFileName.toString().lastIndexOf( "." );
         if ( lastDot == -1 ) {
            return false;
         }
         var strLength = inFileName.toString().length;
         var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );
         extension = extension.toUpperCase();
         for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {
            if ( extension == inArrayOfFileExtensions[i] ) {
               return true;
            }
         }
         return false;
      };