Sub topic 13
Branching with Conditional Actions
This chapter assumes you can record and play actions, and that you know how to insert menu items and stops. It also assumes you have seen why portable actions matter when image sizes and orientations vary. Here you will learn how Photoshop's Conditional command lets one action make decisions, and where its limits are.
What a conditional action does
A plain action runs every step in order. A conditional action checks a property of the active document and then plays one of two actions. This lets one batch run handle a mixed folder. For example, landscape photos can be resized by width, and portrait photos by height.
The command is Insert Conditional, found in the Actions panel flyout menu. It inserts a step into the action you are recording. When the step plays, Photoshop evaluates the condition. If the condition is true, it plays the action you chose for "Then Play". If not, it plays the action you chose for "Else Play".
An important detail: the step does not contain commands. It points to other actions by name. Both target actions must already exist, and they must be loaded in the Actions panel when the batch runs.
The available conditions
The Conditional dialog offers a fixed list of tests. You cannot write your own expression. The tests cover these properties:
| Condition | Typical use |
|---|---|
| Document is landscape, portrait or square (orientation) | Resize by the long side |
| Document is RGB, CMYK, Grayscale, Lab and similar color modes | Convert only files that are not already in the target mode |
| Document has a selection or has no selection | Skip steps that need a selection |
| Document has one or more layers, or has a background layer | Avoid errors on flattened files |
| Document has a path, an alpha channel or a layer comp | Run extra steps only when the item exists |
| Document is a certain bit depth | Convert 16-bit files to 8-bit before saving as JPEG |
| Document is a certain size, wider or taller than a set value | Shrink only large images |
Check the dialog in your version for the exact list. The list is the main limit of conditional actions. You can test one property at a time, and you cannot combine tests with AND or OR.
Building a conditional action step by step
The standard pattern uses three actions: one for each branch, and one controller that holds the conditional step. Suppose you want to fit images into a 1600-pixel box regardless of orientation.
- Create an action set called
Conditional Demo. - Record an action named
Fit Landscape. Use Image > Image Size, turn on Constrain Proportions, set the width to 1600 pixels, and choose a resample method. Stop recording. - Record an action named
Fit Portraitin the same way, but set the height to 1600 pixels. - Create an action named
Fit Any Orientation. Start recording it. - Open the Actions panel flyout menu and choose Insert Conditional.
- In the dialog, set If Current to the orientation test for landscape. Set Then Play Action to
Fit Landscape, and Else Play Action toFit Portrait. Click OK. - Stop recording.
Open any image and play Fit Any Orientation. The controller checks orientation and plays one of the two actions. Because the branch is chosen at playback time, you can then use Fit Any Orientation in the Batch command on a folder of mixed images.
Square images and the Else branch
The Else branch catches everything that fails the test. In the example above, a square image is not landscape, so it goes to Fit Portrait. That sets the height to 1600 pixels, which for a square image gives the same result as setting the width. In other cases the Else branch may be wrong for edge cases, so think about what falls into it.
If you need three outcomes (landscape, portrait and square), you can chain conditionals. Put a second conditional inside the Else action. The first conditional tests for landscape. Its Else action is a second controller that tests for portrait and plays either the portrait action or a square action. Each level adds one more action to maintain, so keep chains short.
Doing nothing in one branch
You may want a step that only runs sometimes. For example, convert to 8-bit only when the file is 16-bit. The Else branch needs an action to play, and an empty action works. Record an action named Do Nothing with no steps, or with a single harmless step such as selecting the top layer. Photoshop will not let you leave the field blank, so keep a shared Do Nothing action in your set and reuse it.
A related pattern is a safety check. Test whether the document has a background layer. If it does, play an action that converts it to a regular layer. If it does not, play Do Nothing. This avoids the failure you get when an action tries to unlock a layer that is not there.
Conditional actions in a batch
Conditionals work inside File > Automate > Batch and in droplets, with these requirements:
- The controller action is the one you choose in the Batch dialog. The branch actions are not chosen there, but they must be in a loaded set.
- A droplet stores a reference to the action. If you share a droplet with a colleague, they need the full set loaded, including branch actions, on the machine that runs it. Save the set as an
.atnfile and share it with the droplet. - If you rename a branch action after building the conditional step, the reference can break. Rename carefully, then open the conditional step and check that it still points to the right action.
Dialog settings matter here too. If a branch action contains a step recorded with a dialog toggle on, the batch will stop at that dialog unless you choose Suppress File Open Options Dialogs and Suppress Color Profile Warnings, or turn off the toggle. Check each branch action separately as you would any other action.
Testing and debugging conditionals
A conditional action can fail silently when a branch does something you did not expect. Test in this order:
- Run each branch action by itself on a suitable image. If a branch fails alone, the conditional is not the cause.
- Run the controller on one image of each type: landscape, portrait, square and any edge case such as a very small image.
- Watch the History panel. The steps in the branch appear as they run, so you can see which branch played.
- Run the batch on a small test folder, with Destination set to a new folder, before the full set.
Common errors:
- Wrong branch plays. Check the condition wording. "Is landscape" and "is portrait" are separate tests, and square images count as neither in some versions. Test a square file.
- Action not found. The branch action is not loaded, or it was renamed or deleted. Load the set and reselect the branch in the conditional step.
- Batch stops on a dialog. A branch action has a dialog toggle turned on, or a file open option dialog appears. Turn off toggles or use the suppress options.
- Branch changes the wrong thing. Branch actions inherit the document state left by earlier steps. If an earlier step changed the active layer or selection, the branch may act on the wrong one. Select the target layer at the start of each branch.
When a conditional is not enough
Conditionals test only the properties in the dialog. They cannot read file names, metadata, or the number of pixels in a region. They also cannot loop or count. If your decision depends on something outside the list, you have three options:
- Sort the files into separate folders beforehand and run one batch per folder.
- Use the Image Processor when the task is only resizing and format conversion.
- Write an ExtendScript file that checks the property and calls
app.doActionfor the right action. The scripting chapter covers this.
The choice depends on how often the decision changes. A conditional action keeps everything inside Photoshop and is easy for others to inspect. A script is more flexible but needs JavaScript knowledge and a separate file to maintain.
Exercise
You built a controller action with a conditional step that plays Shrink Large if the document is wider than 2000 pixels, and plays Do Nothing otherwise. A colleague runs your droplet on their computer and every image fails with an error about a missing action. What is the most likely cause, and how do you fix it?
Show answer
The branch actions (Shrink Large and Do Nothing) are not loaded on the colleague's machine. The conditional step stores references to actions by name, and a droplet does not carry the other actions in the set. Save the whole action set as an .atn file, send it with the droplet, and have the colleague load it in the Actions panel before running the droplet.
Exercise
You need to sort images three ways: landscape images are fit by width, portrait images by height, and square images are fit to 1200 by 1200 pixels. How do you build this with conditionals?
Show answer
Create three branch actions: Fit Landscape, Fit Portrait and Fit Square. Create a second controller, Portrait or Square, whose conditional tests for portrait, playing Fit Portrait if true and Fit Square otherwise. In the main controller, test for landscape, playing Fit Landscape if true and Portrait or Square otherwise. Test with one image of each shape.