Copy link to clipboard
Copied
Hello,
I am new to photoshop scripting and I am looking for a script that sets the biggest document, in terms of file size, to active and brings it to the front on photoshop. I am looking to do this because of the 'crop and straighten' function in photoshop. While batch cropping a thousand photos at a time, sometimes the 'crop and straighten' function crops an image into multiple documents. This isn't a problem, but sometimes then photoshop will perform this function and then set the active document to, for example, a piece of dust. In this example, photoshop will have cropped multiple documents from the original photo and the desired cropped document won't be set to active and instead a cropped image of dust. This doesn't happen too often, but in some cases could happen to maybe 10% of the batched images. To solve this problem, I would need photoshop to find the biggest document loaded in photoshop after it has performed the 'crop and straighten' function. I imagine it would set the original photo as active because that is still loaded, but that can be solved by closing that image first after the 'crop and straighten' is performed. Thank you for the help, everything is greatly appreciated.
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var sizes = [];
for (var i = 0; i < app.documents.length; i++)
sizes.push({size: app.documents[i].width.value * app.documents[i].height.value, idx:i});
sizes.sort(cmp);
function cmp(a, b)
{
if (a.size > b.size) return -1;
if (a.size < b.size) return 1;
return 0;
}
app.preferences.rulerUnits = old_units;
app.activeDocument = app.documents[sizes[0].idx]; // open the largest docum
...
Copy link to clipboard
Copied
That would be a job for a file organizer Lightroom,Bridge, File Explorer, Finder. Photoshop is not a file editor it edit document. It can save many image file formats from document
There is a script on the web the will batch process files through crop and straighten and save the new documents the crop and straighten creates. No document will remain open in Photoshop. Search for that script. I think that is what you need.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var sizes = [];
for (var i = 0; i < app.documents.length; i++)
sizes.push({size: app.documents[i].width.value * app.documents[i].height.value, idx:i});
sizes.sort(cmp);
function cmp(a, b)
{
if (a.size > b.size) return -1;
if (a.size < b.size) return 1;
return 0;
}
app.preferences.rulerUnits = old_units;
app.activeDocument = app.documents[sizes[0].idx]; // open the largest document
//app.activeDocument = app.documents[sizes[1].idx]; // open the second largest document
Copy link to clipboard
Copied
This worked perfectly! Thank you so much. This will save me so much time and make batch cropping much more accurate.
Copy link to clipboard
Copied
little note:
If two of the files are the same size, you will unfortunately not receive any notice.
That could be a problem, but it doesn't have to be.