Sub topic 15
Going Further: Calling Actions from ExtendScript
Assume that you can open, resize, and save files in Photoshop, and that you know basic JavaScript syntax. ExtendScript is Adobe’s JavaScript-based scripting language. A Photoshop script can control documents, folders, layers, and many application commands.
An action is useful when Photoshop already knows the steps you need. A script is useful when you need decisions, loops, file handling, or values that change for each image. You can combine both: let the script choose the files, then let an action perform the repeated editing.
Running an existing action
Photoshop stores actions inside action sets. An action set is a named group that appears in the Actions panel. The app.doAction() method runs an action by its name and set name.
#target photoshop
// Run one existing action on the active document.
app.doAction("Resize for Web", "My Actions");The names must match the Actions panel exactly, including capitalization and spaces. The action must already be loaded in Photoshop. If Photoshop cannot find either name, the script stops with an error.
Save this text with a .jsx extension. In Photoshop, choose File > Scripts > Browse, select the file, and run it. You can also place frequently used scripts in Photoshop’s Scripts folder.
Combining an action with folder processing
A script can select a folder, open each supported image, apply your action, and save a result. The following example also resizes images before saving them. Replace the action names and file settings for your workflow.
#target photoshop
app.displayDialogs = DialogModes.NO;
var inputFolder = Folder.selectDialog("Choose the folder of images");
if (inputFolder) {
var outputFolder = new Folder(inputFolder.fsName + "/web");
if (!outputFolder.exists) {
outputFolder.create();
}
var files = inputFolder.getFiles(function (f) {
return f instanceof File && /\.(jpe?g|png|tiff?)$/i.test(f.name);
});
var maxWidth = 1600;
for (var i = 0; i < files.length; i++) {
var doc = app.open(files[i]);
var w = doc.width.as("px");
var h = doc.height.as("px");
if (w > maxWidth) {
var newHeight = Math.round(h * maxWidth / w);
doc.resizeImage(
UnitValue(maxWidth, "px"),
UnitValue(newHeight, "px"),
doc.resolution,
ResampleMethod.BICUBICSHARPER
);
}
app.doAction("Add Web Sharpening", "My Actions");
var baseName = doc.name.replace(/\.[^\.]+$/, "");
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
doc.saveAs(
new File(outputFolder.fsName + "/" + baseName + ".jpg"),
jpegOptions,
true,
Extension.LOWERCASE
);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}The script keeps the original files unchanged. It creates a web subfolder, preserves proportions while resizing, runs the action, and saves JPEG copies. app.displayDialogs = DialogModes.NO prevents many dialogs from interrupting a batch, but it also means Photoshop may choose defaults instead of asking questions.
Common failure points
- The action or set name does not match the Actions panel.
- The action expects a layer name that some files do not contain.
- A modal action step opens a dialog during the loop.
- The script saves over the source because the output path is not separate.
- A file format does not support the selected save options.
Test the script on copies of a few files first. Keep the action short and predictable, and make the script responsible for file selection and naming.
Exercise
What does app.doAction("Resize for Web", "My Actions") require, and why is the action set name necessary?
Show answer
It requires an action named “Resize for Web” inside the action set named “My Actions”; the set name tells Photoshop which group contains the action.