I have a script that is attempting to:
- scan an InDesign document for all images
- send all images to Photoshop via the
BridgeTalkobject- resize all of the images to 600px width (maintaining aspect-ratio mathematically)
- export all of the images from Photoshop to a new folder
It seems as if I might need to adjust the DPI of each image programmatically, because Photoshop is crashing before even one image is resized. The error suggests that the temporary memory is overloaded by this script, and I assume it has something to do with the image quality and/or size… Here is the error message:
General Photoshop error occurred. This functionality may not be
available in this version of Photoshop.
Error in Line 1:
Could not complete the command because the scratch disks are full.
Here is the relevant code that transforms image size:
function resaveInPS(imagePaths, imagesFolder)
{
/*
* NOTE: no single-line comments are allowed in this function, because it is not read line-by-line by BridgeTalk, but as a single String;
* only multi-line comments will work, because they are terminated at each end
*/
BridgeTalk.bringToFront("photoshop"); /* switch view from InDesign to Photoshop */
app.displayDialogs = DialogModes.NO; /* Photoshop statement, prevents status alerts from interrupting */
var imagePath = "";
var fileName = "";
var largerImage = "";
for(var i = 0; i < imagePaths.length; i++)
{
imagePath = imagePaths[i].fullName;
fileName = imagePaths[i].name;
largerImage = fileName.substr(0, fileName.length - 4); /* getting rid of the file extension: Photoshop will handle the file extension */
var photoshopDoc = "";
photoshopDoc = app.open(new File(imagePath) );
var currentWidth = photoshopDoc.width; /* in inches */
var currentHeight = photoshopDoc.height; /* in inches */
currentWidth.convert("px"); /* now in pixels */
currentHeight.convert("px"); /* now in pixels */
var newWidth = 600; /* defining the desired exported image width here */
var ratio = newWidth / currentWidth;
var newHeight = ratio * currentHeight; /* maintaining aspect ratio of the resized image's height here */
alert("The currentHeight is " + currentHeight + ".\n\nThe ratio is " + ratio + ".\n\nThe newHeight is " + newHeight + ".");
photoshopDoc.resizeImage(newHeight, newWidth); /* (height, width) */
photoshopDoc.resizeCanvas(newHeight, newWidth); /* (height, width) */
var saveOptions = new TiffSaveOptions(); /* handling the file extension here */
photoshopDoc.saveAs(new File(imagesFolder + "/" + largerImage), saveOptions); /* saving the new image in the folder here, with the file extension */
photoshopDoc.close(SaveOptions.DONOTSAVECHANGES); /* close the Photoshop document without saving */
app.purge(PurgeTarget.ALLCACHES); /* clears the clipboard, history, and undo cache in Photoshop; Note: does NOT delete the temporary files! */
} /* end of for loop */
app.displayDialogs = DialogModes.ALL; /* resume normal dialogs after saving the file and closing the document */
app.purge(PurgeTarget.ALLCACHES); /* clears the clipboard, history and undo cache in Photoshop; Note: does NOT delete the temporary files! */
} // end of function ResaveInPS
NOTE — My usage of the statement app.purge(PurgeTarget.ALLCACHES) does not seem to have much of an effect, as the error is still occurring…
So, I was incorrectly assuming that type-coercion would apply to unit-values, but of course it wouldn’t because “pixel” is not a default Javascript type.
Without applying
new UnitValuetovar ratio, ratio is coerced to typeNumber(thoughcurrentWidthis instantiated with a “pixel” unit-type). Also,var newWidthremains a typeNumberas it was.Here is the corrected code, starting with the line
var currentWidthand ending withresizeCanvas:Also, I got the parameters of the methods
resizeImageandresizeCanvasbackwards! This is the correct order for both:(width, height).— thanks to @Mark for pointing me in the right direction.