Skip to main content
Known Participant
September 11, 2023
Answered

Crop the selection ( Photoshop Scripts)

  • September 11, 2023
  • 3 replies
  • 659 views

Hi all Sir . . . 

Please help me

I need

Crop the selection ( photoshop scripts ) 

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

Presuming ExtendScript/JavaScript and that you already have an active selection:

 

try {
var idcrop = stringIDToTypeID( "crop" );
executeAction( idcrop, undefined, DialogModes.NO );
} catch (e) {
    alert("Do you have an active selection?");
}

 

This one is in a function and can delete or retain cropped pixels:

 

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

 

3 replies

c.pfaffenbichler
Community Expert
Community Expert
September 12, 2023

@Thihasoe , please remember to mark a post as »Correct Answer« if it resolves your problem. 

ThihasoeAuthor
Known Participant
September 12, 2023

@c.pfaffenbichler  

I'm so sorry, sir
Because I didn't know.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 11, 2023

Presuming ExtendScript/JavaScript and that you already have an active selection:

 

try {
var idcrop = stringIDToTypeID( "crop" );
executeAction( idcrop, undefined, DialogModes.NO );
} catch (e) {
    alert("Do you have an active selection?");
}

 

This one is in a function and can delete or retain cropped pixels:

 

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

 

ThihasoeAuthor
Known Participant
September 12, 2023

@Stephen Marsh 

Thank you so much sir . . .

Community Expert
September 11, 2023
ThihasoeAuthor
Known Participant
September 12, 2023

@Mariam Hovhannesyan 

Thank you Sir