Modifying a Script for use with AH Bridge Launch2 - Tutorial

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Kukurykus, Tom, xbytor

Andrew

Modifying a Script for use with AH Bridge Launch2 - Tutorial

Post by Andrew »

In the AH Bridge Launch 2 thread I provide a zip file ah-fitimage.zip containing three small test scripts:

ah-fitimage.js - run from AH Bridge Launch 2 in Normal / No Batch mode

ah-fitimage-batch.js - run from AH Bridge Launch 2 in Batch LO mode

ah-fitimage-files.js - run from AH Bridge Launch 2 in Files mode

The script resizes target images (test it with a couple of test jpgs selected in Bridge) according to your instructions and provides a 'fit image' functionality which includes a choice of resize method. There is nothing clever about it, it is only for demonstration purposes.

All three files are very similar in length. The first will only resize the Active Document. The second and third are adapted to provide all the options of batching scripts using Bridge Launch 2. You can use these samples to adapt your own scripts to take advantage of Bridge Launch 2's batching UI.

Here is the first script:

Code: Select alltry {fResizeToBounds();}
catch (e) {alert(e);}

function fResizeToBounds() {
   var oUnits,wh,widthBound,heightBound,rm,nb,nbsm,nbsh,x,y,res;
   oUnits = app.preferences.rulerUnits;
   app.preferences.rulerUnits = Units.PIXELS;
   wh = prompt('Enter Pixel bounds to Fit Resized Image Within\nwidth,height').split(',');
   widthBound = Number(wh[0]);
   heightBound = Number(wh[1]);
   if (isNaN(widthBound) || isNaN(heightBound)) {
      app.preferences.rulerUnits = oUnits;
      alert('Invalid Resize Parameters');
      return;
   }         
   rm = prompt('Enter Resize Method\nb = bicubic\nbsm = bicubic smoother\nbsh = bicubic sharper');
   if (rm == 'b') rm = ResampleMethod.BICUBIC;
   else if (rm == 'bsm') rm = ResampleMethod.BICUBICSMOOTHER;
   else if (rm == 'bsh') rm = ResampleMethod.BICUBICSHARPER;
   else {
      app.preferences.rulerUnits = oUnits;
      alert('Invalid Resize Method');
      return;
   }
   x = activeDocument.width;
   y = activeDocument.height;
   res = activeDocument.resolution;
   if (x/widthBound > y/heightBound) {
      alert('New Width ' + (widthBound) + '\nNew Height ' + (y*widthBound/x));
      activeDocument.resizeImage(Number(widthBound),y*widthBound/x,res,rm)
   }
   else {
      alert('New Width ' + (x*heightBound/y) + '\nNew Height ' + (heightBound));
      activeDocument.resizeImage(x*heightBound/y,Number(heightBound),res,rm);
   }
   alert('Finished');
   app.preferences.rulerUnits = oUnits;
}

The second script uses Bridge Launch 2's built in variables to make for an efficient batching tool. Use it in Batch Leave Open mode so as to preserve your files in their original sizes. It asks you for your target resize parameters only on the first iteration of the batch loop. It remembers those parameters and applies them on all the intermediate loops, and it issues a 'Finished' message after resizing the final image.

If you look at the code you will see:

1. All variables that need to be remembered from onbe iteration to the next are preceded by the Object ah_DataObj eg ah_DataObj.widthBound.

2. The first iteration is recognised by the test if (ah_i == 0) using the ah_i variable.

3. Completion of the final loop is recognised with if (ah_DataObj.lastTime == true).

4. I did not need to wrap the outer function in a try / catch sequence because this is already covered within Bridge Launch 2.

5. While the Object ah_DataObj, and the variable ah_i are not in fact global in scope, they are available to your script as if they were ie they do not need to be passed between functions.

It would not take you long to convert your first lot of code to this and yet now you have a considerably more powerful script:

Code: Select allfResizeToBounds();

function fResizeToBounds() {
   var x,y,res;
   if (ah_i == 0) {
      ah_DataObj.oUnits = app.preferences.rulerUnits;
      app.preferences.rulerUnits = Units.PIXELS;
      ah_DataObj.wh = prompt('Enter bounds to Fit Resized Image Within\nwidth,height').split(',');
      ah_DataObj.widthBound = Number(ah_DataObj.wh[0]);
      ah_DataObj.heightBound = Number(ah_DataObj.wh[1]);
      if (isNaN(ah_DataObj.widthBound) || isNaN(ah_DataObj.heightBound)) {
         app.preferences.rulerUnits = oUnits;
         alert('Invalid Resize Parameters');
         return;
      }         
      ah_DataObj.rm = prompt('Enter Resize Method\nb = bicubic\nbsm = bicubic smoother\nbsh = bicubic sharper');
      if (ah_DataObj.rm == 'b') ah_DataObj.rm = ResampleMethod.BICUBIC;
      else if (ah_DataObj.rm == 'bsm') ah_DataObj.rm = ResampleMethod.BICUBICSMOOTHER;
      else if (ah_DataObj.rm == 'bsh') ah_DataObj.rm = ResampleMethod.BICUBICSHARPER;
      else {
         app.preferences.rulerUnits = ah_DataObj.oUnits;
         alert('Invalid Resize Method');
         return -1;
      }
   }
   x = activeDocument.width;
   y = activeDocument.height;
   res = activeDocument.resolution;
   if (x/ah_DataObj.widthBound > y/ah_DataObj.heightBound) {
      alert('New Width ' + (ah_DataObj.widthBound) + '\nNew Height ' + (y*ah_DataObj.widthBound/x));
      activeDocument.resizeImage(ah_DataObj.widthBound,y*ah_DataObj.widthBound/x,res,ah_DataObj.rm)
   }
   else {
      alert('New Width ' + (x*ah_DataObj.heightBound/y) + '\nNew Height ' + (ah_DataObj.heightBound));
      activeDocument.resizeImage(x*ah_DataObj.heightBound/y,ah_DataObj.heightBound,res,ah_DataObj.rm);
   }
   if (ah_DataObj.lastTime == true) {alert('Finished');app.preferences.rulerUnits = ah_DataObj.oUnits; }
}

The third script is designed to be run in Files mode. In this case the original script must be modified to do it's own batching based on the array of string file paths batchFiles. It does not have access to the scriptObj Object nor the ah_i variable. It remains an easy adaptation which is particularly well suited to the task of sending a list of files to an application for which the files themselves are not to be actually opened, but are to be used within the script. An example of this would be sending a list of image files to a GoLive script in order to build them into a webpage

(OK I better admit I am ad-libbing here because I have not worked with GoLive, but the concept should hold).

Code: Select allfunction fResizeToBounds() {
   var oUnits,wh,widthBound,heightBound,rm,nb,nbsm,nbsh,x,y,res;
   oUnits = app.preferences.rulerUnits;
   app.preferences.rulerUnits = Units.PIXELS;
   for (i = 0; i < batchFiles.length; i++) {
      if (i == 0) {
         wh = prompt('Enter Pixel bounds to Fit Resized Image Within\nwidth,height').split(',');
         widthBound = Number(wh[0]);
         heightBound = Number(wh[1]);
         if (isNaN(widthBound) || isNaN(heightBound)) {
            app.preferences.rulerUnits = oUnits;
            alert('Invalid Resize Parameters');
            return;
         }         
         rm = prompt('Enter Resize Method\nb = bicubic\nbsm = bicubic smoother\nbsh = bicubic sharper');
         if (rm == 'b') rm = ResampleMethod.BICUBIC;
         else if (rm == 'bsm') rm = ResampleMethod.BICUBICSMOOTHER;
         else if (rm == 'bsh') rm = ResampleMethod.BICUBICSHARPER;
         else {
            app.preferences.rulerUnits = oUnits;
            alert('Invalid Resize Method');
            return;
         }
      }
      open(File(batchFiles));
      x = activeDocument.width;
      y = activeDocument.height;
      res = activeDocument.resolution;
      if (x/widthBound > y/heightBound) {
         alert('New Width ' + (widthBound) + '\nNew Height ' + (y*widthBound/x));
         activeDocument.resizeImage(widthBound,y*widthBound/x,res,rm)
      }
      else {
         alert('New Width ' + (x*heightBound/y) + '\nNew Height ' + (heightBound));
         activeDocument.resizeImage(x*heightBound/y,heightBound,res,rm);
      }
      if (i == (batchFiles.length - 1)) alert('Finished');
      /********************** If this was not a test you would do the following **********************
      activeDocument.save();
      activeDocument.close(SaveOptions.DONOTSAVECHANGES);
      *********************************************************************************************************/
   }
   app.preferences.rulerUnits = oUnits;
}


If you want any help in understanding these samples, or in adapting your own scripts, ask in this thread here.

Andrew