-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatch_Resize_1280px.jsx
More file actions
188 lines (154 loc) · 7.34 KB
/
Copy pathBatch_Resize_1280px.jsx
File metadata and controls
188 lines (154 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Photoshop Script: Batch image Resizer
Features: Landscape/Portrait | JPG/PNG/WebP | Copyright Metadata
Author: Tuxxin
*/
#target photoshop
function main() {
// --- 1. BUILD THE UI ---
var win = new Window("dialog", "Batch Resize & Copyright Tool");
win.orientation = "column";
win.alignChildren = "fill";
// Panel: Resize Options
var pnlResize = win.add("panel", undefined, "Resize Mode (Target: 1280px)");
pnlResize.orientation = "column";
pnlResize.alignChildren = "left";
var radLandscape = pnlResize.add("radiobutton", undefined, "Landscape (Fix Width to 1280px)");
var radPortrait = pnlResize.add("radiobutton", undefined, "Portrait (Fix Height to 1280px)");
radLandscape.value = true;
// Panel: Format Options
var pnlFormat = win.add("panel", undefined, "Output Format");
pnlFormat.orientation = "column";
pnlFormat.alignChildren = "left";
var radJPG = pnlFormat.add("radiobutton", undefined, "JPG (Quality 7, Progressive)");
var radPNG = pnlFormat.add("radiobutton", undefined, "PNG (Transparent)");
var radWebP = pnlFormat.add("radiobutton", undefined, "WebP (Requires Photoshop 23.2+)");
radJPG.value = true;
// Panel: Copyright Info
var pnlMeta = win.add("panel", undefined, "Metadata");
pnlMeta.orientation = "column";
pnlMeta.alignChildren = "fill";
pnlMeta.add("statictext", undefined, "Copyright Notice:");
var txtCopyright = pnlMeta.add("edittext", undefined, "");
txtCopyright.helpTip = "e.g. (c) 2026 My Company";
// Buttons
var grpBtns = win.add("group");
grpBtns.orientation = "row";
var btnRun = grpBtns.add("button", undefined, "Select Folders & Run");
var btnCancel = grpBtns.add("button", undefined, "Cancel");
btnRun.onClick = function() { win.close(1); }
btnCancel.onClick = function() { win.close(0); }
var result = win.show();
if (result != 1) return;
// --- 2. SELECT FOLDERS ---
var inputFolder = Folder.selectDialog("Select Source Folder");
if (inputFolder == null) return;
var outputFolder = Folder.selectDialog("Select Destination Folder");
if (outputFolder == null) return;
// --- 3. IMAGE SETTINGS ---
var targetSize = 1280;
var resizeMode = radLandscape.value ? "width" : "height";
var copyrightText = txtCopyright.text;
var formatMode = "jpg";
if (radPNG.value) formatMode = "png";
if (radWebP.value) formatMode = "webp";
var fileList = inputFolder.getFiles();
var successCount = 0;
var errorCount = 0;
var restoreDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;
// --- 4. PROCESSING LOOP ---
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof File && isImageFile(file)) {
try {
processFile(file, outputFolder, targetSize, resizeMode, formatMode, copyrightText);
successCount++;
} catch (e) {
errorCount++;
// Debug: disabled by default so batch continues
// alert("Error on file " + file.name + ": " + e);
}
}
}
app.displayDialogs = restoreDisplayDialogs;
var msg = "Batch Complete!\nProcessed: " + successCount;
if (errorCount > 0) msg += "\nErrors/Skipped: " + errorCount;
alert(msg);
}
function processFile(file, outputFolder, targetSize, resizeMode, formatMode, copyrightText) {
var doc = open(file);
// 1. ADD METADATA
if (copyrightText.length > 0) {
doc.info.copyrighted = CopyrightedType.COPYRIGHTEDWORK;
doc.info.copyrightNotice = copyrightText;
}
// 2. RESIZE
if (resizeMode === "width") {
if (doc.width > targetSize) {
var ratio = doc.height / doc.width;
var newHeight = targetSize * ratio;
doc.resizeImage(UnitValue(targetSize, "px"), UnitValue(newHeight, "px"), null, ResampleMethod.BICUBIC);
}
} else {
if (doc.height > targetSize) {
var ratio = doc.width / doc.height;
var newWidth = targetSize * ratio;
doc.resizeImage(UnitValue(newWidth, "px"), UnitValue(targetSize, "px"), null, ResampleMethod.BICUBIC);
}
}
// 3. SAVE LOGIC
var docName = decodeURI(doc.name);
var baseName = docName.substring(0, docName.lastIndexOf('.')) || docName;
if (formatMode === "jpg") {
// Prepare for JPG (Must be 8bit RGB)
if (doc.mode != DocumentMode.RGB) doc.changeMode(ChangeMode.RGB);
if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
var saveFile = new File(outputFolder + "/" + baseName + ".jpg");
var jpgOpts = new JPEGSaveOptions();
jpgOpts.quality = 7;
jpgOpts.embedColorProfile = true;
jpgOpts.formatOptions = FormatOptions.PROGRESSIVE;
jpgOpts.scans = 5;
jpgOpts.matte = MatteType.NONE;
doc.saveAs(saveFile, jpgOpts, true, Extension.LOWERCASE);
} else if (formatMode === "png") {
// Prepare for PNG (Must be RGB, supports transparency)
if (doc.mode == DocumentMode.CMYK) doc.changeMode(ChangeMode.RGB);
if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
var saveFile = new File(outputFolder + "/" + baseName + ".png");
var pngOpts = new PNGSaveOptions();
pngOpts.compression = 9;
pngOpts.interlaced = false;
doc.saveAs(saveFile, pngOpts, true, Extension.LOWERCASE);
} else if (formatMode === "webp") {
// Prepare for WebP (Must be 8bit RGB, Flattened usually safer for ActionDescriptors)
if (doc.mode != DocumentMode.RGB) doc.changeMode(ChangeMode.RGB);
if (doc.bitsPerChannel != BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
var saveFile = new File(outputFolder + "/" + baseName + ".webp");
saveAsWebP(doc, saveFile);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
// Native WebP ActionDescriptor
function saveAsWebP(document, saveFile) {
var actionDescriptor = new ActionDescriptor();
var webpOptions = new ActionDescriptor();
// WebP Settings: Lossy, 75% Quality, Include Metadata
webpOptions.putEnumerated(stringIDToTypeID("compressionType"), stringIDToTypeID("compressionType"), stringIDToTypeID("lossy"));
webpOptions.putInteger(stringIDToTypeID("compression"), 75);
webpOptions.putBoolean(stringIDToTypeID("includeXMPData"), true);
webpOptions.putBoolean(stringIDToTypeID("includeEXIFData"), true);
webpOptions.putBoolean(stringIDToTypeID("includePsMapData"), true);
var saveAction = new ActionDescriptor();
saveAction.putObject(stringIDToTypeID("as"), stringIDToTypeID("WebPFormat"), webpOptions);
saveAction.putPath(stringIDToTypeID("in"), saveFile);
saveAction.putInteger(stringIDToTypeID("documentID"), document.id);
saveAction.putBoolean(stringIDToTypeID("lowerCase"), true);
executeAction(stringIDToTypeID("save"), saveAction, DialogModes.NO);
}
function isImageFile(file) {
var name = file.name.toLowerCase();
return (name.match(/\.(jpg|jpeg|png|tif|tiff|psd|bmp|webp)$/i));
}
main();