PhotoShop腳本指南

Photoshop腳本語言javascript

Photoshop支持三種腳本語言:AppleScript,VBScript,JavaScript。其中AppleScript爲蘋果系統,VBScript爲Windows操做系統,JavaScript兼容蘋果和Windows操做系統。java


         Photoshop可識別JavaScript腳本,其腳本文件後綴必須爲*.jsx或者*.js文件。你能夠經過文件(File)>腳本(Scripts) >瀏覽(Browse)打開並執行JavaScript腳本文件。app

Photoshop對象模型spa

         DOM(DocumentObject Model)即爲一個API(Application Programming Interface),你能夠經過DOM應用腳本語言執行各類操做。操作系統


JavaScript腳本code

一、Hello World示例orm

         本實例操做以下:一、打開Photoshop;二、新建一個文件;三、新建一個ArtLayer圖層;四、將ArtLayer轉換爲文本圖層;五、將文本內容設置爲「HelloWorld」。對象

JavaScript腳本語言爲:blog


         實現效果爲:圖片


//設置單位
app.preferences.rulerUnits =Units.INCHES
// 新建一個2*4INCHES的文件
var docRef =app.documents.add( 2, 4 )
//新建一個ArtLayer圖層
var artLayerRef =docRef.artLayers.add()
//設置ArtLayer圖層爲文本圖層
artLayerRef.kind =LayerKind.TEXT
//設置文本圖層文字內容
var textItemRef = artLayerRef.textItem
textItemRef.contents ="Hello World"
//釋放參考
docRef = null
artLayerRef = null
textItemRef = null

二、得到Application對象

         你能夠經過預約義的全局對象app得到Photoshop Application對象。下面的例子說明了如何獲取一個Document文件:

var docRef = app.documents[0]

上面的表達式也可寫爲:

var docRef = documents[0]

三、新建一個對象

         你能夠經過File > New新建一個PSD文件。別的類型的如圖層、通道、路徑等,你能夠用過菜單或者別的方式新建。在JavaScript腳本中,你能夠經過add()實現對象的新建。例如:

1) 新建一個PSD文件

documents.add()或者app.documents.add()

2) 新建一個ArtLayer圖層

documents[0].artLayers.add()

四、設置激活對象

1) 設置激活文件

 
var docRef = app.documents[0]
app.activeDocument= docRef

2) 設置激活ArtLayer圖層

docRef.activeLayer =docRef.layers[0]

3) 設置激活通道

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

五、打開一個文件

         因爲Photoshop能打開的格式多種多樣,因此你能夠選用open/Open/open()命令打開一個已存在的文件。


1) 打開一個PSD文件

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

2) 打開一個Pdf文件

//設置單位
var originalRulerUnits =app.preferences.rulerUnits
app.preferences.rulerUnits =Units.PIXELS
//得到打開文件的名稱
var fileRef = new File("C:/Users/Administrator/Desktop/myfile.pdf")
//新建一個PDFOpenOptions
var pdfOpenOptions = newPDFOpenOptions
pdfOpenOptions.antiAlias =true
pdfOpenOptions.mode =OpenDocumentMode.RGB
pdfOpenOptions.resolution = 72
pdfOpenOptions.page = 3
//打開文件
app.open( fileRef,pdfOpenOptions )

六、保存文件

         Photoshop可保存的文件格式以下:


1)  保存爲jpg圖片

jpgFile = new File("C:/Users/Administrator/Desktop/test.jpg" )
jpgSaveOptions = newJPEGSaveOptions()
jpgSaveOptions.embedColorProfile= true
jpgSaveOptions.formatOptions =FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte =MatteType.NONE
jpgSaveOptions.quality = 1
app.activeDocument.saveAs(jpgFile,jpgSaveOptions, true,Extension.LOWERCASE)

六、layer層對象

         Photoshop對象模型裏面包含兩個layer層對象:圖層(ArtLayer)和組(Layer Set)。

1) 建立一個ArtLayer圖層對象

//新建文件
app.documents.add()
//新建層
var layerRef =app.activeDocument.artLayers.add()
//設置層名稱
layerRef.name ="MyBlendLayer"
layerRef.blendMode =BlendMode.NORMAL

2) 建立一個組

//新建文件和圖層
app.documents.add()
varlayer=app.activeDocument.artLayers.add()
layer.name="layer"
//新建組和圖層
var newLayerSetRef =app.activeDocument.layerSets.add()
newLayerSetRef.name="layerset"
varlayerset=newLayerSetRef.artLayers.add()
layerset.name="layerset"

七、應用Layer Set對象

         你能夠將一個圖層移到一個組裏,也能夠進行圖層連接等操做。

1) 複製圖層到組

//新建文件,新建圖層,新建組,並複製圖層到組
var docRef =app.documents.add()
docRef.artLayers.add()
var layerSetRef =docRef.layerSets.add()
var layerRef =docRef.artLayers[0].duplicate(layerSetRef,ElementPlacement.PLACEATEND)

2) 連接圖層

var layerRef1 =docRef.artLayers.add()
var layerRef2 = docRef.artLayers.add()
layerRef1.link(layerRef2)

八、應用文本對象

1) ArtLayer轉換爲文本層。

var newLayerRef =docRef.artLayers.add()
newLayerRef.kind =LayerKind.TEXT

2) 給文本層添加文字

var textLayerRef =docRef.artLayers.add()
textLayerRef.name = "mytext"
textLayerRef.kind = LayerKind.TEXT
var textItemRef =docRef.artLayers["my text"].textItem
textItemRef.contents ="Hello, World!"
textItemRef.justification =Justification.RIGHT

九、應用選擇對象

1) 建立和定義選擇

var docRef =app.documents.add(500, 500)
var shapeRef = [
[0,0],
[0,100],
[100,100],
[100,0]
]

2) 添加邊框

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) 反向選擇

var selRef =app.activeDocument.selection
selRef.invert()

4) 擴展、感染、羽化

var selRef =app.activeDocument.selection
selRef.expand( 5 )
selRef.contract( 5 )
selRef.feather( 5 )
相關文章
相關標籤/搜索