• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Crop to Selection Action

Community Beginner ,
Apr 24, 2017 Apr 24, 2017

Copy link to clipboard

Copied

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

8) select brush

9) crop (w/out delete crop pixels)

 

JP

 

Message was edited by: Jinda Phommavongsa

TOPICS
Actions and scripting

Views

25.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 28, 2017 Jul 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 "

...

Votes

Translate

Translate
Adobe
Advocate ,
Apr 24, 2017 Apr 24, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2017 Apr 24, 2017

Copy link to clipboard

Copied

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?

Kukurykus_0-1635502827795.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 24, 2017 Apr 24, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 24, 2017 Apr 24, 2017

Copy link to clipboard

Copied

Hi macpawel,

that didn't work either, all that did was crop it back to the files original spec.  this one's got me stumped for sure.  i didn't have this issue when working with CS6.  this is a completely new issue since updating to CC.

Screen Shot 2017-04-24 at 4.27.38 PM.png

The only work around i've found so far is to add the dialog box to the Crop step, but that's defeating the point of my action if i have to manually go in there and move the crop around to fit each image.  would this be something that would need to be applescripted? like i mentioned, i've done this before in older versions no problem.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

Any progress on this? I've made a script that does roughly what you're trying to do, but still have the issue of destructive cropping...otherwise it works great. has anyone figured out that issue?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 04, 2021 Sep 04, 2021

Copy link to clipboard

Copied

When I do this the crop it automatically deletes the cropped pixels - is there away to change this option?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2021 Oct 28, 2021

Copy link to clipboard

Copied

LATEST

Goodness!! Thank you so much!! 🙏🙏🙏

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

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 );

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2017 Jul 28, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 09, 2017 Aug 09, 2017

Copy link to clipboard

Copied

Looks interesting.

In the time I've been away I've been doing this work manually.

Ill try the script. Wish me luck.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2019 May 29, 2019

Copy link to clipboard

Copied

Further to Chuck's reply:

Add this before the code in post #7:

var savedRuler = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

Then add this after the code in post #7:

app.preferences.rulerUnits = savedRuler;    

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 23, 2019 Sep 23, 2019

Copy link to clipboard

Copied

Just want to thank you for the amazing script! Simply excellent to resolve such a pain missing feature!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 11, 2021 Jun 11, 2021

Copy link to clipboard

Copied

I just found this years after you posted it. This worked great on 3 of the 4 I processed - Thank you! This is a breakthrough for me. But on only one, the crop was off-center and disappeared from the active image area. I had to reveal all to find it. I tried a handful of things, since I assume it's something in this particular file that is causing it. I'm not keen enough on Javascript to find where this is happening in the script. If you have any insights for me regarding what I can adjust in my file, please let me know!

 

And many thanks, again! This issue has been bothering me for a while.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

Copied

Is there any way to make it deleting cropped pixels?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 11, 2021 Sep 11, 2021

Copy link to clipboard

Copied

@BQ5EF6 

 

Sure there is, this was covered at the beginning of th topic thread (image menu > crop).

 

It would be helpful to have more background info...

 

* Is this for manual use one image at a time, or would this be required for unattended batch automation?

 

* Where does the selection come from? An alpha channel, a path, a layer – or something else? If a channel, path or layer, does it always have the same name or same position in the panel?

 

* Any other background info which would help somebody creating an action or script to help you?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

My action will be used for a single document with only one layer. The selection comes from another script by JJMack I found in Cropping to a fixed ratio as an action.

 

What I already did is created an action that:

1. uses JJMack's script and makes a selection based on aspect ratio.
2. crops the image using script from this topic.

I can't figure out how to make it deleting cropped pixels. I need that to continue working on my action (and create more steps).

 

Thank you in advance!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

If the aspect selection script from JJMack positions the crop where needed, then I don't understand why you need the crop script, what else does it bring to the party?

 

Just remove the crop script, then in your action, simply record the Image > Crop command on a temp file with a selection, then stop recording:

 

crop.png

 

You could alternatively use the insert menu item command in the action panel to insert the Image > Crop command, which will do the same thing (even if it does not mention delete cropped pixels).

 

It will crop to the selection, deleting pixels outside of the selection. This is what you want, yes? What am I missing?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 18, 2021 Sep 18, 2021

Copy link to clipboard

Copied

Thank you! I forgot about the Image > Crop thing. You're a lifesaver.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2021 Sep 18, 2021

Copy link to clipboard

Copied


@BQ5EF6 wrote:

Thank you! I forgot about the Image > Crop thing. You're a lifesaver.


 

In Chuck's script, you can also have the following change applied so that it is a descructive crop:

 

desc11.putBoolean( idDlt, false );

 

to

 

desc11.putBoolean( idDlt, true );

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines