Sub topic 14
Automating Open and Save with ExtendScript
Prerequisites
This chapter assumes that you know the Photoshop workspace, understand pixels and image dimensions, and have a basic understanding of layers. You also need a desktop version of Photoshop that supports ExtendScript.
ExtendScript is Adobe’s JavaScript-based scripting language. A Photoshop script can open files, inspect documents, and save repeatable outputs without repeating menu commands manually. Save the script with a .jsx extension, then run it with File > Scripts > Browse.
The Photoshop scripting model
Photoshop exposes its application through objects. The app object represents Photoshop, a Document object represents an open document, and objects such as JPEGSaveOptions hold format settings.
| Script object | Purpose |
|---|---|
| `app` | Accesses Photoshop and its documents |
| `app.open(file)` | Opens a file as a document |
| `app.activeDocument` | Gets the currently active document |
| `doc.saveAs(...)` | Writes a document to a chosen format |
| `File.openDialog(...)` | Displays a file-selection dialog |
The script can save a copy while leaving the working document open. This is useful when you want to create delivery files without changing the document’s current name or format.
// ExtendScript for Photoshop
// Opens an image and creates PSD, JPEG, and PNG copies on the desktop.
var source = File.openDialog("Choose an image to open");
if (source) {
var doc = app.open(source);
var desktop = Folder.desktop;
var baseName = doc.name.replace(/\.[^\.]+$/, "");
var psdOptions = new PhotoshopSaveOptions();
psdOptions.layers = true;
psdOptions.embedColorProfile = true;
psdOptions.maximizeCompatibility = true;
doc.saveAs(
new File(desktop + "/" + baseName + ".psd"),
psdOptions,
true,
Extension.LOWERCASE
);
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.formatOptions = FormatOptions.STANDARDBASELINE;
doc.saveAs(
new File(desktop + "/" + baseName + ".jpg"),
jpegOptions,
true,
Extension.LOWERCASE
);
var pngOptions = new PNGSaveOptions();
pngOptions.interlaced = false;
doc.saveAs(
new File(desktop + "/" + baseName + ".png"),
pngOptions,
true,
Extension.LOWERCASE
);
alert("Saved PSD, JPEG, and PNG copies to the desktop.");
}The third argument to saveAs is true, which means “save as a copy.” The open document remains the working document instead of becoming the newly saved JPEG or PNG. The PSD options preserve layers and embed the color profile. JPEG uses lossy compression and does not preserve the layer structure. PNG uses lossless compression and can preserve transparency when the document contains transparent pixels.
How the workflow fits together
A script is most useful when the same output steps happen repeatedly. For a one-time export, Photoshop’s normal save and export commands may be faster. For batch processing, add loops and validation so the script checks file types, destination folders, and existing filenames before saving.
Exercise
What does the true argument in each doc.saveAs call do, and why is it useful here?
Show answer
It saves a copy instead of changing the open document into the destination file. This lets the script create PSD, JPEG, and PNG outputs while keeping the working document available for further editing.