基础用例
让程序只在ps中运行
#target photoshop
ps窗口置顶
app.bringToFront()
图层移动
var layer = app.activeDocument.activeLayer
// x和y的单位是根据当前图层的单位
layer.translate(X, Y)
图层大小(根据描点)
mylayer.resize(
horizontal, // 宽的百分比 1、直接写数字是百分比 2、10厘米写作:UnitValue("10 cm")
vertical, // 高的百分比
anchor // 描点位置
)
// 首先选择一个描点
vat dot = AnchorPosition.MIDDLECENTER
layer.resize()
单位声明
// 英寸 in
// 像素 px
// 点 pt
// 厘米 cm
// 毫米 mm
var width = UnitValue("500 px")
var height = UnitValue("10 cm")
单位转换
var width_px = UnitValue("500 px")
// 但单位
var width_cm = width_px.convert("cm")
var width_mm = width_px.convert("mm")
var width_pt = width_px.convert("pt")
// 仅转换成数值,不带单位
var width_cm = width_px.as("cm")
var width_mm = width_px.as("mm")
var width_pt = width_px.as("pt")
全局对象
app {//整个PS软件对象
name,//软件名称
version,//软件版本
documents:{},//打开的文件的信息
fonts:{}//系统字体
recentFiles:{},//所有的打开文档对象(数组)
activeDocument:{}//当前激活的文档对象
}
软件信息
- 版本
alert(app.version)
文档对象
- 创建新文件
var width = 1080; //宽
var height = 1920; //高
var resolution=72; //分辨率
var fileName="phone"; //名称
var mode = NewDocumentMode.RGB;//色彩模式
var fillColor = DocumentFill.TRANSPARENT;//背景填充颜色
var pixelAspectRatio=1;//像素比率
app.documents.add(width, height, resolution, fileName, mode, fillColor, pixelAspectRatio);
- 列出当前所有文档
var docs = app.documents;
var msg = "";
for (var i = 0; i < docs.length; i++) {
msg +=
"名称:" + docs[i].name + "\r\n" +
"宽/像素:" + docs[i].width + "\r\n" +
"高/像素:" + docs[i].height + "\r\n";
}
alert(msg);
- 获取文档对象
var doc = app.activeDocument;
- 复制文件
// 拷贝当前文档 并打开
doc.duplicate(app.activeDocument.name + "-copy", 1);
修改文档尺寸
var idImgS = charIDToTypeID("ImgS")
var desc165 = new ActionDescriptor()
var idPxl = charIDToTypeID("#Pxl")
var idWdth
if ({witch_side} == "w" || {witch_side} == "width"){
idWdth = charIDToTypeID("Wdth")
}else{
idWdth = charIDToTypeID("Hght")
}
desc165.putUnitDouble(idWdth, idPxl, {size})
var idscaleStyles = stringIDToTypeID("scaleStyles")
desc165.putBoolean(idscaleStyles, True)
var idCnsP = charIDToTypeID("CnsP")
desc165.putBoolean(idCnsP, True)
var idIntr = charIDToTypeID("Intr")
var idIntp = charIDToTypeID("Intp")
var idautomaticInterpolation = stringIDToTypeID("automaticInterpolation")
desc165.putEnumerated(idIntr, idIntp, idautomaticInterpolation)
executeAction(idImgS, desc165, DialogModes.NO)
修改文档DPI
文件
- 打开一张jpg
var img = File("J:\\Java\\Android\\TolyGithub\\TolyTest\\toly_test\\src\\main\\res\\mipmap-xxhdpi\\bg_10.jpg");
var ok = confirm("打开图片?");
if (ok) {
open(img)
}
弹出框
- 基础弹出框,只有一个确认按钮
alert('xxxx')
- 带 “确认” 的弹出框
var ok = confirm("是否随机设置前景色和背景色?");
if (ok) {
//前景色
app.foregroundColor.rgb.red = 255;
app.foregroundColor.rgb.green = 255;
app.foregroundColor.rgb.blue = 255;
//背景色
app.backgroundColor.rgb.red = 255;
app.backgroundColor.rgb.green = 255;
app.backgroundColor.rgb.blue = 255;
}
颜色修改
- 前景色/背景色
app.foregroundColor.rgb.red = 255;
app.foregroundColor.rgb.green = 255;
app.foregroundColor.rgb.blue = 255;
app.backgroundColor.rgb.red = 255;
app.backgroundColor.rgb.green = 255;
app.backgroundColor.rgb.blue = 255;
图层相关
- 获取图层
//活动图层
var layer = app.activeDocument.activeLayer;
- 创建图层
// 新建一个空psd
var doc = app.documents.add(500, 200);
// 新建一个空图层
var artLayer = doc.artLayers.add()
- 文字图层
// 修改图层类型
artLayer.kind = LayerKind.TEXT;
// 文字
var textItem = artLayer.textItem
// 文字内容
textItem.contents:string
// 字体大小
textItem.size:float
// 位置
artLayer.translate(0, 100)
- 遍历所有图层
//获取图层对象
var layers = app.activeDocument.artLayers;
var msg = "";
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
msg += i + "--图层名称:" + layer.name + "\r\n";
}
alert(msg);
另存为
var doc = app.activeDocument; //当前文件对象
var outPath = new File("G:\\Photo\\龙少.png");
var options = PNGSaveOptions;//保存png模式
var asCopy = true;//副本方式保存
var extensionType = Extension.LOWERCASE;//拓展名小写
doc.saveAs(outPath, options, asCopy, extensionType);
alert("保存完成")
字体
- 列出当前所有字体
var fonts = app.fonts
for (var i = 0; i < fonts.length; i++) {
msg += "序号:" + (i + 1) + " 字体:" + fonts[i].name + "\r\n";
}
alert(msg);
- 导出所有字体到json文件
var fontFile = File("d:/psFonts.json")
fontFile.open("w")
fontFile.writeln("{")
fontFile.writeln(' "显示名":"设置名",')
for (var i = 0; i < fonts.length; i++) {
// 通过脚本设置时的真实名称
var psName = app.fonts[i].postScriptName
// 我们看到的名字
var outsideName = app.fonts[i].family
if (i != fonts.length - 1){
fontFile.writeln(' "' + outsideName + '":"' + psName + '",')
}else{
fontFile.writeln(' "' + outsideName + '":"' + psName + '"')
}
}
fontFile.writeln("}")
fontFile.close()
更新已改变的智能对象图层
var idplacedLayerUpdateAllModified = stringIDToTypeID("placedLayerUpdateAllModified");
executeAction(idplacedLayerUpdateAllModified, undefined, DialogModes.NO);
智能对象图层外链
var actionStr = "placedLayerConvertToLinked"
var idplacedLayerConvertToLinked = stringIDToTypeID( actionStr );
var desc20 = 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 );
desc20.putReference( idnull, ref1 );
var idUsng = charIDToTypeID( "Usng" );
// 这里的output是路径
// 注意,无论什么平台,这个路径都不应采用 "\",都应该替换为 "/"
desc20.putPath( idUsng, new File( "{output}" ) );
// 执行操作
executeAction( idplacedLayerConvertToLinked, desc20, DialogModes.NO );
锁定图层
var idapplyLocking = stringIDToTypeID("applyLocking");
var desc483 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref51 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref51.putEnumerated(idLyr, idOrdn, idTrgt);
desc483.putReference(idnull, ref51);
var idlayerLocking = stringIDToTypeID("layerLocking");
var desc484 = new ActionDescriptor();
// protectAll 锁定为锁定
// protectNone 为解锁
var idprotectAll = stringIDToTypeID("protectAll");
desc484.putBoolean(idprotectAll, true);
var idlayerLocking = stringIDToTypeID("layerLocking");
desc483.putObject(idlayerLocking, idlayerLocking, desc484);
executeAction(idapplyLocking, desc483, DialogModes.NO);
压缩保存gif
直接保存的png 有11.1M, web导出的0.3k,虽然效果上有些失色,还是很有价值的。
//压缩保存gif
var doc = app.activeDocument;//当前文件对象
var path = new File("G:\\Photo\\龙少.gif");
var eop = new ExportOptionsSaveForWeb();//web图片导出配置
eop.transparecy = false;//是否正常透明度
eop.includeProfile = true;//是否包含内置颜色配置文件
eop.lossy = 0;//有损压缩程度
eop.colors = 256;//色彩量
eop.colorReduction = ColorReductionType.SELECTIVE;////减低颜色深度算法:可选择
eop.formate = SaveDocumentType.COMPUSERVEGIF;//导出格式FIF
eop.ditherAmount = 0;//像素抖动值
eop.dither = Dither.NOISE;//杂色
eop.palette = Palette.LOCALADAPTIVE;//局部(随样性)
doc.exportDocument(path, ExportType.SAVEFORWEB, eop);
alert("保存完成:" + path);
//压缩保存jpeg
var doc = app.activeDocument;//当前文件对象
var filePath = new File("G:\\Photo\\龙少.jpeg");
var eop = new ExportOptionsSaveForWeb();//web图片导出配置
eop.quality = 60;//图片质量
doc.exportDocument(filePath, ExportType.SAVEFORWEB, eop);
alert("保存完成:" + filePath);
//压缩保存png
var doc = app.activeDocument;//当前文件对象
var filePath = new File("G:\\Photo\\龙少-compressed.png");
var eop = new ExportOptionsSaveForWeb();//web图片导出配置
eop.PNG8 = true;//png 8彩
doc.exportDocument(filePath, ExportType.SAVEFORWEB, eop);
alert("保存完成:" + filePath);