1 Reply Latest reply: Sep 4, 2014 2:40 PM by pixxxel schubser RSS

    Applescript, crop based on selection without deleting pixels

    Mattmcquiff Community Member

      I'm trying to crop the image in the same way you would in photoshop.

      You select the area with the marquee then select the crop tool, this then has the tick box to crop without deleting the pixels.


      How can I do this please?

      I believe the crop that I use below is from the menu item and not the crop tool.




      tell application id "com.adobe.photoshop"

        set ruler units of settings to pixel units

        tell current document

        trim basing trim on top left pixel

       

        set {height:originalHeight, width:originalWidth} to it

       

        log originalWidth

        log

       

        select region {{0, (originalHeight / 2)}, {originalWidth, (originalHeight / 2)}, {originalWidth, originalHeight}, {0, originalHeight}} without antialiasing

        set theCropBounds to bounds of selection

       

        --crop based on selection without deleting pixels as per crop tool not crop in the menu item

        crop bounds theCropBounds

       

       

        --reveal all

        end tell

       

      end tell

        • 1. Re: Applescript, crop based on selection without deleting pixels
          pixxxel schubser Community Member

          Hi Mattmcquiff,

          I'm not familiar with AppleScript.

           

          But this JavaScript (with a bit AM code) should do the same:

          // CropBasedOnHalfOfPicture.jsx
          // https://forums.adobe.com/thread/1564681
          // regards pixxxelschubser
          
          var strtRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.POINTS; 
          
          var originalWidth = app.activeDocument.width;
          var originalHeight = app.activeDocument.height;
          var halfHeight = Math.floor (originalHeight / 2);
          
          var desc = new ActionDescriptor();
          var idT = charIDToTypeID( "T   " );
          var desc1 = new ActionDescriptor();
              desc1.putUnitDouble( charIDToTypeID( "Top " ), charIDToTypeID( "#Rlt" ), halfHeight );
              desc1.putUnitDouble( charIDToTypeID( "Left" ), charIDToTypeID( "#Rlt" ), 0 );
              desc1.putUnitDouble( charIDToTypeID( "Btom" ), charIDToTypeID( "#Rlt" ), originalHeight );
              desc1.putUnitDouble( charIDToTypeID( "Rght" ), charIDToTypeID( "#Rlt" ), originalWidth );
              
              desc.putObject( idT, charIDToTypeID( "Rctn" ), desc1 );
              desc.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), 0.000000 );
              
              desc.putBoolean( charIDToTypeID( "Hd  " ), true );
              
              desc.putUnitDouble( charIDToTypeID( "Wdth" ), charIDToTypeID( "#Pxl" ), 0.000000 );
              desc.putUnitDouble( charIDToTypeID( "Hght" ), charIDToTypeID( "#Pxl" ), 0.000000 );
              desc.putUnitDouble( charIDToTypeID( "Rslt" ), charIDToTypeID( "#Rsl" ), 0.000000 );
              
          executeAction( charIDToTypeID( "Crop" ), desc, DialogModes.NO );
          
          app.preferences.rulerUnits = strtRulerUnits;
          

           

          Have fun