Document
Represents a single Photoshop document that is currently open You can access instances of documents using one of these methods:
Copied to your clipboardconst app = require('photoshop').app;const constants = require('photoshop').constants;// The currently active document from the Photoshop objectconst currentDocument = app.activeDocument;// Choose one of the open documents from the Photoshop objectconst secondDocument = app.documents[1];// You can also create an instance of a document via a UXP File entrylet 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.
Copied to your clipboardlet 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
Name | Type |
---|---|
bmp | |
gif | |
jpg | |
png | |
psb | |
psd |
selection
• Readonly
selection: Selection
The object containing the document's currently active selection
Properties
Name | Type | Access | Min Version | Description |
---|---|---|---|---|
activeChannels | Channel[] | R W | 23.0 | Currently active channels of the document. (24.6) |
activeHistoryBrushSource | R W | 22.5 | The history state that history brush tool will use as its source. | |
activeHistoryState | R W | 22.5 | Currently active history state of the document. | |
activeLayers | R | 22.5 | The selected layers in the document. | |
artboards | R | 22.5 | The artboards in the document | |
backgroundLayer | R | 22.5 | Background layer, if it exists. | |
bitsPerChannel | R W | 23.0 | The bits per color channel. | |
channels | R | 23.0 | All channels in the document. | |
cloudDocument | boolean | R | 23.0 | Check whether this a Photoshop cloud document. |
cloudWorkAreaDirectory | string | R | 23.0 | Local directory for this cloud document. |
colorProfileName | string | R W | 23.0 | Name of the color profile. Valid only when colorProfileType is `CUSTOM` or `WORKING`, returns "None" otherwise. |
colorProfileType | R W | 23.0 | Whether the document uses the working color profile, a custom profile, or no profile. | |
colorSamplers | R | 24.0 | The collection of ColorSamplers present in the document. | |
componentChannels | Channel[] | R | 24.5 | Component channels in the document. (24.6) |
Channel[] | R | 23.0 | Deprecated since these channels are component not composite. Use `compositeChannels` above. | |
countItems | R | 24.1 | The collection of CountItems present in the document. | |
guides | R | 23.0 | The collection of Guides present in the document. | |
height | number | R | 22.5 | Document's height in pixels. |
histogram | number[] | R | 23.0 | A histogram containing the number of pixels at each color intensity level for the component channel. The array contains 256 members. Valid only when mode = `DocumentMode.{RGB,CMYK,INDEXEDCOLOR}` |
historyStates | R | 22.5 | History states of the document. | |
id | number | R | 22.5 | The internal ID of this document will remain valid as long as this document is open. It can be used for batchPlay calls to refer to this document. |
layerComps | R | 24.0 | The layer comps present in the document. | |
layers | R | 22.5 | The layers in the document at the top level of the layer/group hierarchy. | |
mode | R | 23.0 | The color mode. To change it, please use Document.changeMode. | |
name | string | R | 23.0 | The name of the document |
path | string | R | 22.5 | Full file system path to this document, or the identifier if it is a cloud document. |
pathItems | R | 23.3 | The collection of paths in this document, as shown in the Paths panel. | |
pixelAspectRatio | number | R W | 22.5 | The (custom) pixel aspect ratio to use. |
quickMaskMode | boolean | R W | 23.0 | The state of Quick Mask mode. If true, the app is in Quick Mask mode. |
resolution | number | R | 22.5 | Document's resolution (in pixels per inch). |
saved | boolean | R | 23.0 | True if the document has been saved since the last change. |
title | string | R | 22.5 | Document title |
typename | string | R | 23.0 | The class name of the referenced object: "Document". |
width | number | R | 22.5 | Document's width in pixels. |
zoom | number | R | 25.1 | Document's zoom factor in percent. |
Methods
calculations
24.5async : 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.
Copied to your clipboardconst doc = app.activeDocument;const options = {source1: {document: doc,layer: doc.layers[0],channel: CalculationsChannel.GRAYinvert: true},source2: {document: doc,layer: CalculationsLayer.MERGED,channel: doc.channels[2]},blending: CalculationsBlendMode.DARKEN,opacity: 50,result: 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
Name | Type | Description |
---|---|---|
calculationsOptions | Option object for the calculations. |
changeMode
23.0async : Promise<void>
Changes the color mode of the document.
Parameters
Name | Type |
---|---|
mode | |
options? |
close
22.5async : Promise<void>
Closes the document, showing a prompt to save unsaved changes if specified.
Parameters
Name | Type | Description |
---|---|---|
saveDialogOptions | By default, prompts a save dialog if there are unsaved changes. |
closeWithoutSaving
22.5void
Close the document, discarding all unsaved changes.
convertProfile
23.0async : 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
Name | Type |
---|---|
destinationProfile | string |
intent | |
blackPointCompensation? | boolean |
dither? | boolean |
createLayer
23.0async : Promise<Layer>
Create a new layer.
Copied to your clipboardawait doc.createLayer() // defaults to pixel layer
async : Promise<Layer>
Create a new pixel layer.
Copied to your clipboardawait doc.createLayer(Constants.LayerKind.NORMAL,{ name: "myLayer", opacity: 80, blendMode: Constants.BlendMode.COLORDODGE })
Parameters
Name | Type | Description |
---|---|---|
kind | The kind of layer to create Constants.LayerKind. | |
options? | The options for creation, including general layer options and those specific to the layer kind. |
async : Promise<Layer>
Create a new layer group.
Copied to your clipboardawait doc.createLayer( Constants.LayerKind.GROUP, { name: "myLayer", opacity: 80 })
Parameters
Name | Type | Description |
---|---|---|
kind | The kind of layer to create Constants.LayerKind. | |
options? | The options for creation, including general layer options and those specific to the layer kind. |
createLayerGroup
23.0async : Promise<Layer>
Create a layer group using options described by GroupLayerCreateOptions.
Copied to your clipboardconst 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
Name | Type |
---|---|
options? |
createPixelLayer
24.1async : Promise<Layer>
Create a pixel layer using options described by PixelLayerCreateOptions.
Copied to your clipboardawait doc.createPixelLayer()await doc.createPixelLayer({ name: "myLayer", opacity: 80, fillNeutral: true })
Parameters
Name | Type |
---|---|
options? |
createTextLayer
24.2async : Promise<Layer>
Create a text layer using options described by TextLayerCreateOptions.
Copied to your clipboardawait doc.createTextLayer()await doc.createTextLayer({ name: "myTextLayer", contents: "Hello, World!", fontSize: 32 })
Parameters
Name | Type |
---|---|
options? |
crop
23.0async : Promise<void>
Crops the document to given bounds
Parameters
Name | Type | Default value |
---|---|---|
bounds | - | |
angle | number | 0 |
width | number | 0 |
height | number | 0 |
duplicate
23.0Promise<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 | Type |
---|---|
name? | string |
mergeLayersOnly? | boolean |
duplicateLayers
23.0async : Promise<Layer[]>
Duplicates given layer(s), creating all copies above the top most one in layer stack, and returns the newly created layers.
Copied to your clipboard// duplicate some layersconst layerCopies = await document.duplicateLayers([layer1, layer3])layerCopies.forEach((layer) => { layer.blendMode = 'multiply' })// ...to another documentconst finalDoc = await photoshop.open('~/path/to/collated/image.psd')await document.duplicateLayers([logo1, textLayer1], finalDoc)await finalDoc.close(SaveOptions.SAVECHANGES)
Parameters
Name | Type | Description |
---|---|---|
layers | Layer[] | |
targetDocument? | if specified, duplicate to a different document target. |
flatten
22.5async : Promise<void>
Flatten all layers in the document.
groupLayers
23.0async : Promise<Layer>
Create a layer group from existing layers.
Copied to your clipboardconst layers = doc.layersconst group = await doc.groupLayers([layers[1], layers[2], layers[4]])
Parameters
Name | Type |
---|---|
layers | Layer[] |
linkLayers
23.0Layer[]
Links layers together if possible, and returns a list of linked layers.
Parameters
Name | Type | Description |
---|---|---|
layers | Layer[] | array of layers to link together |
mergeVisibleLayers
23.0async : Promise<void>
Merges all visible layers in the document into a single layer.
paste
23.0async : 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
Name | Type |
---|---|
intoSelection? | boolean |
rasterizeAllLayers
23.0async : Promise<void>
Rasterizes all layers.
resizeCanvas
23.0async : Promise<void>
Changes the size of the canvas, but does not change image size To change the image size, see resizeImage
Copied to your clipboard// grow the canvas by 400pxlet width = await document.widthlet height = await document.heightawait document.resizeCanvas(width + 400, height + 400)
Parameters
Name | Type | Description |
---|---|---|
width | number | Numeric value of new width in pixels |
height | number | Numeric value of new height in pixels |
anchor? | Anchor point for resizing, by default will resize an equal amount on all sides. |
resizeImage
23.0async : Promise<void>
Changes the size of the image
Copied to your clipboardawait document.resizeImage(800, 600)
Parameters
Name | Type | Description |
---|---|---|
width? | number | Numeric value of new width in pixels |
height? | number | Numeric value of new height in pixels |
resolution? | number | Image resolution in pixels per inch (ppi) |
resampleMethod? | Method used during image interpolation. | |
amount? | number | Numeric value that controls the amount of noise value when using preserve details 0..100 |
revealAll
23.0async : Promise<void>
Expands the document to show clipped sections.
rotate
23.0async : Promise<void>
Rotates the image clockwise in given angle, expanding canvas if necessary. (Previously rotateCanvas)
Parameters
Name | Type |
---|---|
angles | number |
sampleColor
24.0async : Promise<SolidColor | NoColor>
Returns a SolidColor object sampled from the document at the given position.
Copied to your clipboardlet col = await app.activeDocument.sampleColor({x: 100, y: 100});console.log(col.rgb);// {// red: 233,// green: 179,// blue: 135,// hexValue: "E9B387"// }
Parameters
Name | Type | Description |
---|---|---|
position | object | The point to sample {x: number, y: number} . |
position.x | number | The horizontal coordinate in pixels. |
position.y | number | The vertical coordinate in pixels. |
save
23.0async : 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.
Copied to your clipboard// To save a document in the current locationdocument.save()// Shows the save dialogunsavedDocument.save()
splitChannels
23.0async : Promise<Document[]>
Splits the document channels into separate, single-channel documents.
suspendHistory
23.0Promise<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 this API is a simple wrapper for.
Copied to your clipboardrequire("photoshop").app.activeDocument.suspendHistory(async (context) => {// context.document is the `app.activeDocument`context.document.activeLayers[0].name = "Changed name";});
Parameters
Name | Type |
---|---|
callback | ( e : SuspendHistoryContext) => void |
historyStateName | string |
trap
23.0async : Promise<void>
Applies trapping to a CMYK document.
Valid only when Document.mode is Constants.DocumentMode.CMYK
Parameters
Name | Type |
---|---|
width | number |
trim
23.0async : Promise<void>
Trims the transparent area around the image on the specified sides of the canvas base on trimType
Parameters
Name | Type | Default value |
---|---|---|
trimType | - | |
top | boolean | true |
left | boolean | true |
bottom | boolean | true |
right | boolean | true |