Sub topic 18
Automating layer creation and grouping with a script
This chapter assumes you can create, select, reorder, and group layers by hand in the Layers panel. It also assumes you know what opacity and blend modes do. You do not need prior programming experience beyond reading short scripts. Photoshop scripts use ExtendScript, which is Adobe's version of JavaScript. Script files use the .jsx extension.
Why script layer work
Manual layer work is easy to do once and tedious to repeat. A script helps when you:
- start every project with the same set of named groups,
- need to rename or reorder dozens of layers consistently,
- hand a template to a team and want the same structure every time.
A script performs the same actions you would perform in the panel. It calls the same operations, in the same order, on the same document. If you understand the panel, you already understand what the script does.
How the scripting model maps to the panel
Photoshop exposes the open document as an object tree. The names below are the ones you will use most.
| Panel concept | Script object or property | Notes |
|---|---|---|
| Open document | `app.activeDocument` | The document in the front window |
| Pixel layer | `doc.artLayers.add()` | Creates a new empty layer |
| Group | `doc.layerSets.add()` | Creates an empty group |
| Selected layer | `doc.activeLayer` | One layer, even if several are selected in the panel |
| Layer name | `layer.name` | Plain string |
| Opacity | `layer.opacity` | Number from 0 to 100 |
| Blend mode | `layer.blendMode` | Value from the `BlendMode` enumeration |
| Top-level items | `doc.layers` | Index 0 is the topmost item in the panel |
Two details matter. First, layerSets is the scripting name for groups. Second, doc.layers lists only top-level items. A layer inside a group appears in that group's own layers collection, not in the document's.
Create a layer and a group
The first script adds a pixel layer and an empty group, names both, and moves the layer into the group.
// ExtendScript (.jsx): create a named layer and a group, then put the layer inside the group
var doc = app.activeDocument;
var layer = doc.artLayers.add();
layer.name = "Highlights";
var group = doc.layerSets.add();
group.name = "Lighting";
layer.move(group, ElementPlacement.INSIDE);To run it, save the code as lighting.jsx. In Photoshop, open a document and choose File > Scripts > Browse. Then pick the file. You can also place it in the Photoshop Presets/Scripts folder and restart Photoshop, after which it appears in the File > Scripts menu.
The move method takes a reference object and a placement. ElementPlacement.INSIDE puts the layer inside the group, at the top of the group's contents. Other placements are described next.
Placement options for `move`
| Placement | Result |
|---|---|
| `ElementPlacement.INSIDE` | Inside the reference group |
| `ElementPlacement.PLACEBEFORE` | Directly above the reference item in the panel |
| `ElementPlacement.PLACEAFTER` | Directly below the reference item in the panel |
| `ElementPlacement.PLACEATBEGINNING` | At the top of the reference container |
| `ElementPlacement.PLACEATEND` | At the bottom of the reference container |
The word "before" refers to the top of the stack, not to the compositing order. In the panel, the top layer is composited last, so "before" in a script means "visually above".
Reorder a layer
Stacking order decides what you see, so scripts need to change it precisely. This script sends the active layer to the top of the stack.
// ExtendScript (.jsx): send the active layer to the top of the stack
var doc = app.activeDocument;
var layer = doc.activeLayer;
layer.move(doc.layers[0], ElementPlacement.PLACEBEFORE);doc.layers[0] is the topmost item in the Layers panel, so placing the active layer before it puts the layer on top. This matches the keyboard shortcut Shift+Ctrl+] on Windows or Shift+Cmd+] on Mac.
If the active layer is inside a group, this script pulls it out of the group and places it at the top level. That is often what you want, but it can surprise you. To send a layer to the top of its own group, reference the first item of layer.parent.layers instead.
Change how a layer composites
Opacity and blend mode are properties, so setting them is one line each.
// ExtendScript (.jsx): change how the active layer composites
var layer = app.activeDocument.activeLayer;
layer.opacity = 60;
layer.blendMode = BlendMode.MULTIPLY;Opacity uses a 0 to 100 scale, the same as the Opacity field in the panel. Blend modes come from the BlendMode enumeration, for example BlendMode.SCREEN or BlendMode.OVERLAY. A group has the same two properties. A new group starts in Pass Through, and the matching value is BlendMode.PASSTHROUGH.
Build a reusable project skeleton
The three examples above are small pieces. A common real use is a template script that builds a naming and grouping convention in one run. The loop below creates one group per category and keeps them in a predictable order.
// ExtendScript (.jsx): build a standard group structure
var doc = app.activeDocument;
var names = ["Retouch", "Color", "Lighting", "Type"];
for (var i = 0; i < names.length; i++) {
var group = doc.layerSets.add();
group.name = names[i];
}Each new group is added above the active layer. Running the loop in this order therefore leaves "Type" nearest the top, depending on where the active layer sits. Check the result in the panel the first time, and adjust the array order if needed.
Common errors
- "undefined is not an object" on
activeDocument. No document is open. Open one first, or checkapp.documents.length > 0before using it. - The layer is not where you expected.
layerSets.add()andartLayers.add()insert relative to the active layer. If the active layer is inside a group, the new item can end up inside that group. - The Background layer will not move. It is locked by design. Convert it first by setting
doc.backgroundLayer.isBackgroundLayer = false, which has the same effect as double-clicking it in the panel. - A locked layer rejects changes. Check
layer.allLocked, and set it tofalsebefore editing. Restore it afterward. - Only one layer is affected.
doc.activeLayerreturns a single layer. To act on several, loop over a collection such asdoc.layers.
Make a script safer
A short guard makes any script friendlier. This version stops with a clear message when no document is open and groups the whole operation as one undo step.
// ExtendScript (.jsx): guard against a missing document and undo in one step
function main() {
var doc = app.activeDocument;
var group = doc.layerSets.add();
group.name = "Review";
}
if (app.documents.length === 0) {
alert("Open a document before running this script.");
} else {
app.activeDocument.suspendHistory("Add Review group", "main()");
}The suspendHistory method takes a history-state name and a string of code to run. Photoshop records everything the code does as one entry in the History panel.
Decision criteria: script or hand
| Situation | Better choice |
|---|---|
| One-off edit on one file | By hand |
| Same structure on many files | Script |
| Judgment-heavy work such as choosing blend modes by eye | By hand |
| Renaming many layers by a rule | Script |
Exercise
A script runs layer.move(doc.layers[0], ElementPlacement.PLACEBEFORE); while the active layer sits inside a group named "Lighting". Where does the layer end up, and what would you change to send it to the top of "Lighting" instead?
Show answer
The layer leaves the group and becomes the topmost top-level item in the document, because doc.layers[0] is the first top-level item. To keep it in its group, use the group's own collection as the reference: layer.move(layer.parent.layers[0], ElementPlacement.PLACEBEFORE);.
Exercise
Write the two lines that set the active layer to 40% opacity and the Screen blend mode.
Show answer
layer.opacity = 40; and layer.blendMode = BlendMode.SCREEN; (with var layer = app.activeDocument.activeLayer; defined first). Opacity uses a 0 to 100 scale.