Document
Represents a single Photoshop document that is currently open You can access instances of documents using one of these methods:
Copied to your clipboard1// The currently active document from the Photoshop object2const currentDocument = app.activeDocument34// Choose one of the open documents from the Photoshop object5const secondDocument = app.documents[1]67// You can also create an instance of a document via a UXP File entry8let fileEntry = require('uxp').storage.localFileSystem.getFileForOpening()9const newDocument = await app.open('/project.psd')
Accessors
activeLayers
• get activeLayers(): Layer[]
The selected layers in the document
Copied to your clipboard1const layers = doc.activeLayers;2const 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 clipboard1const layers = currentDocument.layers2const 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 clipboard1const resolution = doc.resolution // 3002const width = doc.width // 21003const 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 clipboard1const myEmptyLayer = await doc.createLayer()2const 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 clipboard1const myEmptyGroup = await doc.createLayerGroup()2const myGroup = await doc.createLayerGroup({ name: "myLayer", opacity: 80, mode: "colorDodge" })3const 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 clipboard1// duplicate some layers2const layerCopies = await document.duplicateLayers([layer1, layer3])3layerCopies.forEach((layer) => { layer.blendMode = 'multiply' })4// ...to another document5const finalDoc = await photoshop.open('~/path/to/collated/image.psd')6await document.duplicateLayers([logo1, textLayer1], finalDoc)7await finalDoc.close(SaveDialogOptions.SAVE_CHANGES)
async
Parameters:
Name | Type | Description |
---|---|---|
layers | Layer[] | - |
targetDocument? | Document | 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 clipboard1const layers = doc.layers2const 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 clipboard1// grow the canvas by 400px2let width = await document.width3let height = await document.height4await 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 clipboard1// To save a document in the same location2document.save()34// Shows the save dialog5unsavedDocument.save()67// To save to a path, use UXP storage APIs to get a file for saving8let entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.psd")9document.save(entry)1011// To save to a path, but with some options:12document.save(entry, { embedColorProfile: true })
async
Parameters:
Name | Type |
---|---|
entry? | File |
saveOptions? | SaveOptions |