Resize Image in Phostoshop using Applescript is very slow

I’ve noticed a major slowdown when using a script to tell Photoshop CS2 to resize an image compared to doing it manually in Photoshop.
AppleScript code that I have used is as follows:


tell application "Finder"
	set myFilePath to choose file with prompt "Choose a file"
	set fileName to name of myFilePath
	set nameext to name extension of myFilePath
	set filekind to kind of myFilePath
end tell

tell application "Adobe Photoshop CS2"
	activate
	set NewFilePath to "Macintosh:Users:premedia:Desktop:TESTING:untitled folder:untitled folder:" & fileName as string
	open myFilePath
	tell document 1
		change mode to RGB
		set photoWidth to width * 72
		set photoHeight to height * 72
		if resolution is greater than 72 then
			resize image resolution 72
		end if
		if photoWidth is greater than or equal to 600 or photoHeight is greater than or equal to 600 then
			if photoWidth is greater than or equal to photoHeight then
				resize image width 600
			else
				resize image height 600
			end if
		end if
		save in file NewFilePath as JPEG with options {class:JPEG save options, embed color profile:false, quality:8}
	end tell
end tell

Can anyone provide me the JavaScript code for the same.

This is NOT a forum for JavaScript. However I have been trying to learn some and this should be pretty close to what you want. AppleScript will be slower as photoshop will render the doc window JavaScript does NOT unless you ask it to. The other thing is that in your AppleScript you re-size twice? once to set the resolution then again to fit the image this could be done in one go.

#target photoshop

app.bringToFront();

function main() {
var fileRef = File.openDialog(“Please select a file to ‘Resize’”);
// Record user application preferences
var userRulerUnits = app.preferences.rulerUnits;
var userDisplayDialogs = app.displayDialogs;
// Set application preferences
app.preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;
// Partial path to user home on OSX
var home = Folder(“~”).fsName.toString();
// Check that the output folders exist
var folder1 = home + ‘/Desktop/TESTING/’;
CreateFolder( folder1 );
var folder2 = home + ‘/Desktop/TESTING/untitled folder/’;
CreateFolder( folder2 );
var folder3 = home + ‘/Desktop/TESTING/untitled folder/untitled folder/’;
CreateFolder( folder3 );
open(fileRef);
// Set a reference to opened document
var docRef = app.activeDocument;
// Get file name minus extension
var baseName = docRef.name.slice(0,-4);
// Check and change mode
if (docRef.mode != DocumentMode.RGB) docRef.changeMode(ChangeMode.RGB);
// Call Fit Image Function
FitImage( 600, 72 );
// Make path to save file
var newFilePath = new File(folder3 + baseName + ‘.jpg’);
// Call Save as JPEG Function
SaveFileasJPEG( newFilePath, 8 );
// Close doc
docRef.close(SaveOptions.DONOTSAVECHANGES);
// Re-Set user application preferences
app.preferences.rulerUnits = userRulerUnits;
app.displayDialogs = userDisplayDialogs;

// Create or use existing folder
function CreateFolder(Directory) {
	var folderPath = new Folder(Directory);
	if (folderPath.exists == false) {
	folderPath.create();
	}
} // End function

// Resize Image to fit newSize with newRes
function FitImage(newSize, newRes) {
	if (docRef.width >= docRef.height) {
	docRef.resizeImage(newSize, undefined, newRes, ResampleMethod.BICUBICSMOOTHER);
	}
	else {
	docRef.resizeImage(undefined, newSize, newRes, ResampleMethod.BICUBICSMOOTHER);
	}
} // End function

// Save file as JEPG function
function SaveFileasJPEG(saveFile, qL) {
	jpgSaveOptions = new JPEGSaveOptions();
	jpgSaveOptions.embedColorProfile = false;
	jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
	jpgSaveOptions.matte = MatteType.NONE;
	jpgSaveOptions.quality = qL;
	docRef.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
} // End function

}; // End main function

main();

I’m just curious. Why not just make a photoshop action or droplet?

Hi Mark,

Thanks for your support.
It is really faster than the applescript.

In my applescript I have changed the image size twice, by using resolution and by changing the image pixel dimension.
I have done so due to the following reasons:1. My requirement is to change the image resolution to 72dpi. So First I have changed the image resolution to 72dpi
2. Secondly I have to check the pixel dimension of the image
If the height or width is more than 600 then i have to reset the image size to 600
Else if the height or width is less than 600 then no change in image size should happen.

Please help me by changing the provided script to meet the above requirement.
One more request is while changing the script please make it usable as a batch process (working for every image files one by one in the selected folder)

Thanks in advance for you efforts and support.

Regards
Mani

You may want to take a look at the script ‘image processor’ that ships with CS2 as it does all of the above and more. It may suit your needs as supplied and save you the trouble of writing your own?

Hi Mark,

I could not found the Image Processor script in the provided CS2 suit.
Can you please supply me.

Regards
Mani

In CS4

File>Scripts>Image processor

Regards