Copy link to clipboard
Copied
Hi, hoping someone can help. I am trying to create a scrip/batch process to crop multiples files, resize to 1280x1280 and then save as a PNG file. I am very new to scripting however after a while of Googling have managed to put together a script from various forums which works on a single file that is open in Photoshop. However I have found some code which I hope will allow me to select a whole folder and batch script all the files inside. I seem to have run into a error and hoping someone can assist.
The scrip runs and browse file window opens but when I select a folder I get the following error.
Error 1233: Expected a reference to an existing File/Folder
Line:6
-> app.open(File(theFiles
Here is the code I have so far,
#target photoshop var theFolder = Folder.selectDialog("select folder"); if (theFolder) { var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i); for (var m = 0; m < theFiles.length; m++) } app.open(File(theFiles doc = app.activeDocument; doc.changeMode(ChangeMode.RGB); var thumbDim = 1280; // the dimension of the square // crop to a big square, conditionally, based on dimensions if (doc.height > doc.width) { doc.resizeCanvas(doc.width,doc.width,AnchorPosition.TOPCENTER); } else { doc.resizeCanvas(doc.height,doc.height,AnchorPosition.MIDDLECENTER); } // specify that our units are in pixels, via creation of a UnitValue object doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC); |
Thanks
1 Correct answer
Change:
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}
to
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);
Change:
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
to
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);}}
Explore related tutorials & articles
Copy link to clipboard
Copied
Change:
for (var m = 0; m < theFiles.length; m++) }
to:
for (var m = 0; m < theFiles.length; m++) {
Change:
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);
to
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}
Copy link to clipboard
Copied
Thank you very much for your help. That worked perfectly. I have found some more code which will then save as PNG and rename the files. I just tried it on 3 files however only one file was saved. Do I need to add something to it? Thank you.
Copy link to clipboard
Copied
You want me to advice for something I don't see?
Copy link to clipboard
Copied
My mistake, apologies. Here is the whole code with the new bit in bold. Thank you.
#target photoshop
var theFolder = Folder.selectDialog("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
for (var m = 0; m < theFiles.length; m++) {
app.open(File(theFiles
doc = app.activeDocument;
doc.changeMode(ChangeMode.RGB);
var thumbDim = 1280; // the dimension of the square
// crop to a big square, conditionally, based on dimensions
if (doc.height > doc.width) {
doc.resizeCanvas(doc.width,doc.width,AnchorPosition.TOPCENTER);
}
else {
doc.resizeCanvas(doc.height,doc.height,AnchorPosition.MIDDLECENTER);
}
// specify that our units are in pixels, via creation of a UnitValue object
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}
// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 100;
options.format = SaveDocumentType.PNG;
options.optimized = true;
var newName = 'web-'+doc.name+'.png';
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
Copy link to clipboard
Copied
Change:
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}
to
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);
Change:
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);
to
doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);}}
Copy link to clipboard
Copied
Thank you very much for your help. It is now working perfectly, really appreciate it.
Copy link to clipboard
Copied
Btw you did mistake marking your question as correct answer. Change that as correct one was that from me.
Copy link to clipboard
Copied
If you download my crafting actions package there is a plug-in script in it that will enable you to create an simple action to to that. The plug-in script can create aspect ratio selection selection or path for documents the are rectangular or elliptical. So you can record a simple action to do what you want.
Step 1 Menu File>Automate>Aspect Ratio Selection
Step 2 Menu Image>Crop
Aspect Ratio Selection is my plug-ins script in its dialog you would set a 1 1 aspect ratio, centered, rectangle, Replace, Selection, 0 feather These settings will be recorded into the action step and used when the action is played no dialog will be displayed.
You would batch this action using menu File>Automate>Image Processor Pro...
Image Processor Pro is a Plug-in Script you can download from the web and install into Photoshop. You can save many type of Image file resized to fit area sizes.
https://sourceforge.net/projects/ps-scripts/files/Image%20Processor%20Pro/v3_2%20betas/
My Crafting Actions Package UPDATED Aug 10, 2014 Added Conditional Action steps to Action Palette Tips.
Contains
- Action Actions Palette Tips.txt
- Action Creation Guidelines.txt
- Action Dealing with Image Size.txt
- Action Enhanced via Scripted Photoshop Functions.txt
- CraftedActions.atn Sample Action set includes an example Watermarking action
- Sample Actions.txt Photoshop CraftedActions set saved as a text file.
- More then a dozen Scripts for use in actions
Copy link to clipboard
Copied
Thank your for the info. Will keep this in mind for future projects.
Copy link to clipboard
Copied
Just curious but how could I achieve a tighter crop on the image. So less of a border around the image? What code would I need to edit. Thanks
Copy link to clipboard
Copied
Post screenshot of that you want to achieve so I can imagine it fully.
Copy link to clipboard
Copied
Hi here is an example. The first image is what I am looking for. Less of a border with the crop.
Copy link to clipboard
Copied
If the subject is smaller width and height are both less than 1280 the document would be trimmed smaller then 1280 by 12880. However, the script could the add canvas to make the document 1280 by 1280 the subject have four transparent boarders. If the subject width and height are not both smaller than 1280 the trimmed document will be larger than 1280x1280 at least in width or height and will most likely not be square. The script could then add canvas to make the canvas square and then resize the document 1280x1280 the subject will just have transparent boarders on two side they will be across from each other.
Copy link to clipboard
Copied
If the boarder is a consistent color you may be able to use Image Trim to trim off the boarders. Or you may be able to use use the magic want in contiguous mode or quick selection tools to select the boarder then invert the selection to select the subject or use select subject to select the subject once you have that active selection you can crop to the active selections bounds.
Copy link to clipboard
Copied
Hi, there is no border in the images. Would doing this change the size? I want to maintain dimensions of 1280x1280. Thanks
Copy link to clipboard
Copied
function crp() {
aD = activeDocument
displayDialogs = DialogModes.NO
rulerUnits = preferences.rulerUnits
preferences.rulerUnits = Units.PIXELS
function sTT(v) {return stringIDToTypeID(v)}
res = aD.resolution, h = aD.height, w = aD.width
dsc1 = new ActionDescriptor(), dsc2 = new ActionDescriptor()
dsc2.putUnitDouble(sTT('top'), sTT('pixelsUnit'), t = h / 10)
dsc2.putUnitDouble(sTT('left'), sTT('pixelsUnit'), l = w / 10)
dsc2.putUnitDouble(sTT('bottom'), sTT('pixelsUnit'), h - t)
dsc2.putUnitDouble(sTT('right'), sTT('pixelsUnit'), w - l);
dsc1.putObject(sTT('to'), sTT('rectangle'), dsc2)
dsc1.putUnitDouble(sTT('angle'), sTT('angleUnit'), 0)
dsc1.putEnumerated(sTT('cropAspectRatioModeKey'),
sTT('cropAspectRatioModeClass'), sTT('targetSize'))
dsc1.putUnitDouble(sTT('width'), sTT('pixelsUnit'), w)
dsc1.putUnitDouble(sTT('height'), sTT('pixelsUnit'), h)
dsc1.putUnitDouble(sTT('resolution'), sTT('densityUnit'), res)
executeAction(sTT('crop'), dsc1), preferences.rulerUnits = rulerUnits
}
crp()
Copy link to clipboard
Copied
Thank you. In relation to the rest of the code. Would I paste this at the start?
Copy link to clipboard
Copied
Put it after:
doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);
Copy link to clipboard
Copied
Thanks very much. yes sorry I believe I thanked myself in error.

