A Simple IF then ELSE using actions and batch for non-coders

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

Andrew

A Simple IF then ELSE using actions and batch for non-coders

Post by Andrew »

This is something that might be useful for the non-coders. Often you want to run through a list of images, exclude some and do something to the others. The 'do something' could be for example, a resize and save in a new folder. A second, more powerful version of the same is to 'do something else' to the other group of images (those that were excluded).

This can easily be achieved by integrating a very basic script with Photoshop Batch and Actions and is potentially a very flexible way of working with images.

An example of this, recently asked about in the Adobe forum was: how can I resize some images downwards to 1600 x 1600 while excluding any images which were smaller than that box dimension. This example can be applied to any number of alternative situations.

Here is what you do:

1. Create an actionset: eg 'scriptset'

2. Create an action in that set that does your task and call it 'resizedown'.

Record 'resize-down' to do the resize (and anything else you might want). Add as a last step to the action a 'save as' step to define the parameters of how you want to save the image.

3. Copy the following script into a text editor and save as 'imagefilter.js':

Code: Select allapp.displayDialogs = DialogModes.NO;
var strtRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var testset2 = 'scriptset';
var taction2 = 'resizedown';

if (activeDocument.height < 1600 || activeDocument.width < 1600)
  {
  // doAction(taction1,testset1); // you could point at another action here
  // alert('small'); // or issue a message
  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  }
else
  {
  app.doAction(taction2,testset2);
  activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  }
preferences.rulerUnits = strtRulerUnits;

4. Close and reopen Photoshop (registers the script with Photoshop).

5. Record a second action called 'runscript' in that 'scriptset' action set, and make the only step of the action to run the imagefilter script (using 'add menu item').

6. Select some files in the File Browser (or equivalent in CS2).

7. Go to File>Automate>Batch
set = scriptset, action = runscript
source = File Browser
supress File Open and Color Warnings
Destination = Folder
Choose = select a folder - can be the one files are already in or new one
Tick Over-ride Action Save-As Command
RUN

This will run the action on all images that have one edge larger than 1600 but will leave images with both edges smaller than 1600 unchanged.

This example provides a method for using a basic script combined with batch and actions to:

test for a condition (in this case image dimensions)
run action1 if condition true
run action2 if condition false
save images controlled by parameters set in batch.

A couple of additional points:

1. The code for an AND condition looks like this:

Code: Select allif (activeDocument.height < 1600 && activeDocument.width < 1600)

2. The code:
Code: Select all// at the start

app.displayDialogs = DialogModes.NO;
var strtRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;

// and then at the end

preferences.rulerUnits = strtRulerUnits;

is making sure you are using pixels as ruler units in order to run the image size test, and then resetting these back to their original values afterwards.

The above script / action / batch framework will allow non-scripters to do many useful tasks that are otherwise impossible.

Andrew