function createDialog() {
var dialog = new Window("dialog", "批量处理图片", undefined, {closeButton: true});
dialog.preferredSize.width = 450;
dialog.orientation = "column";
dialog.alignChildren = "fill";
dialog.spacing = 10;
dialog.margins = 16;
var inputGroup = dialog.add("group");
inputGroup.orientation = "row";
inputGroup.alignChildren = "center";
var inputLabel = inputGroup.add("statictext", undefined, "输入文件夹:");
var inputText = inputGroup.add("edittext", undefined, "");
inputText.preferredSize.width = 280;
var inputBtn = inputGroup.add("button", undefined, "浏览...");
var outputGroup = dialog.add("group");
outputGroup.orientation = "row";
outputGroup.alignChildren = "center";
var outputLabel = outputGroup.add("statictext", undefined, "输出文件夹:");
var outputText = outputGroup.add("edittext", undefined, "");
outputText.preferredSize.width = 280;
var outputBtn = outputGroup.add("button", undefined, "浏览...");
var statusPanel = dialog.add("panel", undefined, "处理状态");
statusPanel.orientation = "column";
statusPanel.alignChildren = "left";
statusPanel.margins = 20;
var currentFileText = statusPanel.add("statictext", undefined, "准备开始处理...");
currentFileText.preferredSize.width = 380;
var progressGroup = statusPanel.add("group");
progressGroup.orientation = "row";
var progressText = progressGroup.add("statictext", undefined, "0 / 0");
var btnGroup = dialog.add("group");
btnGroup.orientation = "row";
btnGroup.alignment = "center";
var okBtn = btnGroup.add("button", undefined, "确定", {name: "ok"});
var cancelBtn = btnGroup.add("button", undefined, "取消", {name: "cancel"});
inputBtn.onClick = function() {
var folder = Folder.selectDialog("选择输入文件夹");
if (folder) inputText.text = folder.fsName;
}
outputBtn.onClick = function() {
var folder = Folder.selectDialog("选择输出文件夹");
if (folder) outputText.text = folder.fsName;
}
return {
dialog: dialog,
inputText: inputText,
outputText: outputText,
currentFileText: currentFileText,
progressText: progressText
};
}
function processImage(doc) {
try {
var activeLayer = doc.activeLayer;
if (activeLayer.isBackgroundLayer) {
activeLayer.isBackgroundLayer = false;
}
var idautoCutout = stringIDToTypeID("autoCutout");
var desc01 = new ActionDescriptor();
desc01.putBoolean(stringIDToTypeID("sampleAllLayers"), false);
executeAction(idautoCutout, desc01, DialogModes.NO);
var idMk = charIDToTypeID("Mk ");
var desc02 = new ActionDescriptor();
var idNw = charIDToTypeID("Nw ");
var idChnl = charIDToTypeID("Chnl");
desc02.putClass(idNw, idChnl);
var idAt = charIDToTypeID("At ");
var ref01 = new ActionReference();
var idChnl = charIDToTypeID("Chnl");
var idChnl = charIDToTypeID("Chnl");
var idMsk = charIDToTypeID("Msk ");
ref01.putEnumerated(idChnl, idChnl, idMsk);
desc02.putReference(idAt, ref01);
var idUsng = charIDToTypeID("Usng");
var idUsrM = charIDToTypeID("UsrM");
var idRvlS = charIDToTypeID("RvlS");
desc02.putEnumerated(idUsng, idUsrM, idRvlS);
executeAction(idMk, desc02, DialogModes.NO);
var idsmartBrushWorkspace = stringIDToTypeID("smartBrushWorkspace");
var desc03 = new ActionDescriptor();
var idpresetKind = stringIDToTypeID("presetKind");
var idpresetKindType = stringIDToTypeID("presetKindType");
var idpresetKindCustom = stringIDToTypeID("presetKindCustom");
desc03.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom);
desc03.putInteger(stringIDToTypeID("smartBrushRadius"), 3);
desc03.putInteger(stringIDToTypeID("smartBrushSmooth"), 0);
desc03.putUnitDouble(stringIDToTypeID("smartBrushFeather"), charIDToTypeID("#Pxl"), 0);
desc03.putUnitDouble(stringIDToTypeID("smartBrushContrast"), charIDToTypeID("#Prc"), 0);
desc03.putUnitDouble(stringIDToTypeID("smartBrushShiftEdge"), charIDToTypeID("#Prc"), 0);
desc03.putBoolean(stringIDToTypeID("sampleAllLayers"), false);
desc03.putBoolean(stringIDToTypeID("smartBrushUseSmartRadius"), true);
desc03.putBoolean(stringIDToTypeID("smartBrushUseDeepMatte"), true);
desc03.putBoolean(stringIDToTypeID("autoTrimap"), true);
desc03.putBoolean(stringIDToTypeID("smartBrushDecontaminate"), true);
desc03.putUnitDouble(stringIDToTypeID("smartBrushDeconAmount"), charIDToTypeID("#Prc"), 100);
desc03.putString(stringIDToTypeID("refineTool"), "edgeRefineBrush");
desc03.putBoolean(stringIDToTypeID("refineToolAutoPath"), true);
var idrefineEdgeStroke = stringIDToTypeID("refineEdgeStroke");
var descStroke = new ActionDescriptor();
descStroke.putEnumerated(
stringIDToTypeID("refineEdgeStrokeType"),
stringIDToTypeID("refineEdgeStrokeType"),
stringIDToTypeID("refineEdgeStrokeAuto")
);
desc03.putObject(idrefineEdgeStroke, idrefineEdgeStroke, descStroke);
var idrefineEdgeOutput = stringIDToTypeID("refineEdgeOutput");
var idselectionOutputToNewSheetAndMask = stringIDToTypeID("selectionOutputToNewSheetAndMask");
desc03.putEnumerated(idrefineEdgeOutput, idrefineEdgeOutput, idselectionOutputToNewSheetAndMask);
executeAction(idsmartBrushWorkspace, desc03, DialogModes.NO);
doc.selection.deselect();
} catch (e) {
alert("处理图片时出错: " + e.message);
}
}
function batchProcess() {
try {
var ui = createDialog();
var result = ui.dialog.show();
if (result == 1) {
var inputFolder = new Folder(ui.inputText.text);
var outputFolder = new Folder(ui.outputText.text);
if (!inputFolder.exists) {
alert("输入文件夹不存在!");
return;
}
if (!outputFolder.exists) {
outputFolder.create();
}
var files = inputFolder.getFiles(/\.(jpg|jpeg|png|tif|tiff|psd)$/i);
if (files.length === 0) {
alert("没有找到支持的图片文件!");
return;
}
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;
var psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.alphaChannels = true;
psdSaveOptions.layers = true;
psdSaveOptions.spotColors = true;
psdSaveOptions.embedColorProfile = true;
for (var i = 0; i < files.length; i++) {
var file = files[i];
ui.currentFileText.text = "正在处理: " + file.name;
ui.progressText.text = (i + 1) + " / " + files.length;
$.sleep(100);
var doc = app.open(file);
processImage(doc);
var saveFile = new File(outputFolder + "/" + file.name.replace(/\.[^\.]+$/, "_processed.psd"));
doc.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;
alert("批量处理完成!\n共处理 " + files.length + " 个文件。");
}
} catch (e) {
alert("批量处理出错: " + e.message);
}
}
batchProcess();