Home Web Front-end PS Tutorial PhotoShop Scripting Guide

PhotoShop Scripting Guide

Feb 24, 2017 am 09:27 AM
photoshop

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the reason why PS keeps showing loading? What is the reason why PS keeps showing loading? Apr 06, 2025 pm 06:39 PM

PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

What are the common questions about exporting PDF on PS What are the common questions about exporting PDF on PS Apr 06, 2025 pm 04:51 PM

Frequently Asked Questions and Solutions when Exporting PS as PDF: Font Embedding Problems: Check the "Font" option, select "Embed" or convert the font into a curve (path). Color deviation problem: convert the file into CMYK mode and adjust the color; directly exporting it with RGB requires psychological preparation for preview and color deviation. Resolution and file size issues: Choose resolution according to actual conditions, or use the compression option to optimize file size. Special effects issue: Merge (flatten) layers before exporting, or weigh the pros and cons.

How to solve the problem of loading when PS is always showing that it is loading? How to solve the problem of loading when PS is always showing that it is loading? Apr 06, 2025 pm 06:30 PM

PS card is "Loading"? Solutions include: checking the computer configuration (memory, hard disk, processor), cleaning hard disk fragmentation, updating the graphics card driver, adjusting PS settings, reinstalling PS, and developing good programming habits.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

How to use PS Pen Tool How to use PS Pen Tool Apr 06, 2025 pm 10:15 PM

The Pen Tool is a tool that creates precise paths and shapes, and is used by: Select the Pen Tool (P). Sets Path, Fill, Stroke, and Shape options. Click Create anchor point, drag the curve to release the Create anchor point. Press Ctrl/Cmd Alt/Opt to delete the anchor point, drag and move the anchor point, and click Adjust curve. Click the first anchor to close the path to create a shape, and double-click the last anchor to create an open path.

How to solve the problem of loading when the PS opens the file? How to solve the problem of loading when the PS opens the file? Apr 06, 2025 pm 06:33 PM

"Loading" stuttering occurs when opening a file on PS. The reasons may include: too large or corrupted file, insufficient memory, slow hard disk speed, graphics card driver problems, PS version or plug-in conflicts. The solutions are: check file size and integrity, increase memory, upgrade hard disk, update graphics card driver, uninstall or disable suspicious plug-ins, and reinstall PS. This problem can be effectively solved by gradually checking and making good use of PS performance settings and developing good file management habits.

How to draw vector PS How to draw vector PS Apr 06, 2025 pm 10:00 PM

Vector diagrams are images created using mathematical curves with the advantages of scalability, clarity, and small file size. Drawing vector graphics requires using vector editing software to create images by creating shapes, combining shapes, adding colors, adding text, grouping and layers.

See all articles