6 Replies Latest reply on Nov 28, 2011 1:26 AM by Muppet Mark

    Is this possible? crop selection....?

    yajiv_viki Level 2

      Hi Everyone!,


      Is this possible?


      we do this step more than 50 times every day.


      1. Get selection from alpha mask.

      2. we add 10 pixel to top selection and left & right selection to add 30 pixel.

      3.finally crop the image use that selection.


      Thanks in advance. If anyone guide me how to write this script...?


      find the attachment for more clarity.









        • 1. Re: Is this possible? crop selection....?
          Paul Riggott Level 6

          Please try this..



          function main(){
          if(!documents.length) return;
          var doc = activeDocument;
          if(doc.componentChannels.length != doc.channels.length){ 
          if(doc.activeLayer.isBackgroundLayer) doc.activeLayer.isBackgroundLayer = false;
          var startRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          doc.resizeCanvas(doc.width+60, doc.height, AnchorPosition.MIDDLECENTER);
          doc.resizeCanvas(doc.width, doc.height+10, AnchorPosition.BOTTOMCENTER);
          app.preferences.rulerUnits = startRulerUnits;
          }catch(e){alert(e + " - " + e.line);}
          function addMask(){
              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 );
          • 2. Re: Is this possible? crop selection....?
            c.pfaffenbichler Level 9

            It is certainly possible.

            A Selection’s Bounds-property gives the maximal left, top, right, bottom-coordinates of the Selection.


            How is the corresponding alpha channel to be identified?

            Is it the only additional Channel, the first, the last, does it have a specific name …?

            • 3. Re: Is this possible? crop selection....?
              yajiv_viki Level 2

              Hi Paul,


              Thanks for your reply.


              Its awesome code. but background color was changed after run the script.


              Actulally I also done this code from earlier with this reference link  http://forums.adobe.com/thread/581846)


              Thanks for your support....






              var startRulerUnits = app.preferences.rulerUnits;

              app.preferences.rulerUnits = Units.PIXELS;

              try {

                  var docRef = app.activeDocument;

                  var myChannels = docRef.channels;

                  var selAlpha;

                  selAlpha=myChannels.getByName("Alpha 1")

                  docRef.selection.load(selAlpha, SelectionType.REPLACE, false);


                  var SB = activeDocument.selection.bounds;

                  var X1=SB[0]-30;

                  var Y1=SB[1]-8;

                  var X2=SB[2]+29;

                  var Y2=SB[3];



                  app.preferences.rulerUnits = startRulerUnits;

                  } catch (e) {

                     app.preferences.rulerUnits = startRulerUnits;





              function CropSel(x1,y1,x2,y2){

              var id2711 = charIDToTypeID( "Crop" );

                  var desc517 = new ActionDescriptor();

                  var id2712 = charIDToTypeID( "T   " );

                      var desc518 = new ActionDescriptor();

                      var id2713 = charIDToTypeID( "Top " );

                      var id2714 = charIDToTypeID( "#Rlt" );

                      desc518.putUnitDouble( id2713, id2714, y1 ); //y1

                      var id2715 = charIDToTypeID( "Left" );

                      var id2716 = charIDToTypeID( "#Rlt" );

                      desc518.putUnitDouble( id2715, id2716, x1 ); //x1

                      var id2717 = charIDToTypeID( "Btom" );

                      var id2718 = charIDToTypeID( "#Rlt" );

                      desc518.putUnitDouble( id2717, id2718, y2 );//y2

                      var id2719 = charIDToTypeID( "Rght" );

                      var id2720 = charIDToTypeID( "#Rlt" );

                      desc518.putUnitDouble( id2719, id2720, x2 );//x2

                  var id2721 = charIDToTypeID( "Rctn" );

                  desc517.putObject( id2712, id2721, desc518 );

                  var id2722 = charIDToTypeID( "Angl" );

                  var id2723 = charIDToTypeID( "#Ang" );

                  desc517.putUnitDouble( id2722, id2723, 0.000000 );

                  var id2724 = charIDToTypeID( "Wdth" );

                  var id2725 = charIDToTypeID( "#Pxl" );

                  desc517.putUnitDouble( id2724, id2725, 0.000000 );

                  var id2726 = charIDToTypeID( "Hght" );

                  var id2727 = charIDToTypeID( "#Pxl" );

                  desc517.putUnitDouble( id2726, id2727, 0.000000 );

                  var id2728 = charIDToTypeID( "Rslt" );

                  var id2729 = charIDToTypeID( "#Rsl" );

                  desc517.putUnitDouble( id2728, id2729, 0.000000 );

              executeAction( id2711, desc517, DialogModes.NO );



              • 4. Re: Is this possible? crop selection....?
                Muppet Mark Level 5

                You only need to make the selection to read the bounds array, after that why not just use the DOM crop and pass new values?


                var startRulerUnits = app.preferences.rulerUnits;
                app.preferences.rulerUnits = Units.PIXELS;
                try {
                          var docRef = app.activeDocument;
                          docRef.selection.load(docRef.channels.getByName("Alpha 1"), SelectionType.REPLACE, false);
                          var SB = docRef.selection.bounds;
                          docRef.crop( [SB[0]-30,SB[1]-10,SB[2]+30,SB[3]] );
                          app.preferences.rulerUnits = startRulerUnits;
                          } catch (e) {
                                    app.preferences.rulerUnits = startRulerUnits;
                1 person found this helpful
                • 5. Re: Is this possible? crop selection....?
                  yajiv_viki Level 2

                  Hi Muppet Mark,


                  Thanks for your reply.


                  you are really good to simplifying my script.


                  Thanks for cheers...



                  - yajiv

                  • 6. Re: Is this possible? crop selection....?
                    Muppet Mark Level 5

                    Less is usually more… or something like that… I tend to be verbose when first setting out my script once it does what I want… I then go thru it and clean up what I can… For something that is run 50 times a day. I can't see any reason for using action manager code over the regular dom… If it becomes part of a much larger more complex script then you may reconsider…