Script Description
This script will resize a batch of jpeg files. It is set to resize images to half of their current dimensions. It starts by opening image files in Photoshop one at a time and then prompting for confirmation that the new image size is acceptable. If the image needs to be scaled further up or down, you can click either click the “larger” or “smaller” buttons to scale the image in 50px increments.
Looking at the script
The script starts by asking you to select a folder that you would like to save the re-sized images to. Next it askes to to select the folder that the orignal files are in. It then creates the dialog box that will be presented after each image is resized so you have the option to accept the new size or increase or decrease the image size by 50px.
//===== Get the location to save new files to. =====\\
var mySaveToPath = Folder.selectDialog("Please select the folder to save files to.");
mySaveToPath = mySaveToPath + "/";
// ===== Create Dialog Box ===== \\
var myDialog = new Window ('dialog', 'Image Resizer!');
var myBiggerButton = "Bigger";
var mySmallerButton = "Smaller";
var myAcceptedButton = "ok"; // The enter key also works for this button
myDialog.button1 = myDialog.add("button", undefined ,myBiggerButton ); // Make image bigger
myDialog.button1.helpTip = "Increase width by 50px";
myDialog.button1.onClick = function()
{
myImageWidth = myImage.width; //Get current width of image
myImage.activeHistoryState = myImage.historyStates[1]; //Go back in history to state 0
myNewWidth = (myImageWidth + 50); //resize image to current width + 50 pixels
myImage.resizeImage(myNewWidth);
app.refresh() //refresh the screen
this.parent.close(0); //close the dialog
myDialog.show(); //run dialog again
};
myDialog.button2 = myDialog.add("button", undefined ,mySmallerButton ); // Make image smaller
myDialog.button2.helpTip = "Decrease width by 50px";
myDialog.button2.onClick = function()
{
myImageWidth = myImage.width; //Get current width of image
myImage.activeHistoryState = myImage.historyStates[1]; //Go back in history to state 0
myNewWidth = (myImageWidth - 50); //resize image to current width - 50 pixels
myImage.resizeImage(myNewWidth);
app.refresh() //refresh the screen
this.parent.close(0); //close the dialog
myDialog.show(); //run dialog again
};
myDialog.button3 = myDialog.add("button", undefined ,myAcceptedButton ); // Image size is acceptable
myDialog.button3.helpTip = "Save re-sized image";
myDialog.button3.onClick = function()
{
mySaveToFile = new File(mySaveToPath + myImage.name);
myImageOptions = new JPEGSaveOptions();
myImage.saveAs(mySaveToFile, myImageOptions, false);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
this.parent.close(0);
};
myDialog.orientation = 'column';
myDialog.center();
Next the script gets the current unit types and change them to pixels. It keeps the current unit types so it can reset them at the end of the script.
// ===== Get the current unit types ===== \\ startRulerUnits = preferences.rulerUnits; startTypeUnits = preferences.typeUnits; // ===== Change unit types to pixels ===== \\ preferences.rulerUnits = Units.PIXELS; preferences.typeUnits = TypeUnits.PIXELS;
Here’s the meat of the script. It asks you to choose the folder that contains the original files you want to resize. It creates an array of all the jpegs within that folder.
The script uses a for loop to open each file, change the width, and display a dialog box so you can confirm the new size is what you want. The app.refresh() method is key here because that refreshes the screen while the script is running.
An action is required to change the view to “Actual Pixels”. Before running this script make sure you import the “ActualPixels” action and put it into an action set named “Change_View”. The names of the action and action set are case-sensitive here.
Image dimensions are changed by dividing the current width of the image by two.
// ===== Get the working folder, and find the images to open ===== \\
var myPath = Folder.selectDialog("Please select the folder with the images to be resized: ");
var myFolder = new Folder ([myPath]);
var myFolderContents = myFolder.getFiles("*.jpg"); // array of jpegs
var myFileAmount = (myFolderContents.length - 1);
var myInPostionAmount = 0;
// ===== Open, Resize, Get Approval, Save, Close ===== \\
for (i = myFileAmount; i >= 0; i--)
{
app.open(File (myFolderContents[i]));
myImage = app.activeDocument;
myImageWidth = myImage.width;
myNewWidth = (myImageWidth/2); //Resize image
myImage.resizeImage(myNewWidth);
app.doAction("ActualPixels","Change_View");//Show the image at actual pixels
app.refresh();
myDialog.show(); // Display the dialog box
}
Finally it restores the unit types. This script could be easily modified to resize other types of images, resize in inches instead of pixels, or resized based on a more complex formula than just dividing by two.
// ===== Restores unit types to default ===== \\ preferences.rulerUnits = startRulerUnits; preferences.typeUnits = startTypeUnits;

