Skip to main content
Marin George
Participating Frequently
October 27, 2022
Answered

image > crop without deleting cropped pixels

  • October 27, 2022
  • 1 reply
  • 2087 views

hello

 

i'm having a blockage in my daily work routine as i found a way to edit faster my photos

 

it's a bit long to read but please hear me out

 

don't ask me why I work like this, this is not the issue here, so:

 

I'm using an action that says the following:

 

1. layer via copy

2. select subject

3. crop 

4. image size 990

5. canvas size 1485

6. select previous document

 

so, i'm having trouble with the 3rd step, the crop step. If i just use CROP as in pressing C, enter, enter and save it like that, then all of my photos will be done with the location of the crop as i recorded it. that's no bueno

 

if i use image crop and save it like that, the action says it makes that crop as it's supposed to be, but it deletes my cropped pixels.

 

so the question at hand here, is there any way of making an action, with IMAGE>CROP recorded in an action WITHOUT DELETING CROPPED PIXELS?

 

thank you, kindly

georgie

 

 

 

 

This topic has been closed for replies.
Correct answer Stephen Marsh

hello, 

 

i have changed my pc due to work stuff ... and now I am trying to run this action on photoshop 20.0.2 and it's not working anymore

 

script that was working was this

{
var bound = activeDocument.selection.bounds;
cropToSelection(bound[1], bound[0], bound[3], bound[2], false); // delete cropped pixels

}

// Courtesy of Chuck Uebele
function cropToSelection(top, left, bottom, right, retainPixels) {
var origRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
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, retainPixels); // delete cropped pixels = true | false
var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
var idtargetSize = stringIDToTypeID("targetSize");
desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
executeAction(idCrop, desc11, DialogModes.NO);
app.preferences.rulerUnits = origRuler;
}


but now on the 20.0.2 I get the following error
Error 1302: No such element
Line: 2
-> var bound = activeDocument.selection.bounds;

 

 

 


@Marin George – The error appears to indicate that there is no selection.

 

EDIT: Try this -

 

try {
    var bound = activeDocument.selection.bounds;
    cropToSelection(bound[1], bound[0], bound[3], bound[2], false); // delete cropped pixels
} catch (e) {
    alert('There was an unexpected error when cropping');
}

// Courtesy of Chuck Uebele
function cropToSelection(top, left, bottom, right, retainPixels) {

    var origRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    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, retainPixels); // delete cropped pixels = true | false
    var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
    var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
    var idtargetSize = stringIDToTypeID("targetSize");
    desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
    executeAction(idCrop, desc11, DialogModes.NO);

    app.preferences.rulerUnits = origRuler;
}

1 reply

Stephen Marsh
Community Expert
Community Expert
October 27, 2022

Not that I am aware of.

 

It always worked that way, before Adobe added the ability to retain cropped pixels with the crop tool. I think it is just there as a label/identifier – not signalling that there is another ability possible in an action (yet?).

 

It is possible in a script though! And the script can be recorded into an Action as an individual step, replacing the other step.

 

try {
    var bound = activeDocument.selection.bounds;
    cropToSelection(bound[1], bound[0], bound[3], bound[2], false); // delete cropped pixels
} catch (e) {
    alert('There was an unexpected error when cropping');
}

// Courtesy of Chuck Uebele
function cropToSelection(top, left, bottom, right, retainPixels) {
    var origRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    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, retainPixels); // delete cropped pixels = true | false
    var idcropAspectRatioModeKey = stringIDToTypeID("cropAspectRatioModeKey");
    var idcropAspectRatioModeClass = stringIDToTypeID("cropAspectRatioModeClass");
    var idtargetSize = stringIDToTypeID("targetSize");
    desc11.putEnumerated(idcropAspectRatioModeKey, idcropAspectRatioModeClass, idtargetSize);
    executeAction(idCrop, desc11, DialogModes.NO);
    app.preferences.rulerUnits = origRuler;
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save the text file as .txt
  5. Rename the file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below)

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html#Photoshop

 

Marin George
Participating Frequently
October 27, 2022

 

Stephen Marsh
Community Expert
Community Expert
October 27, 2022

What Program is the error screenshot from? Photoshop, or ESTK?

 

Is there an active selection before the script step?