Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Need script to crop to path without deleting pixels

Community Beginner ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Hi All,

I need some help finding a script that I'm sure must be out there (or easy to create for those who actually know hot to write scripts..)
I'm trying to batch crop multiple photos (PSDs) from a path that is already in the files (named "Crop (Capture One)".

With actions I can only go so far, because:

-When converting path to selection and using crop tool, the crop tool records specific XY coordinates, not the path coordinates.

-Using Image/Crop makes the right crop but deletes the pixels outside the crop area.

I would like to find a way to directly crop according to the path, without deleting the pixels outside the crop zone...

 

Any Ideas out there?

Thanks a million,

J

TOPICS
Actions and scripting

Views

47

Likes

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

Adobe Community Professional , Sep 12, 2021 Sep 12, 2021
@jog16137815  Here is the full code: /* Crop to Named Path Retaining Pixels.jsx v1.1, Stephen Marsh, 13th September 2021 https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-script-to-crop-to-path-without-deleting-pixels/td-p/12378330 Need script to crop to path without deleting pixels */ if (app.documents.length) { var doc = app.activeDocument; var savedRuler = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS; try { doc.pathItems...

Likes

Translate

Translate
Adobe Community Professional ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

Likes

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
Adobe Community Professional ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

@jog16137815 

 

Here is the full code:

 

/*
Crop to Named Path Retaining Pixels.jsx
v1.1, Stephen Marsh, 13th September 2021

https://community.adobe.com/t5/photoshop-ecosystem-discussions/need-script-to-crop-to-path-without-deleting-pixels/td-p/12378330
Need script to crop to path without deleting pixels
*/

if (app.documents.length) {
    var doc = app.activeDocument;
    var savedRuler = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;
    try {
        doc.pathItems.getByName("Crop (Capture One)");
    } catch (e) {
        alert('There is no path named "Crop (Capture One)"');
    }
    loadCropPath("Crop (Capture One)");
    try {
        var bound = doc.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');
    }
    app.preferences.rulerUnits = savedRuler;
} else {
    alert('A document must be open to use this script!');
}

/******************** FUNCTIONS ********************/

// Courtesy of Chuck Uebele
function cropToSelection(top, left, bottom, right, retainPixels) {
    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);
}

function loadCropPath(pathName) {
    var idset = stringIDToTypeID("set");
    var desc1606 = new ActionDescriptor();
    var idnull = stringIDToTypeID("null");
    var ref418 = new ActionReference();
    var idchannel = stringIDToTypeID("channel");
    var idselection = stringIDToTypeID("selection");
    ref418.putProperty(idchannel, idselection);
    desc1606.putReference(idnull, ref418);
    var idto = stringIDToTypeID("to");
    var ref419 = new ActionReference();
    var idpath = stringIDToTypeID("path");
    ref419.putName(idpath, pathName); // path name
    desc1606.putReference(idto, ref419);
    var idversion = stringIDToTypeID("version");
    desc1606.putInteger(idversion, 1);
    var idvectorMaskParams = stringIDToTypeID("vectorMaskParams");
    desc1606.putBoolean(idvectorMaskParams, true);
    executeAction(idset, desc1606, DialogModes.NO);
}

 

Downloading and Installing Adobe Scripts

Likes

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

Amazing!

A million thanks, I was looking for this since for ever, you're a saver 🙂

Likes

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
Adobe Community Professional ,
Sep 12, 2021 Sep 12, 2021

Copy link to clipboard

Copied

LATEST

Your welcome!

 

EDIT: Original v1.0 code slightly modified to include error checking for target path.

Likes

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