Try here:
Crop to Selection Action
@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