Sub topic 15
Automating replacement and export with a script
This chapter assumes that you know Photoshop layers, smart objects, file paths, and basic JavaScript syntax. ExtendScript is Adobe’s JavaScript-based scripting language for older Photoshop automation workflows. A script can select a layer, replace the smart object contents, and export a finished mockup repeatedly.
What the script automates
A reusable mockup normally has three parts:
- A smart object layer named
Artwork. - Transforms, masks, shadows, and highlights around that layer.
- An output file for each design variation.
Replacing the smart object contents changes the artwork while keeping the layer’s transform and surrounding effects.
The script below processes every PNG or JPEG file in an input folder. It expects the smart object to be a top-level layer named Artwork.
A batch replacement script
Create a .jsx file and run it with File > Scripts > Browse in Photoshop. Open the mockup template before running the script, or change the script to open a template file first.
#target photoshop
var doc = app.activeDocument;
var inputFolder = new Folder("~/mockups/art");
var outputFolder = new Folder("~/mockups/out");
if (!inputFolder.exists) {
throw new Error("Input folder does not exist: " + inputFolder.fsName);
}
if (!outputFolder.exists) {
outputFolder.create();
}
var artworkFiles = inputFolder.getFiles(/\.(png|jpe?g)$/i);
var artworkLayer = doc.layers.getByName("Artwork");
var previousDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
try {
for (var i = 0; i < artworkFiles.length; i++) {
var artworkFile = artworkFiles[i];
if (!(artworkFile instanceof File)) {
continue;
}
doc.activeLayer = artworkLayer;
var desc = new ActionDescriptor();
desc.putPath(charIDToTypeID("null"), artworkFile);
desc.putInteger(charIDToTypeID("PgNm"), 1);
executeAction(
stringIDToTypeID("placedLayerReplaceContents"),
desc,
DialogModes.NO
);
var baseName = artworkFile.name.replace(/\.[^\.]+$/, "");
var outputFile = new File(
outputFolder.fsName + "/" + baseName + ".jpg"
);
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
doc.saveAs(
outputFile,
jpegOptions,
true,
Extension.LOWERCASE
);
}
} finally {
app.displayDialogs = previousDialogs;
}saveAs receives true for asCopy, so Photoshop saves a JPEG copy without converting the open template into a JPEG document. The script uses getByName on doc.layers, which searches only the document’s top-level layers. If Artwork is inside a group, access that group first and then search its layers collection.
Script requirements and common errors
| Requirement | Why it matters |
|---|---|
| Layer name is exactly `Artwork` | The script uses the name to find the smart object |
| Artwork files are PNG or JPEG | The folder filter ignores other file types |
| Template is open and unsaved changes are acceptable | The script works on the active document |
| Output folder is writable | Photoshop must create each JPEG copy |
A missing layer name causes an error before any files are processed. Rename the placeholder or change "Artwork" in the script. If the replacement action fails, verify that the selected layer is a smart object rather than a rasterized layer.
Exercise
Exercise
Why does the script save each JPEG with asCopy set to true?
Show answer
It preserves the open mockup template as the reusable master document while exporting a separate JPEG copy for each design.