Edit in GitHubLog an issue

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
1// The currently active document from the Photoshop object
2const currentDocument = app.activeDocument
3
4// Choose one of the open documents from the Photoshop object
5const secondDocument = app.documents[1]
6
7// You can also create an instance of a document via a UXP File entry
8let fileEntry = require('uxp').storage.localFileSystem.getFileForOpening()
9const 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 clipboard
1let entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.psd");
2document.saveAs.psd(entry);
3
4// Save as a Copy (High JPG quality)
5document.saveAs.jpg(entryJpg, { quality: 12 }, true);
6
7// Save a PSB, with some options:
8document.saveAs.psb(entryPsb, { embedColorProfile: true });

Type declaration

名称类型
bmp(entry: File, saveOptions?: BMPSaveOptions, asCopy: boolean) => Promise<void>
gif(entry: File, saveOptions?: GIFSaveOptions, asCopy: boolean) => Promise<void>
jpg(entry: File, saveOptions?: JPEGSaveOptions, asCopy: boolean) => Promise<void>
png(entry: File, saveOptions?: PNGSaveOptions, asCopy: boolean) => Promise<void>
psb(entry: File, saveOptions?: PhotoshopSaveOptions, asCopy: boolean) => Promise<void>
psd(entry: File, saveOptions?: PhotoshopSaveOptions, asCopy: boolean) => Promise<void>

Properties

名称类型访问最低版本描述
activeChannelsChannel[]R W23.0Currently active channels of the document.
activeHistoryBrushSourceHistoryStateR W22.5The history state that history brush tool will use as its source.
activeHistoryStateHistoryStateR W22.5Currently active history state of the document.
activeLayersLayersR22.5The selected layers in the document.
artboardsLayersR22.5The artboards in the document
backgroundLayerLayerR22.5Background layer, if it exists.
bitsPerChannelBitsPerChannelTypeR W23.0The bits per color channel.
channelsChannelsR23.0All channels in the document.
cloudDocumentbooleanR23.0Check whether this a Photoshop cloud document.
cloudWorkAreaDirectorystringR23.0Local directory for this cloud document.
colorProfileNamestringR W23.0Name of the color profile. Valid only when colorProfileType is `CUSTOM` or `WORKING`, returns "None" otherwise.
colorProfileTypeColorProfileTypeR W23.0Whether the document uses the working color profile, a custom profile, or no profile.
colorSamplersColorSamplersR24.0The collection of ColorSamplers present in the document.
compositeChannelsChannel[]R23.0Composite channels in the document.
countItemsCountItemsR24.1The collection of CountItems present in the document.
guidesGuidesR23.0The collection of Guides present in the document.
heightnumberR22.5Document's height in pixels.
histogramnumber[]R23.0A histogram containing the number of pixels at each color intensity level for the composite channel. The array contains 256 members. Valid only when mode = `DocumentMode.{RGB,CMYK,INDEXEDCOLOR}`
historyStatesHistoryStatesR22.5History states of the document.
idnumberR22.5The 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.
layerCompsLayerCompsR24.0The layer comps present in the document.
layersLayersR22.5The layers in the document at the top level of the layer/group hierarchy.
modeDocumentModeR23.0The color mode. To change it, please use Document.changeMode.
namestringR23.0The name of the document
pathstringR22.5Full file system path to this document, or the identifier if it is a cloud document.
pathItemsPathItemsR23.3The collection of paths in this document, as shown in the Paths panel.
pixelAspectRationumberR W22.5The (custom) pixel aspect ratio to use.
quickMaskModebooleanR W23.0The state of Quick Mask mode. If true, the app is in Quick Mask mode.
resolutionnumberR22.5Document's resolution (in pixels per inch).
savedbooleanR23.0True if the document has been saved since the last change.
titlestringR22.5Document title
typenamestringR23.0The class name of the referenced object: "Document".
widthnumberR22.5Document's width in pixels.

Methods

changeMode

23.0

async : Promise<void>

Changes the color mode of the document.

Parameters


close

22.5

async : Promise<void>

Closes the document, showing a prompt to save unsaved changes if specified.

Parameters

名称类型描述
saveDialogOptionsSaveOptionsBy default, prompts a save dialog if there are unsaved changes.

closeWithoutSaving

22.5

void

Close the document, discarding all unsaved changes.


convertProfile

23.0

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

名称类型
destinationProfilestring
intentIntent
blackPointCompensation?boolean
dither?boolean

createLayer

23.0

async : Promise<Layer>

Create a new layer.

Copied to your clipboard
await doc.createLayer() // defaults to pixel layer

async : Promise<Layer>

Create a new pixel layer.

Copied to your clipboard
1await doc.createLayer(
2 Constants.LayerKind.NORMAL,
3 { name: "myLayer", opacity: 80, blendMode: Constants.BlendMode.COLORDODGE })

Parameters

名称类型描述
kindNORMALThe kind of layer to create Constants.LayerKind.
options?PixelLayerCreateOptionsThe 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 clipboard
await doc.createLayer( Constants.LayerKind.GROUP, { name: "myLayer", opacity: 80 })

Parameters

名称类型描述
kindGROUPThe kind of layer to create Constants.LayerKind.
options?GroupLayerCreateOptionsThe options for creation, including general layer options and those specific to the layer kind.

createLayerGroup

23.0

async : Promise<Layer>

Create a layer group using options described by GroupLayerCreateOptions.

Copied to your clipboard
1const myEmptyGroup = await doc.createLayerGroup()
2const myGroup = await doc.createLayerGroup({ name: "myLayer", opacity: 80, blendMode: "colorDodge" })
3const nonEmptyGroup = await doc.createLayerGroup({ name: "group", fromLayers: [layer1, layer2] })
4const selectedGroup = await doc.createLayerGroup({ name: "group", fromLayers: doc.activeLayers })

Parameters

名称类型
options?GroupLayerCreateOptions

createPixelLayer

24.1

async : Promise<Layer>

Create a pixel layer using options described by PixelLayerCreateOptions.

Copied to your clipboard
1await doc.createPixelLayer()
2await doc.createPixelLayer({ name: "myLayer", opacity: 80, fillNeutral: true })

Parameters

名称类型
options?PixelLayerCreateOptions

createTextLayer

24.2

async : Promise<Layer>

Create a text layer using options described by TextLayerCreateOptions.

Copied to your clipboard
1await doc.createTextLayer()
2await doc.createTextLayer({ name: "myTextLayer", contents: "Hello, World!", fontSize: 32 })

Parameters

名称类型
options?TextLayerCreateOptions

crop

23.0

async : Promise<void>

Crops the document to given bounds

Parameters

名称类型默认值
boundsBounds-
anglenumber0
widthnumber0
heightnumber0

duplicate

23.0

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?string
mergeLayersOnly?boolean

duplicateLayers

23.0

async : 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
1// duplicate some layers
2const layerCopies = await document.duplicateLayers([layer1, layer3])
3layerCopies.forEach((layer) => { layer.blendMode = 'multiply' })
4// ...to another document
5const finalDoc = await photoshop.open('~/path/to/collated/image.psd')
6await document.duplicateLayers([logo1, textLayer1], finalDoc)
7await finalDoc.close(SaveOptions.SAVECHANGES)

Parameters

名称类型描述
layersLayer[]
targetDocument?Documentif specified, duplicate to a different document target.

flatten

22.5

async : Promise<void>

Flatten all layers in the document.


groupLayers

23.0

async : Promise<Layer>

Create a layer group from existing layers.

Copied to your clipboard
1const layers = doc.layers
2const group = await doc.groupLayers([layers[1], layers[2], layers[4]])

Parameters

名称类型
layersLayer[]

linkLayers

23.0

Layer[]

Links layers together if possible, and returns a list of linked layers.

Parameters

名称类型描述
layersLayer[]array of layers to link together

mergeVisibleLayers

23.0

async : Promise<void>

Merges all visible layers in the document into a single layer.


paste

23.0

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?boolean

rasterizeAllLayers

23.0

async : Promise<void>

Rasterizes all layers.


resizeCanvas

23.0

async : 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
1// grow the canvas by 400px
2let width = await document.width
3let height = await document.height
4await document.resizeCanvas(width + 400, height + 400)

Parameters

名称类型描述
widthnumberNumeric value of new width in pixels
heightnumberNumeric value of new height in pixels
anchor?AnchorPositionAnchor point for resizing, by default will resize an equal amount on all sides.

resizeImage

23.0

async : Promise<void>

Changes the size of the image

Copied to your clipboard
await document.resizeImage(800, 600)

Parameters

名称类型描述
width?numberNumeric value of new width in pixels
height?numberNumeric value of new height in pixels
resolution?numberImage resolution in pixels per inch (ppi)
resampleMethod?ResampleMethodMethod used during image interpolation.
amount?numberNumeric value that controls the amount of noise value when using preserve details 0..100

revealAll

23.0

async : Promise<void>

Expands the document to show clipped sections.


rotate

23.0

async : Promise<void>

Rotates the image clockwise in given angle, expanding canvas if necessary. (Previously rotateCanvas)

Parameters

名称类型
anglesnumber

sampleColor

24.0

async : Promise<SolidColor | NoColor>

Returns a SolidColor object sampled from the document at the given position.

Copied to your clipboard
1let col = await app.activeDocument.sampleColor({x: 100, y: 100});
2console.log(col.rgb);
3// {
4// red: 233,
5// green: 179,
6// blue: 135,
7// hexValue: "E9B387"
8// }

Parameters

名称类型描述
positionobjectThe point to sample {x: number, y: number}.
position.xnumberThe horizontal coordinate in pixels.
position.ynumberThe vertical coordinate in pixels.

save

23.0

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.

Copied to your clipboard
1// To save a document in the current location
2document.save()
3
4// Shows the save dialog
5unsavedDocument.save()

splitChannels

23.0

async : Promise<Document[]>

Splits the document channels into separate, single-channel documents.


suspendHistory

23.0

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 this API is a simple wrapper for.

Copied to your clipboard
1 require("photoshop").app.activeDocument.suspendHistory(async (context) => {
2 // context.document is the `app.activeDocument`
3 context.document.activeLayers[0].name = "Changed name";
4 });

Parameters

名称类型
callback(e: SuspendHistoryContext) => void
historyStateNamestring

trap

23.0

async : Promise<void>

Applies trapping to a CMYK document.

Valid only when Document.mode is Constants.DocumentMode.CMYK

Parameters

名称类型
widthnumber

trim

23.0

async : Promise<void>

Trims the transparent area around the image on the specified sides of the canvas base on trimType

Parameters

名称类型默认值
trimTypeTrimType-
topbooleantrue
leftbooleantrue
bottombooleantrue
rightbooleantrue
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2023 Adobe. All rights reserved.