Skip to main content
heresha93876846
Inspiring
October 6, 2016
Question

Open all pdf files in folder and save as resized jpgs in other folder

  • October 6, 2016
  • 2 replies
  • 431 views

I have a script that checks if a image is wider than 250px and if it is taller than 300px and if any of these statements are true it should resize them to fit within both of these values. Then it should save them as jpgs with the extension large to the filename in a folder named large. However I dont know how to make photoshop open all files in a given folder and execute the script, also I dont know how to export them correctly, photoshop stops when it tries to export.

Here is my code:

// get a reference to the current (active) document and store it in a variable named "doc"
doc
= app.activeDocument; 

// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc
.changeMode(ChangeMode.RGB); 

// these are our values for the END RESULT width and height (in pixels) of our image
var fWidth = 250;
var fHeight = 300;

// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > 300) { doc.resizeImage(null,UnitValue(fHeight,"300"),null,ResampleMethod.BICUBIC); }
else if (doc.width > 250) {  doc.resizeImage(null,UnitValue(fWidth,"250"),null,ResampleMethod.BICUBIC); }


// Makes the default background white
var white = new SolidColor();
white
.rgb.hexValue = "FFFFFF";
app
.backgroundColor = white;

// Convert the canvas size as informed above for the END RESULT app.activeDocument.resizeCanvas(UnitValue(fWidth,"px"),UnitValue(fHeight,"px"));

// our web export options
var options = new ExportOptionsSaveForWeb();
options
.quality = 70;
options
.format = SaveDocumentType.JPEG;
options
.optimized = true;

var newName = 'web-'+doc.name+'.jpg';

doc
.exportDocument(File(doc.path+'/images/'+newName);

This topic has been closed for replies.

2 replies

heresha93876846
Inspiring
October 6, 2016

I want to open all the files in the folder and then make sure all the sizes of the jpgs i output is no taller than 300px and no wider than 250px.

c.pfaffenbichler
Community Expert
Community Expert
October 6, 2016

So you need to trouble-shoot your code and then put the appropriate parts in a for-clause.

c.pfaffenbichler
Community Expert
Community Expert
October 6, 2016

The line

doc.exportDocument(File(doc.path+'/images/'+newName);

contains no reference to the options you set up.

Something like this should open all files of the defined formats from a folder:

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))

};