Sub topic 17
Automating Smart Object Actions with Scripts
This chapter assumes you know Photoshop’s Layers panel, can select layers, and understand how smart objects differ from ordinary pixel layers. It also assumes basic JavaScript syntax. The examples use Photoshop ExtendScript, Photoshop’s older scripting environment. Run scripts from Photoshop’s scripting tools or save them as .jsx files.
Why automate smart object actions?
Manual commands work well for one layer. A script helps when you repeat the same operation across many documents or mockups. Scripts can:
- Convert the active layer into a smart object.
- Open the contents of the selected smart object.
- Combine these actions with naming, saving, or batch processing.
- Reduce errors caused by repeating menu commands.
Photoshop scripts often use the Action Manager. This API exposes commands that do not have simple, direct scripting methods. The script identifies a command with stringIDToTypeID, then runs it with executeAction.
Convert the active layer
Select a layer before running this script. The command matches Layer > Smart Objects > Convert to Smart Object.
// Photoshop ExtendScript: convert the active layer or layers to a smart object
// The active document and at least one selected layer are required.
if (app.documents.length === 0) {
throw new Error("Open a document before running this script.");
}
executeAction(
stringIDToTypeID("newPlacedLayer"),
undefined,
DialogModes.NO
);The script does not choose a layer by name. It acts on the current selection. This makes it useful in a recorded workflow, but it also creates a common failure: running it with the wrong layer selected.
Open smart object contents
After selecting a smart object layer, run the following script. Photoshop opens the embedded contents in another document window. Save that document to update the parent smart object.
// Photoshop ExtendScript: open the contents of the active smart object layer
if (app.documents.length === 0) {
throw new Error("Open a document before running this script.");
}
executeAction(
stringIDToTypeID("placedLayerEditContents"),
undefined,
DialogModes.NO
);This command expects the active layer to be a smart object. It can fail, or produce no useful result, if the active layer is a pixel layer, a group, or a locked item.
A small scripted workflow
A typical mockup workflow has this sequence:
flowchart TD
A[Select source layer] --> B[Convert to smart object]
B --> C[Transform or place in mockup]
C --> D[Open smart object contents]
D --> E[Edit and save contents]
E --> F[Parent document updates]Scripts can also call app.activeDocument, rename layers, and save files. Keep each script focused. A short script is easier to test and safer to rerun than one large script that changes many documents at once.
| Situation | Useful script action | Main check |
|---|---|---|
| Reusable source layer | Convert to smart object | Correct layer is active |
| Editing a mockup asset | Open contents | Active layer is a smart object |
| Batch preparation | Repeat conversion | Document and save paths are controlled |
Exercise
Why might placedLayerEditContents fail when the script itself is valid?
Show answer
The active layer may not be a smart object, or no document may be open.