Document
Represents a single Photoshop document that is currently open You can access instances of documents using one of these methods:
const {app, constants} = require('photoshop');
// The currently active document from the Photoshop object
const currentDocument = app.activeDocument;
// Choose one of the open documents from the Photoshop object
const secondDocument = app.documents[1];
// You can also create an instance of a document via a UXP File entry
let fileEntry = require('uxp').storage.localFileSystem.getFileForOpening();
const newDocument = await app.open('/project.psd');
Properties
saveAs
• saveAs: object
Save the document to a desired file type.
For operations that require a UXP File token, use the UXP storage APIs to generate one.
let entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.psd");
document.saveAs.psd(entry);
// Save as a Copy (High JPG quality)
document.saveAs.jpg(entryJpg, { quality: 12 }, true);
// Save a PSB, with some options:
document.saveAs.psb(entryPsb, { embedColorProfile: true });
Type declaration
bmpgifjpgpngpsbpsdselection
• Readonly selection: Selection
The object containing the document's currently active selection
Properties
Methods
calculations
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">24.5</span>
<br/> async : Promise<void | Channel | Document>
The Calculations command lets you blend two individual channels from one or more source images. You can then apply the results to a new image or to a new channel or selection in the active image.
Performs Image > Calculations on the document. See the CalculationsOptions object for more info and examples.
const doc = app.activeDocument;
const options = {
source1: {
document: doc,
layer: doc.layers[0],
channel: constants.CalculationsChannel.GRAY
invert: true
},
source2: {
document: doc,
layer: constants.CalculationsLayer.MERGED,
channel: doc.channels[2]
},
blending: constants.CalculationsBlendMode.DARKEN,
opacity: 50,
result: constants.CalculationsResult.NEWCHANNEL
};
doc.calculations(options);
Known issue: currently calculations requires having exactly one unlocked pixel layer being selected otherwise it won't work. In future there should not be any layer requirements since this cannot output into layer.
Parameters
changeMode
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Changes the color mode of the document.
Parameters
close
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">22.5</span>
<br/> async : Promise<void>
Closes the document, showing a prompt to save unsaved changes if specified.
Parameters
closeWithoutSaving
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">22.5</span>
<br/> void
Close the document, discarding all unsaved changes.
convertProfile
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Changes the color profile.
destinationProfile must be either a string that names the color mode, or one of these below, meaning of the working color spaces or Lab color.
"Working RGB", "Working CMYK", "Working Gray", "Lab Color"
Parameters
createLayer
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Layer>
General form of the kind-specific methods below. See those methods for more information.
Create a new layer of the given kind. With no arguments, a pixel layer will be created. The options object will have properties specific to the kind, though all layers share a basic set of properties common to all. The override signatures below are provided as type guardrails to help ensure the options provided match the layer kind.
await doc.createLayer(); // defaults to pixel layer
await doc.createLayer(
constants.LayerKind.NORMAL, // pixel layer
{ name: "myLayer",
opacity: 80,
blendMode: constants.BlendMode.COLORDODGE }
);
Promise<Layer>
Parameters
async : Promise<Layer>
Create a new layer group.
await doc.createLayer(
constants.LayerKind.GROUP,
{ name: "myLayer", opacity: 80 }
);
Parameters
async : Promise<Layer>
Create a new text layer.
await doc.createLayer(
Constants.LayerKind.TEXT,
{ name: "message", contents: "Hello World" }
);
Parameters
createLayerGroup
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Layer>
Create a layer group using options described by GroupLayerCreateOptions.
const myEmptyGroup = await doc.createLayerGroup()
const myGroup = await doc.createLayerGroup({
name: "myLayer",
opacity: 80,
blendMode: "colorDodge"
});
const nonEmptyGroup = await doc.createLayerGroup({
name: "group",
fromLayers: [layer1, layer2]
});
const selectedGroup = await doc.createLayerGroup({
name: "group",
fromLayers: doc.activeLayers
});
Parameters
options?createPixelLayer
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">24.1</span>
<br/> async : Promise<Layer>
Create a pixel layer using options described by PixelLayerCreateOptions.
await doc.createPixelLayer({
name: "myLayer",
opacity: 80,
fillNeutral: true
});
Parameters
options?createTextLayer
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">24.2</span>
<br/> async : Promise<Layer>
Create a text layer using options described by TextLayerCreateOptions.
await doc.createTextLayer()
await doc.createTextLayer({
name: "myTextLayer",
contents: "Hello, World!",
fontSize: 32
});
Parameters
options?crop
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Crops the document to the given bounds.
Parameters
duplicate
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> Promise<Document>
Creates a duplicate of the document, making the duplicate active.
The optional parameter name provides the name for the duplicated document.
The optional parameter mergeLayersOnly indicates whether to only duplicate merged layers.
Parameters
name?mergeLayersOnly?duplicateLayers
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Layer[]>
Duplicates given layer(s), creating all copies above the top most one in layer stack, and returns the newly created layers.
// duplicate some layers
const layerCopies = await document.duplicateLayers([layer1, layer3])
layerCopies.forEach((layer) => { layer.blendMode = 'multiply' })
// ...to another document
const finalDoc = await photoshop.open('~/path/to/collated/image.psd')
await document.duplicateLayers([logo1, textLayer1], finalDoc)
await finalDoc.close(SaveOptions.SAVECHANGES)
Parameters
flatten
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">22.5</span>
<br/> async : Promise<void>
Flatten all layers in the document. The remaining layer will become Background.
generativeUpscale
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">27.2</span>
<br/> async : Promise<Document>
Applies generative upscaling to the currently selected layer(s) using AI-powered upscaling technology.
// Upscale using Firefly model with default options (2x scale)
await document.generativeUpscale(constants.GenerativeUpscaleModel.FIREFLY);
// Upscale using Firefly model with 4x scale
const doc4x = await document.generativeUpscale(constants.GenerativeUpscaleModel.FIREFLY, { scale: 4 });
Parameters
groupLayers
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Layer>
Create a layer group from existing layers.
const layers = doc.layers
const group = await doc.groupLayers(
[ layers[1], layers[2], layers[4] ]
);
Parameters
layerslinkLayers
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> Layer[]
Links layers together if possible, and returns a list of linked layers.
Parameters
mergeVisibleLayers
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Merges all visible layers in the document into a single layer. In constrast to flatten, mergeVisibleLayers will not convert the remaining layer to Background if no Background already exists. If not Background, then the name of the merged layer will be either that of the top of the selected layers or the top layer.
paste
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Layer>
Pastes the contents of the clipboard into the document. If the optional argument is set to true and a selection is active, the contents are pasted into the selection.
Parameters
intoSelection?rasterizeAllLayers
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Converts all layers to pixel layers.
resizeCanvas
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Changes the size of the document, but does not scale the image. To scale the image size, see resizeImage.
// grow the canvas by 400px
const {width, height} = await app.activeDocument;
await document.resizeCanvas(width + 400, height + 400);
Parameters
widthheightanchor?resizeImage
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Changes the size of the image by scaling the dimensions to meet the targeted number of pixels.
await document.resizeImage(800, 600)
Parameters
width?height?resolution?amount?revealAll
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Expands the document to show clipped sections.
rotate
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Rotates the image clockwise in given angle, expanding canvas if necessary. (Previously rotateCanvas)
Parameters
anglessampleColor
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">24.0</span>
<br/> async : Promise<SolidColor | NoColor>
Returns a SolidColor object sampled from the document at the given position.
let col = await app.activeDocument.sampleColor({x: 100, y: 100});
console.log(col.rgb);
// {
// red: 233,
// green: 179,
// blue: 135,
// hexValue: "E9B387"
// }
Parameters
position{x: number, y: number}.position.xposition.ysave
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Performs a save of the document. The user will be presented with a Save dialog if the file has yet to be saved on disk.
// To save a document in the current location
document.save()
// Shows the save dialog
unsavedDocument.save()
splitChannels
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<Document[]>
Splits the document channels into separate, single-channel documents.
suspendHistory
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> Promise<void>
Creates a single history state encapsulating everything that is done in the callback, only for this document. All changes to the document should be done in this callback.
Note: If you make changes to any other document, those changes will not be suspended in the same history state.
The callback is passed in a SuspendHistoryContext object, which contains the current document in a variable document.
For more info and advanced context, see core.executeAsModal API, for which suspendHistory is a simple wrapper.
app.activeDocument.suspendHistory(async (context) => {
// context.document below is, in this case, `app.activeDocument`
context.document.activeLayers[0].name = "Changed name";
});
Parameters
callbacke: SuspendHistoryContext) => voidhistoryStateNametrap
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Applies trapping to a CMYK document.
Valid only when Document.mode is Constants.DocumentMode.CMYK
Parameters
widthtrim
<span class="minversion" style="display: block; margin-bottom: -1em; margin-left: 36em; float:left; opacity:0.5;">23.0</span>
<br/> async : Promise<void>
Trims the area around the image according to the type of pixels given. All sides of the image are targeted by default. Optionally, the sides may be individually specified for exclusion.
// trim transparent pixels from only the bottom of the image
app.activeDocument.trim(constants.TrimType.TRANSPARENT, false, false, true, false);
Parameters
topleftbottomright