Your post inspired me to write new script. I don't know you know how to use them, because you mentioned only about actions, so here few instructins:
- I tested it only in CS2, CS5.1
- I didn't implement any try-catch
- that may not working in same extreme cases
- it uses 2 files cropping.jsx and cropping.exe
- cropping.jsx and cropping.exe should be put on your desktop
- you can ran script from there, no matter which of them you execute
How it works:
- You'll be prompted to put width & height & resolution in xx,xx,xxx where "x" is a digit
- You'll be asked to browse a folder with images you want to crop
- there will be automatically created "Cropped" subfolder in that folder
- you'll have to crop first image (don't rotate but do use (alt+)shift on corners only)
- it starts cropping up to 100 images in choosen folder
- it'll close Photoshop to reopen it after 2 seconds itself with cropping.exe
- it'll start to crop next 100 images in the same folder
- this process will be renewed as far as there left some images
What's else:
Meantime of auto-cropping, a script will be creating 3 notepad files on your desktop to keep metadata, whr.txt (image dimension), pth.txt (images folder path) and bnd.txt (calculation on first image). All of them will be removed after script finish its job.
Cropping.exe is compiled cropping.ahk script with just 3 very short code lines inside. I needed it as far as I know JavaScript is asynchronous language what means you can't run two process at same time. So to execute cropping.jsx I couldn't just give an proper instruction at end of that script, but I had execute AHK script (from JS's), which ran cropping.jsx after its process (with quitting photoshop) ceased. If you don't want to run cropping.exe I attached, you must compile it yourself pasting to notepad that 3 lines you find below and saving it with.ahk extension (but then you need downloaded autohotkey from ahkscript.net)
I love scripting, though I started just one year ago, and only one language I tried was JavaScript, mainly to use Photoshop from its hidden side.
Cropping.exe:
Code: Select all
sleep 2000
run, %A_Desktop%\Cropping.jsx
return
Cropping.jsx:
Code: Select all
#target photoshop
bringToFront();
(function dat() {
var whr = File('~/Desktop/whr.txt')
if (whr.exists) whr.open('r'), prm = whr.read(), whr.close(), dlg = false
else {
PRM = 'Put date of cropping - width, height, resolution, ex. :\n\n'
PRM += '10,20,300 or 20,40,400 or else 30,45,72'
var prm = prompt(PRM, ''); whr.open('w'), whr.write(prm), whr.close(), dlg = true
}
if (!(DAT = /(\d{2}),(\d{2}),(\d{2,3})/i).test(prm)) {alert('Try correctly...'), whr.remove(), dat()}
else {
var pth = File('~/Desktop/pth.txt'), rmv = 'whr.remove(), pth.remove(), bnd.remove(), br = false'
if (!(pth).exists) (fld = Folder.selectDialog()), pth.open('w'), pth.write(fld), pth.close()
else pth.open('r'), fld = File(pth.read()), pth.close()
var crp = fld + '/Cropped', reg = /\.jpg$/i, br = true
if (!File(crp).exists) Folder(crp).create()
var len = (cnt = File(crp).getFiles(reg)).length, fls = fld.getFiles(reg)
if (!len || (cnt[cnt.length - 1].name != fls[fls.length - 1].name)) {
for(var i = len; i < (LEN = len + 100); i++) {
var img = open(File(fls[i])), w = prm.match(DAT)[1], h = prm.match(DAT)[2], r = prm.match(DAT)[3], p = prm.match(DAT)[4]
wPx = Math.round(w * (px = 28.3465)), hPx = Math.round(h * px), preferences.rulerUnits = Units.PIXELS, bnd = File('~/Desktop/bnd.txt');
(wi = (aD = activeDocument).width / wPx) < (he = aD.height / hPx) ? (WI = aD.width, HE = wi * hPx) : (HE = aD.height, WI = he * wPx), f = Math.min(wi, he)
if (!File(crp).getFiles(reg).length) {
var aHS = aD.activeHistoryState; aD.activeLayer.duplicate(), executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO)
var DLG = displayDialogs = (dlg ? DialogModes.ALL : DialogModes.NO); aD.crop(Array(0, 0, WI, HE)), activeDocument.activeLayer = aD.layers[0]
displayDialogs = DialogModes.NO, aD.selection.fill(foregroundColor), DLG, aD.revealAll()
bN = activeDocument.layers[0].bounds; bO = activeDocument.layers[1].bounds
if (((b0 = (bN[0] < bO[0] ? bN[0] : 0) - bO[0]) || !b0) && (bN[2] <= bO[2] || bN[2] > bO[2])) b2 = b0
if (((b1 = (bN[1] < bO[1] ? bN[1] : 0) - bO[1]) || !b1) && (bN[3] <= bO[3] || bN[3] > bO[3])) b3 = b1
aD.activeHistoryState = aHS; bnd.open('w'), bnd.write([bN[0] + b0, bN[1] + b1, bN[2] + b2, bN[3] + b3]), bnd.close()
}
if (bnd) {
bnd.open('r'), BND = bnd.read(), bnd.close(), px = BND.match(/(-?\d+ px),(-?\d+ px),(\d+ px),(\d+ px)/), aD.resizeImage(null, null, r), R = r / 72
activeDocument.crop(Array(new UnitValue(px[1])*R, new UnitValue(px[2])*R, new UnitValue(px[3])*R, new UnitValue(px[4])*R), null, (WI / f)*R, (HE / f)*R)
}
var dlg = false, jpg = new JPEGSaveOptions(); jpg.quality = 12
img.saveAs(File(fls[i].parent + '/Cropped/' + img.name), jpg), img.close(SaveOptions.DONOTSAVECHANGES)
if (fls[i].name == fls[fls.length - 1].name) {eval(rmv); break}
}
}
else alert('The process of cropping is finished for all ' + len + ' files!'), eval(rmv)
if (br) File('~/Desktop/Cropping.exe').execute()
photoshop.quit()
}
})()