Crop from selection in batch

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

tweedceretops
Posts: 1
Joined: Thu Mar 16, 2017 8:07 pm

Crop from selection in batch

Post by tweedceretops »

Hi everyone-

I have what I think is a relatively simple problem that I can't solve on my own (i'm not great at writing my own scripts):

I have a folder of images (products on white) that I need to crop to a square. What I want to do in the script is:

1 - Load the path for image "A" to a selection
2 - Crop (without deleting pixels) based on the selection from step 1
3 - expand the canvas by 10% (to give a little breathing room)
4 - Using expand canvas, match the short dimension to the longer one.

Basically the end result is the masked layer in the center of a square document...

I've got steps 3 and 4 good to go, the problem I'm having is that I can't figure out how to load the selection and use that as the guide for the crop in the context of an action or script. When working on a document one by one, you can load the selection, hit the crop tool, and then the crop box snaps to that selection.

Hopefully someone can give me a hand!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Crop from selection in batch

Post by Kukurykus »

Code: Select all

function sTT(v) {return stringIDToTypeID(v)}

function pDS(v) {// PATH (DE)SELECTION:
eval("(ref1 = new ActionReference()).put" + (v ? (v == 'workPath' ? "Property" : (isNaN(v) ? "Name" : "Index")) : "Class") +
"(sTT('path'), " + (v == 'workPath' ? "sTT(v)" : "v") + ")"); (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1)
executeAction(sTT((v ? '' : 'de') +'select'), dsc1, DialogModes.NO)
// v: null / path name / index
}

function SfP(v1, v2) {// SELECTION FROM PATH:
(ref1 = new ActionReference()).putProperty(sTT('channel'), sTT('selection')); (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1)
eval("(ref2 = new ActionReference()).put" + (!v2 ? "Enumerated" : ((wP = v2 == 'workPath') ? "Property" : (isNaN(v2) ? "Name" : "Index")))
+ "(sTT('path'), " + (!v2 ? "sTT('ordinal'), sTT('targetEnum')" : (wP ? "sTT(v2)" : "v2")) + ")")
dsc1.putReference(sTT('to'), ref2), dsc1.putInteger(sTT('version'), 1), dsc1.putBoolean(sTT('vectorMaskParams'), true)
try{executeAction(sTT(v1), dsc1, DialogModes.NO)} catch(errPth) {}
// v1: 'set', 'addTo', 'subtractFrom', 'interfaceWhite'; v2: null, 'workPath', path name
}

// LAYER FROM CURRENT LAYER:
function cTL() {executeAction(sTT('copyToLayer'), undefined, DialogModes.NO)}

// INTELLIGENT OBJECT CREATION:
function nPL() {executeAction(sTT('newPlacedLayer'), undefined, DialogModes.NO)}

function pD(v) {// PATH DELETION:
eval("(ref1 = new ActionReference()).put" + (!v ? "Enumerated" : ((wP = v == 'workPath') ? "Property" : (isNaN(v) ? "Name" : "Index")))
+ "(sTT('path'), " + (!v ? "sTT('ordinal'), sTT('targetEnum')" : (wP ? "sTT(v)" : "v")) + ")");
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1), executeAction(sTT('delete'), dsc1, DialogModes.NO)
// v: null, 'workPath', path name / index
}

pDS('workPath'), SfP('set'), nPL(), cTL(), (lyr0 = (aD = activeDocument).layers[1]).visible = false, aD.trim(TrimType.TRANSPARENT)
aD.resizeCanvas(w = (w = aD.width) + w / 100 * 10, h = (h = aD.height) + h / 100 * 10), aD.resizeCanvas(mm = Math.max(w, h), mm)
aD.activeLayer.remove(), pD(), lyr0.isBackgroundLayer = true
It can be made clearly mathematically, without all steps meantime I used to get the final result, that the only thing there would be done was cropping - but maybe I'm wrong. I'll try it other time...
Attachments
Square from Rectangle.rar
(1.1 KiB) Downloaded 823 times