// ====================================== // Photoshop CS javascript Utility Script // Rags Gardner www.rags-int-inc.com // javascript main() starts here // ====================================== // ===================================================== // This utility script is intended for PS batch resizing // Specify the taget dimensions in landscape orientation // via global variables in the calling script // newDocWidth // The long side (landscape) // newDocHeight // The short side (landscape) // newDocUnits // INCHES or PIXELS // newDocAdjust // crop, expand, or none // bgFillColor // canvas fill color // myActionSet // Action Set // myPostAction // Post-processing Action or null // ===================================================== var scriptVersion = "V2.2"; // the script version // ====================================== // coreReSizeMain() // ====================================== function coreReSizeMain() { var coreDocRef; var tmpDocWidth; var smallDocScale; var largeDocScale; var newDocScaleH; var newDocScaleW; var oldDocWidth; var oldDocHeight; var docPixResolution; var bgFill = new SolidColor(); var reSampleMethod = "smooth"; //--------------------------------- // Initialize Values & Orientation //--------------------------------- app.preferences.rulerUnits = Units.PIXELS; app.DisplayDialogs = DialogModes.NO; coreDocRef = app.activeDocument; oldDocWidth = coreDocRef.width.value; oldDocHeight = coreDocRef.height.value; //---------------------------- // is it portrait orientation? //---------------------------- if (oldDocHeight > oldDocWidth) { tmpDocWidth = newDocWidth; newDocWidth = newDocHeight; newDocHeight = tmpDocWidth; } docPixResolution = coreDocRef.resolution; if (newDocUnits == Units.INCHES && (docPixResolution == 72 || docPixResolution == 96)) docPixResolution = 300; // JPG, adjust to print resolution //------------------------------------- // CS2 Requires Resize based on Pixels //------------------------------------- if (newDocUnits == Units.INCHES) { newDocWidth *= docPixResolution; newDocHeight *= docPixResolution; } //-------------------------- // determine the new scaling //-------------------------- newDocScaleW = newDocWidth / oldDocWidth; newDocScaleH = newDocHeight / oldDocHeight; if (newDocScaleH < 1 || newDocScaleW < 1) reSampleMethod = "sharp"; // size reduction //--------------------- // scaling up or down ? //--------------------- if (newDocScaleH > newDocScaleW) { largeDocScale = newDocScaleH; smallDocScale = newDocScaleW; } else { largeDocScale = newDocScaleW; smallDocScale = newDocScaleH; } //------------------------------ // does it need to be resmapled? //------------------------------ if (smallDocScale != 1 || largeDocScale != 1) { if (newDocAdjust == "crop") resizeImage(true, reSampleMethod, oldDocWidth*largeDocScale, oldDocHeight*largeDocScale, docPixResolution); else // expand or none resizeImage(true, reSampleMethod, oldDocWidth*smallDocScale, oldDocHeight*smallDocScale, docPixResolution); } //------------------ // set canvas size ? //------------------ if (newDocAdjust != "none") { // crop or expand bgFill.rgb.red = bgFillColor[0]; bgFill.rgb.green = bgFillColor[1]; bgFill.rgb.blue = bgFillColor[2]; app.backgroundColor = bgFill; // the canvas fill color coreDocRef.resizeCanvas(newDocWidth, newDocHeight, AnchorPosition.MIDDLECENTER); } // =================================== // Was a post action requested? // =================================== if (myPostAction != "" && myPostAction != undefined) { if (myPostAction == "thumbNail") saveJpgThumbnail(coreDocRef); else doAction(myPostAction, myActionSet); } //-------------------------- // Restore document settings //-------------------------- coreCleanup(); return; } // ====================================== // function resizeImage(){ // ====================================== function resizeImage(scaleStyles, rsMethod, width, height, resolution) { var aDesc = new ActionDescriptor(); var interpolate; var constrainProportions; if (scaleStyles) constrainProportions = true; else constrainProportions = false; switch(rsMethod) { case "sharp": interpolate = stringIDToTypeID("bicubicSharper"); break; case "smooth": interpolate = stringIDToTypeID("bicubicSmoother"); break; default: interpolate = stringIDToTypeID("Bcbc"); break; } aDesc.putUnitDouble(charIDToTypeID("Wdth"), charIDToTypeID("#Pxl"), width); aDesc.putUnitDouble(charIDToTypeID("Hght"), charIDToTypeID("#Pxl"), height); aDesc.putBoolean(stringIDToTypeID("scaleStyles"), scaleStyles); aDesc.putBoolean(charIDToTypeID("CnsP"), constrainProportions); aDesc.putUnitDouble(charIDToTypeID("Rslt"), charIDToTypeID("#Rsl"), resolution); aDesc.putEnumerated(charIDToTypeID("Intr"), charIDToTypeID("Intp"), interpolate); executeAction(charIDToTypeID("ImgS"), aDesc, DialogModes.NO); return; } // ====================================== // function validateDefaults(){ // ====================================== function validateDefaults() { var retVal = true; if (newDocWidth == undefined || newDocHeight == undefined || newDocUnits == undefined || newDocAdjust == undefined) retVal = false; // missing global variable definition if (!(newDocAdjust == "crop" || newDocAdjust == "expand" || newDocAdjust == "none")) retVal = false; // invalid adjustment option if (newDocWidth < newDocHeight) retVal = false; // portrait dimensions (landscape needed) if (!(newDocUnits == Units.INCHES || newDocUnits == Units.PIXELS)) retVal = false; // invalid pixels or inches if (newDocUnits == Units.PIXELS && (newDocWidth < 16 || newDocHeight < 16)) retVal = false; // too small if (newDocUnits == Units.INCHES && (newDocWidth < 2 || newDocHeight < 2)) retVal = false; // too small return retVal; } // ====================================== // function saveJpgThumbnail() // ====================================== function saveJpgThumbnail(docRef) { var newFileName, tgtFileName, tgtFilePath; var extPos, pathEnd, newFile; var fullFileName = new String(docRef.fullName); var jpgSaveOptions = new JPEGSaveOptions(); jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; jpgSaveOptions.embedColorProfile = false; jpgSaveOptions.matt = MatteType.NONE; jpgSaveOptions.quality = 4; extPos = fullFileName.indexOf("."); // find extension pathEnd = fullFileName.lastIndexOf("/"); // find path info tgtFileName = fullFileName.substring(pathEnd+1, extPos); tgtFilePath = fullFileName.substring(0, pathEnd+1); newFileName = "sm_" + tgtFileName + ".jpg"; newFile = new File(tgtFilePath + newFileName); docRef.flatten(); // discard all layers docRef.bitsPerChannel = BitsPerChannelType.EIGHT // 8-bit mode docRef.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false, Dither.NONE); docRef.saveAs(newFile, jpgSaveOptions, true, Extension.LOWERCASE); docRef.close(SaveOptions.DONOTSAVECHANGES); return; } // ----------------------------------------- // coreCleanup() // ----------------------------------------- function coreCleanup() { app.preferences.rulerUnits = initCoreRulerUnits; app.backgroundColor = initCoreBgColor; app.DisplayDialogs = initCoreDisplayDialogs; return; } // =================================== // Global Application Variables // =================================== var scriptName = "ReSizeCoreScript.jsxinc"; // core script name var callerName = "unknown"; // calling script name var initCoreRulerUnits = app.preferences.rulerUnits; var initCoreDisplayDialogs = app.DisplayDialogs; var initCoreBgColor = app.backgroundColor; var coreExcption; if (bgFillColor == undefined) var bgFillColor = [0,0,0]; // canvas fill color (RGB) // =================================== // JavaScript Main // =================================== scriptName = scriptName + " " + scriptVersion; if (myScriptName != undefined) callerName = myScriptName; else callerName = scriptName; if (documents.length == 0) { alert(callerName + " There is no active document"); } else if (!validateDefaults()) { alert(callerName + " Script defaults are invalid: target size(" + newDocWidth + "," + newDocHeight + "," + newDocUnits + ") Aspect Adjust=" + newDocAdjust); } else { try { coreReSizeMain(); } catch(coreExcption) { if (coreExcption.line != undefined) alert("ReSizeCoreScript line[" + coreExcption.line + "] " + coreExcption); else alert("ReSizeCoreScript " + coreExcption); coreCleanup(); // reset rulers... } } // END JavaScript Main