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')
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 clipboard1let entry = await require('uxp').storage.localFileSystem.getFileForSaving("target.psd");2document.saveAs.psd(entry);34// Save as a Copy (High JPG quality)5document.saveAs.jpg(entryJpg, { quality: 12 }, true);67// 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
名称 | 类型 | 访问 | 最低版本 | 描述 |
---|---|---|---|---|
activeChannels | Channel[] | R W | 23.0 | Currently active channels of the document. |
activeHistoryBrushSource | HistoryState | R W | 22.5 | The history state that history brush tool will use as its source. |
activeHistoryState | HistoryState | R W | 22.5 | Currently active history state of the document. |
activeLayers | Layers | R | 22.5 | The selected layers in the document. |
artboards | Layers | R | 22.5 | The artboards in the document |
backgroundLayer | Layer | R | 22.5 | Background layer, if it exists. |
bitsPerChannel | BitsPerChannelType | R W | 23.0 | The bits per color channel. |
channels | 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 | ColorProfileType | R W | 23.0 | Whether the document uses the working color profile, a custom profile, or no profile. |
colorSamplers | ColorSamplers | R | 24.0 | The collection of ColorSamplers present in the document. |
compositeChannels | Channel[] | R | 23.0 | Composite channels in the document. |
countItems | CountItems | R | 24.1 | The collection of CountItems present in the document. |
guides | 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 composite channel. The array contains 256 members. Valid only when mode = `DocumentMode.{RGB,CMYK,INDEXEDCOLOR}` |
historyStates | 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 | LayerComps | R | 24.0 | The layer comps present in the document. |
layers | Layers | R | 22.5 | The layers in the document at the top level of the layer/group hierarchy. |
mode | DocumentMode | 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 | 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. |
Methods
changeMode
23.0async : Promise<void>
Changes the color mode of the document.
Parameters
名称 | 类型 |
---|---|
mode | ChangeMode |
options? | BitmapConversionOptions | IndexedConversionOptions |
close
22.5async : Promise<void>
Closes the document, showing a prompt to save unsaved changes if specified.
Parameters
名称 | 类型 | 描述 |
---|---|---|
saveDialogOptions | SaveOptions | 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
名称 | 类型 |
---|---|
destinationProfile | string |
intent | 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 clipboard1await doc.createLayer(2 Constants.LayerKind.NORMAL,3 { name: "myLayer", opacity: 80, blendMode: Constants.BlendMode.COLORDODGE })
Parameters
名称 | 类型 | 描述 |
---|---|---|
kind | NORMAL | The kind of layer to create Constants.LayerKind. |
options? | PixelLayerCreateOptions | 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
名称 | 类型 | 描述 |
---|---|---|
kind | GROUP | The kind of layer to create Constants.LayerKind. |
options? | GroupLayerCreateOptions | 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 clipboard1const 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.1async : Promise<Layer>
Create a pixel layer using options described by PixelLayerCreateOptions.
Copied to your clipboard1await doc.createPixelLayer()2await doc.createPixelLayer({ name: "myLayer", opacity: 80, fillNeutral: true })
Parameters
名称 | 类型 |
---|---|
options? | PixelLayerCreateOptions |
createTextLayer
24.2async : Promise<Layer>
Create a text layer using options described by TextLayerCreateOptions.
Copied to your clipboard1await doc.createTextLayer()2await doc.createTextLayer({ name: "myTextLayer", contents: "Hello, World!", fontSize: 32 })
Parameters
名称 | 类型 |
---|---|
options? | TextLayerCreateOptions |
crop
23.0async : Promise<void>
Crops the document to given bounds
Parameters
名称 | 类型 | 默认值 |
---|---|---|
bounds | 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? | 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 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(SaveOptions.SAVECHANGES)
Parameters
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 clipboard1const layers = doc.layers2const group = await doc.groupLayers([layers[1], layers[2], layers[4]])
Parameters
名称 | 类型 |
---|---|
layers | Layer[] |
linkLayers
23.0Layer[]
Links layers together if possible, and returns a list of linked layers.
Parameters
名称 | 类型 | 描述 |
---|---|---|
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
名称 | 类型 |
---|---|
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 clipboard1// grow the canvas by 400px2let width = await document.width3let height = await document.height4await document.resizeCanvas(width + 400, height + 400)
Parameters
名称 | 类型 | 描述 |
---|---|---|
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. |
resizeImage
23.0async : Promise<void>
Changes the size of the image
Copied to your clipboardawait document.resizeImage(800, 600)
Parameters
名称 | 类型 | 描述 |
---|---|---|
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. |
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
名称 | 类型 |
---|---|
angles | number |
sampleColor
24.0async : Promise<SolidColor | NoColor>
Returns a SolidColor object sampled from the document at the given position.
Copied to your clipboard1let 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
名称 | 类型 | 描述 |
---|---|---|
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 clipboard1// To save a document in the current location2document.save()34// Shows the save dialog5unsavedDocument.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 clipboard1 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 |
historyStateName | string |
trap
23.0async : Promise<void>
Applies trapping to a CMYK document.
Valid only when Document.mode is Constants.DocumentMode.CMYK
Parameters
名称 | 类型 |
---|---|
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
名称 | 类型 | 默认值 |
---|---|---|
trimType | TrimType | - |
top | boolean | true |
left | boolean | true |
bottom | boolean | true |
right | boolean | true |