// 定義源文件夾和目標文件夾路徑
var sourceFolderPath = "C:/Users/***/Desktop/拆分/";
var destFolderPath = "C:/Users/***/Desktop/結果/";
// 定義要填充的區域坐標 (x, y, 寬度, 高度)
var fillArea = {
x: 765, // 左上角x坐標
y: 66, // 左上角y坐標
width: 157, // 區域寬度
height: 24 // 區域高度
};
// 檢查源文件夾是否存在
var sourceFolder = new Folder(sourceFolderPath);
if (!sourceFolder.exists) {
alert("源文件夾不存在: " + sourceFolderPath);
exit();
}
// 創建目標文件夾(如果不存在)
var destFolder = new Folder(destFolderPath);
if (!destFolder.exists) {
destFolder.create();
}
// 獲取源文件夾中的所有JPG文件
var files = sourceFolder.getFiles(/\.(jpg|jpeg)$/i);
if (files.length === 0) {
alert("源文件夾中沒有找到JPG文件");
exit();
}
// 批量處理文件
var successCount = 0;
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (processFile(file)) {
successCount++;
}
}
alert("處理完成!\n成功處理: " + successCount + " 個文件\n失敗: " + (files.length - successCount) + " 個文件");
// 處理單個文件的函數
function processFile(file) {
try {
// 打開文件
var doc = app.open(file);
// 創建選框
doc.selection.select([
[fillArea.x, fillArea.y],
[fillArea.x + fillArea.width, fillArea.y],
[fillArea.x + fillArea.width, fillArea.y + fillArea.height],
[fillArea.x, fillArea.y + fillArea.height]
]);
// 設置前景色為白色
var whiteColor = new SolidColor();
whiteColor.rgb.red = 255;
whiteColor.rgb.green = 255;
whiteColor.rgb.blue = 255;
app.foregroundColor = whiteColor;
// 填充選框
doc.selection.fill(app.foregroundColor);
// 取消選擇
doc.selection.deselect();
// 創建保存路徑
var destFile = new File(destFolderPath + file.name);
// 保存到目標文件夾
var saveOptions = new JPEGSaveOptions();
saveOptions.quality = 12; // JPG質量,0-12(12為最高)
doc.saveAs(destFile, saveOptions, true);
doc.close(SaveOptions.DONOTSAVECHANGES);
return true;
} catch (e) {
alert("處理文件 " + file.name + " 時出錯: " + e.message);
return false;
}
}