Document
Represents a single Photoshop document that is currently open You can access instances of documents using one of these methods:
Copied to your clipboard// 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')
Accessors
activeLayers
• get activeLayers(): Layer[]
The selected layers in the document
Copied to your clipboardconst layers = doc.activeLayers;const topLayer = layers[0]
backgroundLayer
• get backgroundLayer(): Layer | null
Background layer, if it exists
Copied to your clipboardconst bgLayer = currentDocument.backgroundLayer
height
• get height(): number
Document's height in pixels
layerTree
• get layerTree(): LayerTypes[]
Top level layers in the document
layers
• get layers(): LayerTypes[]
All the layers in the document, in a flat list
Copied to your clipboardconst layers = currentDocument.layersconst topLayer = layers[0]
path
• get path(): string
Path to this document, or it's identifier if a cloud document
resolution
• get resolution(): number
Document's resolution (in pixels per inch)
For example, in the default PS document (7 in wide by 5 in tall at 300 ppi)
Copied to your clipboardconst resolution = doc.resolution // 300const width = doc.width // 2100const height = doc.height // 1500
title
• get title(): string
Document title
Copied to your clipboardconst currentTitle = doc.title
readonly
width
• get width(): number
Document's width in pixels
Methods
close
▸ close(saveDialogOptions?: SaveDialogOptions): Promise‹void›
Closes the document, showing a prompt to save unsaved changes if specified
async
Parameters:
| Name | Type |
|---|---|
saveDialogOptions? | SaveDialogOptions |
closeWithoutSaving
▸ closeWithoutSaving(): void
createLayer
▸ createLayer(options?: LayerCreateOptions): Promise‹Layer | null›
Create a layer. See @CreateOptions
Copied to your clipboardconst myEmptyLayer = await doc.createLayer()const myLayer = await doc.createLayer({ name: "myLayer", opacity: 80, mode: "colorDodge" })
async
Parameters:
| Name | Type |
|---|---|
options? | LayerCreateOptions |
createLayerGroup
▸ createLayerGroup(options?: GroupLayerCreateOptions): Promise‹GroupLayer | null›
Create a layer group. See @CreateOptions
Copied to your clipboardconst myEmptyGroup = await doc.createLayerGroup()const myGroup = await doc.createLayerGroup({ name: "myLayer", opacity: 80, mode: "colorDodge" })const nonEmptyGroup = await doc.createLayerGroup({ name: "group", fromLayers: [layer1, layer2] })
async
Parameters:
| Name | Type |
|---|---|
options? | GroupLayerCreateOptions |
crop
▸ crop(bounds: PsCommon.Bounds, angle: number): Promise‹void›
Crops the document to given bounds
async
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
bounds | PsCommon.Bounds | - | - |
angle | number | 0 |
duplicateLayers
▸ duplicateLayers(layers: Layer[], targetDocument?: Document): 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(SaveDialogOptions.SAVE_CHANGES)
async
Parameters:
| Name | Type | Description |
|---|---|---|
layers | Layer[] | - |
targetDocument? | if specified, duplicate to a different document target. |
flatten
▸ flatten(): Promise‹void›
Flatten all layers in the document
async
groupLayers
▸ groupLayers(layers: Layer[]): Promise‹GroupLayer | null›
Create a layer group from existing layers.
Copied to your clipboardconst layers = doc.layersconst group = await doc.groupLayers([layers[1], layers[2], layers[4]])
async
Parameters:
| Name | Type |
|---|---|
layers | Layer[] |
linkLayers
▸ linkLayers(layers: Layer[]): Layer[]
Links layers together if possible, and returns a list of linked layers.
Parameters:
| Name | Type | Description |
|---|---|---|
layers | Layer[] | array of layers to link together |
array of successfully linked layers
mergeVisibleLayers
▸ mergeVisibleLayers(): Promise‹void›
Flattens all visible layers in the document
async
resizeCanvas
▸ resizeCanvas(width: number, height: number, anchor?: AnchorPosition): 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)
async
Parameters:
| Name | Type | Description |
|---|---|---|
width | number | Numeric value of new width in pixels |
height | number | Numeric value of new height in pixels |
anchor? | AnchorPosition | Anchor point for resizing, by default will resize an equal amount on all sides. Of format: [top/middle/bottom]-[left/center/right] |
resizeImage
▸ resizeImage(width: number, height: number, resolution?: number, resampleMethod?: ResampleMethod): Promise‹void›
Changes the size of the image
Copied to your clipboardawait document.resizeImage(800, 600)
async
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? | ResampleMethod | Method used during image interpolation. Possible values are:
|
rotate
▸ rotate(angles: number): Promise‹void›
Rotates the image clockwise in given angle, expanding canvas if necessary
async
Parameters:
| Name | Type |
|---|---|
angles | number |
save
▸ save(entry?: File, saveOptions?: SaveOptions): Promise‹void›
Saves the document or a copy, the format is deduced by the extension
Copied to your clipboard// To save a document in the same locationdocument.save()// Shows the save dialogunsavedDocument.save()// To save to a path, use UXP storage APIs to get a file for savinglet entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.psd")document.save(entry)// To save to a path, but with some options:document.save(entry, { embedColorProfile: true })
async
Parameters:
| Name | Type |
|---|---|
entry? | File |
saveOptions? | SaveOptions |
