Skip to main content
Participating Frequently
April 24, 2017
Answered

Crop to Selection Action

  • April 24, 2017
  • 2 replies
  • 30429 views

Hello,

 

Does anyone know how to "Crop to Selection" within an action so that each time the "Crop" step takes place, it crops to the specific selection of the document?  Currently this step works only by remembering the dimensions of the selection which were currently selected during the recording of the action.

 

In a nutshell, I have hundreds of files which contain paths of the object within the document.  These objects are a range of different items so therefore, their paths and thus selections, will have varying dimensions.  I would like to crop each file down to the selection of said object quickly and save before running add'l actions.

 

I would like the "Crop" step within the action to perform independently of the dimensions that were originally recorded.  again, my action only works on the file i recorded it on.  once i apply the same action to a different file, it crops with the previously recorded selection settings and not the current selection.

 

any insight would be greatly appreciated.

 

here are my steps already in place (Photoshop CC):

1) duplicate background

2) select backward layer

3) make fill

4) select forward layer

5) set selection from "path1"

6) Layer via copy

7) set selection

😎 select brush

9) crop (w/out delete crop pixels)

 

JP

 

Message was edited by: Jinda Phommavongsa

This topic has been closed for replies.
Correct answer Chuck Uebele

try adding this script to your action. Just make sure you have a selection for it to crop.

var doc = activeDocument;

try{

    var bound = doc.selection.bounds

    cropToSelection (bound[1], bound[0], bound[3], bound[2])

}

catch(e){}

function cropToSelection(top, left, bottom, right){

    var idCrop = charIDToTypeID( "Crop" );

        var desc11 = new ActionDescriptor();

        var idT = charIDToTypeID( "T   " );

            var desc12 = new ActionDescriptor();

            var idTop = charIDToTypeID( "Top " );

                var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idTop, idPxl, top );

            var idLeft = charIDToTypeID( "Left" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idLeft, idPxl,left );

            var idBtom = charIDToTypeID( "Btom" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idBtom, idPxl, bottom );

            var idRght = charIDToTypeID( "Rght" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idRght, idPxl, right );

        var idRctn = charIDToTypeID( "Rctn" );

        desc11.putObject( idT, idRctn, desc12 );

        var idAngl = charIDToTypeID( "Angl" );

        var idAng = charIDToTypeID( "#Ang" );

        desc11.putUnitDouble( idAngl, idAng, 0.000000 );

        var idDlt = charIDToTypeID( "Dlt " );

        desc11.putBoolean( idDlt, false );

        var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );

        var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );

        var idtargetSize = stringIDToTypeID( "targetSize" );

        desc11.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize );

    executeAction( idCrop, desc11, DialogModes.NO );

    }

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
July 28, 2017

try adding this script to your action. Just make sure you have a selection for it to crop.

var doc = activeDocument;

try{

    var bound = doc.selection.bounds

    cropToSelection (bound[1], bound[0], bound[3], bound[2])

}

catch(e){}

function cropToSelection(top, left, bottom, right){

    var idCrop = charIDToTypeID( "Crop" );

        var desc11 = new ActionDescriptor();

        var idT = charIDToTypeID( "T   " );

            var desc12 = new ActionDescriptor();

            var idTop = charIDToTypeID( "Top " );

                var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idTop, idPxl, top );

            var idLeft = charIDToTypeID( "Left" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idLeft, idPxl,left );

            var idBtom = charIDToTypeID( "Btom" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idBtom, idPxl, bottom );

            var idRght = charIDToTypeID( "Rght" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc12.putUnitDouble( idRght, idPxl, right );

        var idRctn = charIDToTypeID( "Rctn" );

        desc11.putObject( idT, idRctn, desc12 );

        var idAngl = charIDToTypeID( "Angl" );

        var idAng = charIDToTypeID( "#Ang" );

        desc11.putUnitDouble( idAngl, idAng, 0.000000 );

        var idDlt = charIDToTypeID( "Dlt " );

        desc11.putBoolean( idDlt, false );

        var idcropAspectRatioModeKey = stringIDToTypeID( "cropAspectRatioModeKey" );

        var idcropAspectRatioModeClass = stringIDToTypeID( "cropAspectRatioModeClass" );

        var idtargetSize = stringIDToTypeID( "targetSize" );

        desc11.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize );

    executeAction( idCrop, desc11, DialogModes.NO );

    }

Inspiring
July 28, 2017

hmm..this IS non-destructive, but when I tried this with a selection active it crops a 15x15 px square in the upper-left corner of the image but I can't seem to figure out why

Chuck Uebele
Community Expert
Community Expert
July 28, 2017

Are your document units set to pixels? If not, they need to be, of some lines of code need to be added to change the to pixels then back to whatever you were using.

macpawel
Participating Frequently
April 24, 2017

Maybe do somethink like this:

1-8 - would be same

9 - use Image>Crop command instead using Crop Tool

it works fine in my example

Pawel

jindapAuthor
Participating Frequently
April 24, 2017

Hi Macpawel,

 

you're right that option does work, but i need retain the pixels outside the crop bounds because i'd like to run actions afterword which fit the image into set canvas size rules so each image, regardless of the selection dimensions, is placed into the same "pixel padding" specs.  i think using Image>Crop kills that by removing the pixels so that when i expand the canvas back out there are missing areas to my images.

 

 

To further clarify, here is a screenshot of the settings i'd like removed from the Crop step within my action.  is there any way that the Photoshop Crop tool can crop automatically to what the current selection is without remembering what was recorded?

macpawel
Participating Frequently
April 24, 2017

OK, it's possible!

Repeat 1-8 steps and then

Go to Crop Tool (C) use Crop Settings Top-left corner and select Front Image option there

Thats all - I hope

Pawel