Home  >  Article  >  Web Front-end  >  PhotoShop Scripting Guide

PhotoShop Scripting Guide

高洛峰
高洛峰Original
2017-02-24 09:27:2910404browse

PhotoshopScript language

Photoshop supports three scripting languages: AppleScript, VBScript, and JavaScript. Among them, AppleScript is for Apple system, VBScript is for Windows operating system, and JavaScript is compatible with Apple and Windows operating systems.

# This Photoshop can recognize the JavaScript script, and its script file suffix must be*.jsx or*.js files. You can open and execute JavaScript script files through File >Scripts >Browse.

PhotoshopObject Model

DOM (Document Object Model) is an API (Application Programming Interface). You can apply scripting language through DOM Perform various operations.

JavaScriptScript

1. Hello World example

The operation of this example is as follows: 1. Open Photoshop; 2. Create a new file; 3. Create a new ArtLayer layer; 4. Convert the ArtLayer to a text layer; 5. Set the text content to "Hello World".

The JavaScript script language is:

//Set the unit

app. preferences.rulerUnits = Units.INCHES

// Create a new file of 2*4INCHES

var docRef = app.documents.add( 2, 4 )

//Create a newArtLayer Layer

var artLayerRef = docRef.artLayers.add()

// Set the ArtLayer layer to the text layer

artLayerRef.kind = LayerKind.TEXT

//Set text layer text content

var textItemRef = artLayerRef.textItem

textItemRef.contents = " Hello World"

//Release reference

docRef = null

artLayerRef = null

textItemRef = null

The implementation effect is:

2. Obtain the Application object

You can obtain the Photoshop Application object through the predefined global object app. The following example illustrates how to obtain a Document file:

var docRef = app.documents[0]

The above expression can also be written as:

var docRef = documents[0]

3. Create a new object

You can create a new PSD file through File > New. For other types, such as layers, channels, paths, etc., you can create new ones using the menu or other methods. In JavaScript scripts, you can create new objects through add(). For example:

1) Create a new PSD file

documents.add() or app.documents.add()

2) Create a new ArtLayer layer

documents[0].artLayers.add()

4. Set the activation object

1) Set activation file

var docRef = app.documents[0]

app.activeDocument= docRef

2) Set the active ArtLayer layer

docRef.activeLayer = docRef.layers[0]

3) Set the activation channel

docRef.activeChannels = new Array(docRef.channels[0], docRef.channels[2])

5. Open a file

Because Photoshop can open a variety of formats Various, so you can use the open/Open/open() command to open an existing file.

1) Open a PSD file

var fileRef = File("C:/Users/Administrator/Desktop/test.psd")

var docRef = app.open(fileRef)

2) Open a Pdf file

//Set units

var originalRulerUnits = app.preferences.rulerUnits

app.preferences.rulerUnits = Units.PIXELS

//Get the name of the open file

var fileRef = new File("C:/Users/Administrator/Desktop/myfile.pdf")

//Create a new onePDFOpenOptions

var pdfOpenOptions = new PDFOpenOptions

pdfOpenOptions.antiAlias ​​= true

pdfOpenOptions.mode = OpenDocumentMode.RGB

pdfOpenOptions.resolution = 72

pdfOpenOptions.page = 3

//Open the file

app.open( fileRef, pdfOpenOptions )

6. Save the file

The file formats that Photoshop can save are as follows:

1) Save as jpg image

jpgFile = new File( "C:/Users/ Administrator/Desktop/test.jpg" )

jpgSaveOptions = new JPEGSaveOptions()

jpgSaveOptions.embedColorProfile = true

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE

jpgSaveOptions.matte = MatteType.NONE

jpgSaveOptions.quality = 1

app.activeDocument.saveAs(jpgFile, jpgSaveOptions, true,Extension.LOWERCASE)

6. Layer object

Photoshop object model It contains two layer objects: layer (ArtLayer) and group (Layer Set).

1) Create an ArtLayer layer object

//New file

app.documents.add ()

//New layer

##var layerRef = app.activeDocument.artLayers.add()

//Set layer name

layerRef.name = "MyBlendLayer"

layerRef.blendMode = BlendMode.NORMAL

2) Create a group

//New files and layers

app.documents.add()

var layer=app.activeDocument.artLayers.add()

layer.name="layer"

//New group and layer

var newLayerSetRef = app.activeDocument .layerSets.add()

newLayerSetRef.name="layerset"

##var layerset=newLayerSetRef.artLayers.add()

layerset.name="layerset"

7. Apply Layer Set object

You can move a layer to a group, or You can perform layer linking and other operations.

1) Copy the layer to the group

//

Create a new file, create a new layer, create a new group, and copy the layer to the group

var docRef = app.documents.add()

docRef.artLayers.add()

var layerSetRef = docRef.layerSets.add()

var layerRef = docRef.artLayers[0].duplicate(layerSetRef,ElementPlacement.PLACEATEND)

2) Link layer

var layerRef1 = docRef.artLayers.add()

var layerRef2 = docRef.artLayers.add()

layerRef1.link(layerRef2)

8. Apply text object

1) Convert ArtLayer to a text layer.

var newLayerRef = docRef.artLayers.add()

newLayerRef.kind = LayerKind.TEXT

2) Give Add text to the text layer

var textLayerRef = docRef.artLayers.add()

textLayerRef.name = "my text"

textLayerRef.kind = LayerKind.TEXT

var textItemRef = docRef. artLayers["my text"].textItem

textItemRef.contents = "Hello, World!"

textItemRef.justification = Justification.RIGHT

9. Application selection object

1) Create and define selection

var docRef = app.documents.add(500, 500)

var shapeRef = [

[0,0],

[0,100],

[100,100],

[100,0]

##]

2) Add border

strokeColor = new solidColor

strokeColor.cmyk.cyan = 20

strokeColor.cmyk.magenta = 50

strokeColor.cmyk.yellow = 30

strokeColor.cmyk.black = 0

app.activeDocument.selection.stroke (strokeColor, 2,StrokeLocation.OUTSIDE, ColorBlendMode.VIVIDLIGHT, 75, false)

3) Inverse selection

var selRef = app.activeDocument.selection

selRef.invert()

4) Expansion, infection, eclosion

var selRef = app.activeDocument.selection

selRef.expand( 5 )

selRef.contract( 5 )

selRef.feather( 5 )


Please pay attention to more PhotoShop script guide related articles PHP Chinese website!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn