5 Replies Latest reply on Sep 2, 2010 8:47 PM by JJMack

    Cropping a selection (JS CS3)

    jmw107 Level 1

      Hi -- I am developing a script in which a user chooses one of three predetermined sizes for an image from a dialog. This displays the crop selection but does not crop until the user has a chance to change the crop area (but not the size).

       

      I can create the target crop selection with an action, but I cannot figure out how to crop to the selection area. The size will always be the same -- that I know how to enter in the script. But I do not know how to tell the script to crop the selected area.

       

      Is there a way using the app.activeDocument.crop() command that I can have it crop to only the selected area?

       

      Or is there a simple command I can put in the script to simply "crop" after the user changes the crop area and hits the OK button in my dialog. (Obviously, the user can simply doublelclick to set the crop -- but the OK button will do other things after the crop, and I am trying to eliminate as many clicks as possible.)

       

      Any help would be greatly appreciated.

        • 1. Re: Cropping a selection (JS CS3)
          Muppet Mark-QAl63s Level 4

          It has always been the app's default behavior when using 'crop' from the menubar with an active selection… So using a recorded function may help you out…?

           

          #target photoshop
          
          app.bringToFront();
          
          var docRef = app.activeDocument;
          //docRef.flatten();
          crop();
          //docRef.selection.deselect();
          
          // Crop to Active Selection
          function crop() {
               function cTID(s) { return app.charIDToTypeID(s); };
                    var desc001 = new ActionDescriptor();
               executeAction(cTID('Crop'), desc001, DialogModes.NO);
          }
          

          • 2. Re: Cropping a selection (JS CS3)
            Michael L Hale Level 5

            You don't need to use scriptlistener. Selection.bounds was fixed in CS3. Run the line below with an active selection and it will crop the document to the selection.

             

            app.activeDocument.crop(app.activeDocument.selection.bounds);
            
            • 3. Re: Cropping a selection (JS CS3)
              Muppet Mark-QAl63s Level 4

              Yeah l'll be be a lot happier when I get a working version of that… Keep forgetting Im in the dark ages…

              • 4. Re: Cropping a selection (JS CS3)
                jmw107 Level 1

                Thanks -- I think I am running into one issue:

                 

                When you select the crop for the photo, the script item in the pulldown menu is greyed out. I am not using this for my script -- I am using a palette dialog. But I believe it is the same issue -- having the selection made but not cropped seems to be halting my ability to run scripts.

                 

                Am I missing something here? Is there anyway around it?

                 

                thanks again

                • 5. Re: Cropping a selection (JS CS3)
                  JJMack Most Valuable Participant

                  jmw107 wrote:

                   

                  Hi -- I am developing a script in which a user chooses one of three predetermined sizes for an image from a dialog. This displays the crop selection but does not crop until the user has a chance to change the crop area (but not the size).

                   

                  I can create the target crop selection with an action, but I cannot figure out how to crop to the selection area. The size will always be the same -- that I know how to enter in the script. But I do not know how to tell the script to crop the selected area.

                   


                  I do not understand exactly your above description about user has a chance to change the crop area (but not the size). Do you mean they can move the position of the fixed crop size or the can position and size a part of the image to the crop area.  If it is they can move and size part of the image into the crop area.  I do that a different way. I set up a layer above the layer the image is on to act as a crop mask. Then I put the user into an menu File>Place dialog where the user select the image which Photoshop then open into a smart object layer below the cropping layer. The user is then in the Place interactive transform dialog where they size and position the part of the image the want within the cropping area. When they commit the place transform I delete the layer used as a cropping mask and add a layer mask to the smart object layer to mask all but the crop area.  I don't actually Crop the layer after the process runs the image is still intact and a smart object layer with a cropping layer mask.  The user can if the wish to tweak the image still without quality loss.