dxf22_jscode = """ //PS智能对象换图 function 模特换衣功能(){ var dialog = new Window("dialog"); dialog.text = "模特批量替换"; dialog.preferredSize.width = 400; dialog.preferredSize.height = 150; dialog.orientation = "column"; dialog.alignChildren = ["center","top"]; dialog.spacing = 10; dialog.margins = 16; // GROUP1 // ====== var group1 = dialog.add("group"); group1.orientation = "row"; group1.alignChildren = ["left","center"]; group1.spacing = 10; group1.margins = 0; var statictext1 = group1.add("statictext"); statictext1.text = "模板文件"; statictext1.justify = "center"; var edittext1 = group1.add("edittext"); edittext1.preferredSize.width = 250; var button1 = group1.add("button"); button1.text = "选择文件"; button1.justify = "center"; // GROUP2 // ====== var group2 = dialog.add("group"); group2.orientation = "row"; group2.alignChildren = ["left","center"]; group2.spacing = 10; group2.margins = 0; var statictext2 = group2.add("statictext"); statictext2.text = "素材目录"; statictext2.justify = "center"; var edittext2 = group2.add("edittext"); edittext2.preferredSize.width = 250; var button2 = group2.add("button"); button2.text = "选择目录"; button2.justify = "center"; // GROUP3 // ====== var group3 = dialog.add("group"); group3.orientation = "row"; group3.alignChildren = ["left","center"]; group3.spacing = 10; group3.margins = 0; var statictext3 = group3.add("statictext"); statictext3.text = "导出目录"; statictext3.justify = "center"; var edittext3 = group3.add("edittext"); edittext3.preferredSize.width = 250; var button3 = group3.add("button"); button3.text = "选择目录"; button3.justify = "center"; var group5 = dialog.add("group"); var cbox1 = group5.add("checkbox"); cbox1.text = "是否遍历子文件夹素材"; cbox1.value = true; var cbox2 = group5.add("checkbox"); cbox2.text = "是否保存文件结构"; cbox2.value = true; var cbox3 = group5.add("checkbox"); cbox3.text = "切片导出"; cbox3.value = false; // GROUP4 // ====== var group4 = dialog.add("group"); group4.orientation = "row"; group4.alignChildren = ["left","center"]; group4.spacing = 10; group4.margins = 0; var button4 = group4.add("button"); button4.text = "执行"; button4.justify = "center"; var button5 = group4.add("button"); button5.text = "退出"; button5.justify = "center"; button1.onClick = function() { var inputFile= app.openDialog(); if (inputFile != null) { edittext1.text = decodeURI(inputFile); } } button2.onClick = function() { var inputFolder = Folder.selectDialog("请选择素材目录:"); if (inputFolder != null) { edittext2.text = decodeURI(inputFolder); } } button3.onClick = function() { var inputFolder = Folder.selectDialog("请选择导出目录:"); if (inputFolder != null) { edittext3.text = decodeURI(inputFolder); } } button4.onClick = function() { 模板路径 = edittext1.text; 素材目录 = edittext2.text; 导出目录 = edittext3.text; main(模板路径,素材目录,导出目录); } button5.onClick = function() { dialog.close(); } function main(模板路径,素材目录,导出目录) { if(Folder(导出目录).exists==false){Folder(导出目录).create();} var doc = app.open(File(模板路径)); //~ 素材文件列表 = Folder(素材目录).getFiles("*.psd"); isSubFolders = cbox1.value; 素材文件列表 = 遍历目录指定类型文件(素材目录,isSubFolders); alert("当前目录一共有"+素材文件列表.length+"个素材。","提示:"); try { lay_替换对象图层名 = "替换对象"; lay_替换对象 = app.activeDocument.artLayers.getByName(lay_替换对象图层名); } catch(e) { alert("未找到["+lay_替换对象图层名+"]智能对象图层!","提示:"); } app.activeDocument.activeLayer = lay_替换对象; for(var i=0;i<素材文件列表.length;i++) { scpsd_path = 素材文件列表[i]; //~ lay_替换对象.visible = false; 素材名 = decodeURI(File(scpsd_path).name.replace(/(?:\.[^.]*$|$)/, '')); //替换内容 var d = new ActionDescriptor(); d.putPath(stringIDToTypeID("null"), new File(scpsd_path)); executeAction(stringIDToTypeID("placedLayerReplaceContents"), d, DialogModes.NO); if(cbox2.value) //保持结构 { 结构导出目录 = 导出目录+"/" + getRelativePath(decodeURI(File(scpsd_path).path), 素材目录); if(Folder(结构导出目录).exists==false){Folder(结构导出目录).create();} 保存路径 = 结构导出目录+"/"+素材名+".jpg"; $.writeln(保存路径); if(cbox3.value) //按切片 { //~ 按切片导出图片(结构导出目录,app.activeDocument.name.replace(/(?:\.[^.]*$|$)/, '')); 按切片导出图片(结构导出目录,素材名); } else { 保存JPG(保存路径); } } else { if(cbox3.value) //按切片 { //~ 按切片导出图片(导出目录,app.activeDocument.name.replace(/(?:\.[^.]*$|$)/, '')); 按切片导出图片(导出目录,素材名); } else { 保存JPG(导出目录+"/"+素材名+".jpg"); } } } doc.close(SaveOptions.DONOTSAVECHANGES); alert("处理完成!","提示:"); } function getRelativePath(targetPath, basePath) { var targetFile = new File(targetPath); var baseFolder = new Folder(basePath); var relativePath = targetFile.getRelativeURI(baseFolder); return decodeURI(relativePath); // 解码 URI 编码的路径 } function 保存JPG(jpg_save_path) { // 以JPEG格式保存输出 var jpegOptions = new JPEGSaveOptions(); // 将jpeg质量设置得很低,使文件很小 jpegOptions.quality = 12; app.activeDocument.saveAs(new File(jpg_save_path), jpegOptions,true); } function 遍历目录指定类型文件(inputFolder,isSubFolders) { if(isSubFolders==undefined) { isSubFolders = true; } all_files_list = []; if (inputFolder != null) { filesArray = scanFolder(inputFolder); if (filesArray.length > 0) { for (i = 0;i