photoshop脚本jsx概述
Photoshop支持使用脚本来批处理psd文件。此文章只是一些说明。
- psd脚本包含三类:appleScript、VbScript、JavaScript
- js脚本后缀支持:.js或者.jsx。可以自动加载也可以手动加载
- psd文档对象-包含关系:Application------包含DocumentCollection----包含DocumentObject-----包含ArtLayerCollection、HistoryStateCollection、LayersCollection、LayerSetCollection、ChannelsCollection
-
处理对象前,需要将对象激活、置顶、选中,即设置ActiveDocument或者ActiveLayer。
-
跳过optional parameter 方法里可以用 placeholder undefined
1.psd脚本使用方法
2 编写脚本所需工具:
- photoshopCC2014:运行脚本环境,查看psd文档,对照结果
- ExtendScript Toolkit:运行脚本、调试脚本 https://www.adobe.com/devnet/scripting/estk.html
- ScriptListener.8li :即ScriptListener,脚本录制器,用来实现jsx的sdk开发pdf文档里没有的功能
- json2.json:处理json对象
- jsonViewer:查看json对象
- visualStudio2017:编辑jsx脚本及代码美化
- photoshop 开发文档:https://www.adobe.com/devnet/photoshop/scripting.html
3.ExtendScript ToolKit:
快捷键 f7错误检查、f2 书签、ctrl+f2书签、f6切换文档、
4.常用代码片段
打开文件: var fileRef=new File("c:\\xx.pdf"); var pdfOpenOption=new PdfOpenOptions(); pdfOpenOption.antiAlias=true;//抗锯齿 ---------------.mode=OpenDocumentMode.RGB ---------------.resolution=72 ---------------.page=3 app.open(fileRef,pdfOpenOption)
保存图片: var jpgFile=new File("c:\\xx.jpg") jpgSaveOption=new JpegSaveOption() app.activeDocument.saveAs(jpegFile,jpgSaveOption,true,CaseExtention.Lower)
禁用对话框:
app.displayDialogs=DialogModes.No
调整图像和画布的尺寸: app.preferences.rulerUnits=units.INCHS var fileRef=new File(app.Path+"\samples/xx.tif") var docRef=app.Open(fileRef) docRef.ResizeImage(4,4) docRef.ResizeCanvas(4,4) docRef.Trim(TrimType.TOPLEFT,true,false,true,false)//trim意思是修剪,此处类似于超出边界时的修剪规则设置
裁剪图像 app.preference.rulerUnits=units.pixels docRef.Crop(new Array(100,200,300,300),45,20,2), docRef.flipCanvasDirection.Horizontal)//翻转 ArtLayer和LayerSets下面有collectionObjects。ArtLayers和LayerSets有Add()方法//layerSet相当于图层组 取得图层名: layers["Layer 3"].name var layerRef=app.activeDocument.layers[0] 图层融合模式 layerRef。blendMode=blendMode.Normal
多选:layer1Ref.link(layer2Ref)
加载第三方库: var c = $.fileName; var jsonF = c.substr(0, c.lastIndexOf("/")) + "/json2.js"; $.evalFile(jsonF)
打印日志:
$.writeln (text)
常用ExtendScript Toolkit软件界面: Console->查看日志 DataBrowser->调试数据实时查看
取色器取得某个点的颜色function getDocColor(doc, x, y) { try { var samplers = doc.colorSamplers; if (samplers.length == 0) { var posA = new Array(x, y); samplers.add(posA); } var sam = doc.colorSamplers[0]; var p = new Array(x, y) sam.move(p); var col = sam.color; return col.rgb.hexValue; } catch (e) { return "000000" } }
复制样式: var idCpFX = charIDToTypeID("CpFX"); executeAction(idCpFX, undefined, DialogModes.NO);
黏贴样式: var idPaFX = charIDToTypeID("PaFX"); var desc4 = new ActionDescriptor(); var idallowPasteFXOnLayerSet = stringIDToTypeID("allowPasteFXOnLayerSet"); desc4.putBoolean(idallowPasteFXOnLayerSet, true); executeAction(idPaFX, desc4, DialogModes.NO);
图层增加颜色函数 var r = 255; var g = 0; var b = 0; var a = 58; var idsetd = charIDToTypeID("setd"); var desc5 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); var ref2 = new ActionReference(); var idPrpr = charIDToTypeID("Prpr"); var idLefx = charIDToTypeID("Lefx"); ref2.putProperty(idPrpr, idLefx); var idLyr = charIDToTypeID("Lyr "); var idOrdn = charIDToTypeID("Ordn"); var idTrgt = charIDToTypeID("Trgt"); ref2.putEnumerated(idLyr, idOrdn, idTrgt); desc5.putReference(idnull, ref2); var idT = charIDToTypeID("T "); var desc6 = new ActionDescriptor(); var idScl = charIDToTypeID("Scl "); var idPrc = charIDToTypeID("#Prc"); desc6.putUnitDouble(idScl, idPrc, 100.000000); var idSoFi = charIDToTypeID("SoFi"); var desc7 = new ActionDescriptor(); var idenab = charIDToTypeID("enab"); desc7.putBoolean(idenab, true); var idMd = charIDToTypeID("Md "); var idBlnM = charIDToTypeID("BlnM"); var idNrml = charIDToTypeID("Nrml"); desc7.putEnumerated(idMd, idBlnM, idNrml); var idOpct = charIDToTypeID("Opct"); var idPrc = charIDToTypeID("#Prc"); desc7.putUnitDouble(idOpct, idPrc, a); var idClr = charIDToTypeID("Clr "); var desc8 = new ActionDescriptor(); var idRd = charIDToTypeID("Rd "); desc8.putDouble(idRd, r); var idGrn = charIDToTypeID("Grn "); desc8.putDouble(idGrn, g); var idBl = charIDToTypeID("Bl "); desc8.putDouble(idBl, b); var idRGBC = charIDToTypeID("RGBC"); desc7.putObject(idClr, idRGBC, desc8); var idSoFi = charIDToTypeID("SoFi"); desc6.putObject(idSoFi, idSoFi, desc7); var idLefx = charIDToTypeID("Lefx"); desc5.putObject(idT, idLefx, desc6); executeAction(idsetd, desc5, DialogModes.NO);
载入选区 必须在栅格化之后调用,因为不同格式的图层载入选区方式不一样,但是栅格化之后,旧可以得到正确的选取了 相当于ps里 按下ctrl键,然后左键点击图层 function LoadSelectLayerArea() { var idsetd = charIDToTypeID("setd"); var desc7 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); var ref8 = new ActionReference(); var idChnl = charIDToTypeID("Chnl"); var idfsel = charIDToTypeID("fsel"); ref8.putProperty(idChnl, idfsel); desc7.putReference(idnull, ref8); var idT = charIDToTypeID("T "); var ref9 = new ActionReference(); var idChnl = charIDToTypeID("Chnl"); var idChnl = charIDToTypeID("Chnl"); var idTrsp = charIDToTypeID("Trsp"); ref9.putEnumerated(idChnl, idChnl, idTrsp); desc7.putReference(idT, ref9); executeAction(idsetd, desc7, DialogModes.NO); }
栅格化-图层样式 function RasterizeLayerStyle() { var idrasterizeLayer = stringIDToTypeID("rasterizeLayer"); var desc2 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); var ref1 = new ActionReference(); var idLyr = charIDToTypeID("Lyr "); var idOrdn = charIDToTypeID("Ordn"); var idTrgt = charIDToTypeID("Trgt"); ref1.putEnumerated(idLyr, idOrdn, idTrgt); desc2.putReference(idnull, ref1); var idWhat = charIDToTypeID("What"); var idrasterizeItem = stringIDToTypeID("rasterizeItem"); var idlayerStyle = stringIDToTypeID("layerStyle"); desc2.putEnumerated(idWhat, idrasterizeItem, idlayerStyle); executeAction(idrasterizeLayer, desc2, DialogModes.NO); }
//获取文档某个位置的颜色 function getDocColor(doc, x, y) { try { var samplers = doc.colorSamplers; if (samplers.length == 0) { var posA = new Array(x, y); samplers.add(posA); } var sam = doc.colorSamplers[0]; var p = new Array(x, y) sam.move(p); var col = sam.color; return col.rgb.hexValue; } catch (e) { return "000000" } }