如何修改Photoshop批量智能对象脚本以导出透明背景PNG
Photoshop批量替换智能对象并导出透明PNG脚本修改方案
我有一个用于Photoshop样机批量自动化的脚本,原本运行效果良好,但希望将其修改为导出带透明背景的PNG而非JPG。作为脚本新手,我附上了自己尝试修改的.jsx代码,恳请各位提供帮助。
以下是我尝试的代码:
// Replace SmartObject’s Content and Save as JPG // 2017, use it at your own risk #target photoshop if (app.documents.length > 0) { var myDocument = app.activeDocument; var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; var thePath = myDocument.path; var theLayer = myDocument.activeLayer; // PNG Options; pngSaveOptions = new pngSaveOptions(); pngSaveOptions.embedColorProfile = true; pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 11; // Check if layer is SmartObject; if (theLayer.kind != "LayerKind.SMARTOBJECT") { alert("selected layer is not a smart object") } else { // Select Files; if ($.os.search(/windows/i) != -1) { var theFiles = File.openDialog("please select files", "*.png;*.psd;*.tif;*.jpg", true) } else { var theFiles = File.openDialog("please select files", getFiles, true) }; if (theFiles) { for (var m = 0; m < theFiles.length; m++) { // Replace SmartObject theLayer = replaceContents(theFiles[m], theLayer); var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]; // Save png myDocument.saveAs((new File(thePath + "/" + theNewName + ".png")), pngSaveOptions, true,Extension.LOWERCASE); } } } }; // Get PSDs, TIFs and JPGs from files function getFiles(theFile) { if (theFile.name.match(/\.(png|psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") { return true }; }; // Replace SmartObject Contents function replaceContents(newFile, theSO) { app.activeDocument.activeLayer = theSO; // ======================================================= var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents"); var desc3 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); desc3.putPath(idnull, new File(newFile)); var idPgNm = charIDToTypeID("PgNm"); desc3.putInteger(idPgNm, 1); executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO); return app.activeDocument.activeLayer };
修正后的完整脚本
// 批量替换智能对象并导出透明PNG // 2017, 风险自负 #target photoshop if (app.documents.length > 0) { var myDocument = app.activeDocument; var thePath = myDocument.path; var theLayer = myDocument.activeLayer; // 初始化PNG保存选项 var pngSaveOptions = new PNGSaveOptions(); pngSaveOptions.embedColorProfile = true; pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.compression = 9; // PNG压缩等级(0-9,9为最高压缩) // 检查选中图层是否为智能对象 if (theLayer.kind !== LayerKind.SMARTOBJECT) { alert("选中的图层不是智能对象"); return; } // 选择要替换的素材文件 var theFiles; if ($.os.search(/windows/i) !== -1) { theFiles = File.openDialog("请选择素材文件", "*.png;*.psd;*.tif;*.jpg", true); } else { theFiles = File.openDialog("请选择素材文件", getFiles, true); }; if (theFiles) { for (var m = 0; m < theFiles.length; m++) { // 替换智能对象内容 theLayer = replaceContents(theFiles[m], theLayer); // 获取素材文件名(不含后缀) var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]; // 导出PNG var savePath = new File(thePath + "/" + theNewName + ".png"); myDocument.saveAs(savePath, pngSaveOptions, true, Extension.LOWERCASE); } alert("批量导出完成!"); } }; // 文件筛选函数:仅允许指定格式文件和文件夹 function getFiles(theFile) { return theFile.name.match(/\.(png|psd|tif|jpg)$/i) !== null || theFile.constructor.name === "Folder"; }; // 替换智能对象内容的核心函数 function replaceContents(newFile, theSO) { app.activeDocument.activeLayer = theSO; var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents"); var desc3 = new ActionDescriptor(); var idnull = charIDToTypeID("null"); desc3.putPath(idnull, new File(newFile)); var idPgNm = charIDToTypeID("PgNm"); desc3.putInteger(idPgNm, 1); executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO); return app.activeDocument.activeLayer; };
关键修改说明
- 修正智能对象判断逻辑:原代码用字符串
"LayerKind.SMARTOBJECT"和图层类型比较是错误的,直接使用枚举值LayerKind.SMARTOBJECT比较,确保判断有效。 - 修正PNG保存参数:PNG格式没有
quality属性,替换为PNG专属的compression属性,取值范围0-9,数值越高压缩率越大。 - 变量声明规范:给
pngSaveOptions加上var关键字,避免污染全局变量。 - 优化用户提示:将英文提示改为中文,添加批量完成后的提示弹窗,提升易用性。
- 代码格式优化:调整缩进和换行,让代码结构更清晰易读。
使用注意事项
- 确保你的Photoshop文档本身存在透明通道(即没有锁定的背景层,或背景层已隐藏/删除)。
- 运行脚本前请先选中要批量替换的智能对象图层。
- 选择素材时支持PNG、PSD、TIF、JPG格式文件。
内容的提问来源于stack exchange,提问作者Tali
相关产品推荐
相关产品推荐

