Posted: Sun Nov 13, 2011 9:58 am Post subject: script to close all files below size and "rename"
Hello,
I am working on a similar problem. I want to use the Image Processor to automatically crop 12000 files and save them as jpg in an identical file tree.
I looked around and found a few scripts. Depending on your needs - this could be a solution, but it only leaves one image open. In my case bigger than 1850 pixels - you would have to adjust that. I have set it to more than half my original canvas, so that only one image stays open. If you can't do that you would have to find a smarter way to close all but one images. I didn't write the routine to look for the biggest image either - I copied it from here:
Please note, that I have not done any scripting before and just have spent my first few hours adjusting this code - but I thought it might help you.
As far as I found out you cannot rename the activeDocument, but only do a saveAs to "rename" it - so I save my files to the trash in minimum quality, but you would have to adjust the path if you are not on OS-X or some Unix derivate. I'm sure there is an OS independent way, but I don't know it yet. I will keep you posted if I find out more.
It is doing what you want I think, but the image processor doesn't want to process the files when I run an action that executes this script, so it doesn't solve my problem.
Hopefully someone can help me correcting this.
Cheers - Robert.
Code:
// crop and straighten - close files below either certain width or height in pixels - rename to original filename;
// use it at your own risk;
#target photoshop
app.bringToFront;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var theDocument = app.activeDocument;
var origDocName = theDocument.name;
var docCount = app.documents.length;
// crop and straighten;
var idCropPhotosAutozerozerozeroone = stringIDToTypeID( "CropPhotosAuto0001" );
executeAction( idCropPhotosAutozerozerozeroone, undefined, DialogModes.ALL );
// close the original without saving only if cropAndStraughten created new documents
if(app.documents.length > docCount) theDocument.close(SaveOptions.DONOTSAVECHANGES);
// close any open images below sizes;
var theDocs = app.documents;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// iterate through open files;
for (var m = theDocs.length - 1; m >= 0; m--) {
var theDoc = theDocs[m];
app.activeDocument = theDoc;
var theWidth = theDoc.width;
var theHeight = theDoc.height;
// for closing files below certain width and height change || to &&;
if (theWidth < 1850 || theHeight < 1850) {
theDoc.close(SaveOptions.DONOTSAVECHANGES)
}
else {}
};
Posted: Wed Nov 16, 2011 8:30 am Post subject: THANKS SO MUCH
Hello,
very Funny Paul - I'm totally on it though. I'm Austrian/Aussie, so it's just a long way from one place to the other :-).
It was really awesome of you to respond so quickly, although I didn't really ask for a solution yet - that's what I call good service. I actually planned on getting a bit better with scripting - then my questions would appear a bit more informed at least.
I tried the script you supplied - and it worked great - did right just what I wanted. I have done 2000 pictures that were somewhat more urgent, but I have 10000 to go and there will be a lot more. I converted a slide projector into a slide copier, so all the family friends want their slides copied now.
So I thought I could make the code better as an exercise. The problem is the following:
If the crop and straighten fails this script either puts in half a picture, or just a thin strip (if it has more than 1850 px on on side) - or even worse - if just saves nothing, as it has closed all images, so I have to manually hunt the missing images.
My plan was to get good enough to multiply height x width, so it actually demands a minimum pictures size and if this is not met go back and open die original image again and save it without cropping - so I don't have to copy them in manually again. Would that be an appropriate/usual method to find the picture size ? First thing for me would be to find out how I let this for loop open the same image again and do a saveAS into the file tree using your function instead of exiting - I think that would be an easy enough task for a beginner... hopefully.
Also I am using dng - so I might have to get my head around the CameraRAWOpenOptions.CameraRAWSize, so that it opens them always in jpg sRGB 8 bit original size (4416x3312 in my case), but the options in the documentation have 6 steps and my raw tool has 7.
EXTRALARGE=5120 x 4096
LARGE=4096 x 2731
MAXIMUM=6144 X 4096
MEDIUM=3072 x 2048
MINIMUM=1536 x 1024
SMALL=2048 x 1365
vs... see attached.
I am a bit confused there, but I am majorly confused about the syntax and scripting in general :-D - so it might take a while until I can come back to you and ask for more help.
I thank you all very much so far - it will be quite an effort for me to get my head around scripting, but with your help I am confident.
Cheers - Rob.
PS: will attach and image that crop and straighten usually fails on, so that you see what I was working on and it gets more tangible.
Last edited by helixpteron on Wed Nov 16, 2011 9:04 am; edited 3 times in total
I changed the code a tiny bit - making my first step. I don't need file numbering, so I commented it out, but I know this is not actually the way it's done. Just so that you can see how bad I am still
I do need some help right now:
I am using the extendScripot toolkit and I can't work out how the console works. I want to do "step into" and see what value my variables have while the code runs from step to step - how can this be done ?
Thanks a lot.
Cheers - Rob.
Code:
#target photoshop
app.bringToFront();
main();
function main(){
topLevelFolder = Folder.selectDialog("Please select the top level folder with Files to process");
newTopLevelFolder = Folder.selectDialog("Please select the new top level folder (Output)");
var folderList = [];
folderList = FindAllFolders( topLevelFolder, folderList);
folderList.unshift (topLevelFolder);
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
for(var f in folderList){
var fileList = folderList[f].getFiles(/\.(jpg|tif|psd|dng)$/i); //RB - added |dng
for (var z in fileList){
open(fileList[z]);
activeDocument.flatten();
executeAction( app.stringIDToTypeID('CropPhotosAuto0001'), undefined, DialogModes.NO );
app.documents[0].close(SaveOptions.DONOTSAVECHANGES);
if(!documents.length) continue;
var Name = fileList[z].name.replace(/\.[^\.]+$/, '');
var saveFileFolder = createRelativeFolder(newTopLevelFolder, folderList[f]);
Count=0;
for(var r =0; r<app.documents.length;r++){
activeDocument = app.documents[r];
var doc = activeDocument;
if (doc.width >= 1850 || doc.height >= 1850) {
Count++;
var saveFile = File(saveFileFolder + "/" + Name + ".jpg"); //RB omit file numbering +"-#" + Count
SaveJPEG(saveFile, 10); //RB - changed quality 8 to 10
}
}
while (app.documents.length) {
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
app.preferences.rulerUnits = startRulerUnits;
}
function createRelativeFolder(newTopLevelFolder, existingFolder){
var PathBits = decodeURI(newTopLevelFolder).toString().split('/');
var TmpArray = decodeURI(existingFolder).toString().split('/');
for(var a = 3;a<TmpArray.length;a++){
PathBits.push(TmpArray[a]);
}
for(var a = 2;a<PathBits.length;a++){
var newFolder = '';
for( var s = 0;s<a+1;s++){
newFolder += PathBits[s].toString() +'/';
}
var toCreate = Folder(newFolder);
if(!toCreate.exists) toCreate.create();
}
return toCreate;
}
function FindAllFolders( srcFolderStr, destArray) {
var fileFolderArray = Folder( srcFolderStr ).getFiles();
for ( var i = 0; i < fileFolderArray.length; i++ ) {
var fileFoldObj = fileFolderArray[i];
if ( fileFoldObj instanceof File ) {
} else {
destArray.push( Folder(fileFoldObj) );
FindAllFolders( fileFoldObj.toString(), destArray );
}
}
return destArray;
};
function SaveJPEG(saveFile, jpegQuality){
var doc = activeDocument;
if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(File(saveFile), jpgSaveOptions, true,Extension.LOWERCASE);
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum