Adobe Photoshop supports UXP scripting, allowing developers to automate repetitive tasks using JavaScript without creating a complete plugin.
In this tutorial, we will create a simple script that resizes an image to a maximum of 1920 pixels, maintains its aspect ratio and prevents upscaling.
Requirements
- Adobe Photoshop 23.5 or later.
- A text editor such as Visual Studio Code.
- An image for testing.
The JavaScript code
Create a file named resize-image.psjs and insert the following code:
const { app, core } = require("photoshop");
const MAX_SIZE = 1920;
if (app.documents.length === 0) {
await core.showAlert("Open an image first.");
} else {
const doc = app.activeDocument;
const scale = Math.min(
MAX_SIZE / Math.max(doc.width, doc.height), 1
);
if (scale < 1) {
await doc.resizeImage(
Math.max(1, Math.round(doc.width * scale)),
Math.max(1, Math.round(doc.height * scale))
);
}
await core.showAlert(
`${doc.width} x ${doc.height} px`
);
}
The script reads the image dimensions, calculates the scaling factor and uses the Photoshop resizeImage() API to resize the active document.
Change MAX_SIZE to customize the maximum image dimension.
Installation and usage
- Save the code as
resize-image.psjs. - Open an image in Photoshop.
- Select File → Scripts → Browse.
- Select the script and run it.
- Check the result under Image → Image Size.
No plugin installation is required. The script does not automatically save the image.
Expected results
| Original size | Output size |
|---|---|
| 4000 × 3000 px | 1920 × 1440 px |
| 3000 × 4000 px | 1440 × 1920 px |
| 3000 × 3000 px | 1920 × 1920 px |
| 1200 × 800 px | Unchanged |
The script has been successfully tested in Adobe Photoshop.
Conclusion
UXP scripting allows developers to create reusable automation tools for Adobe Photoshop with just a few lines of JavaScript.
This example provides a starting point for more advanced image-processing workflows.
Learn more
Official Adobe documentation: