-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_Layer_Export.jsx
More file actions
318 lines (251 loc) · 8.65 KB
/
Copy pathSmart_Layer_Export.jsx
File metadata and controls
318 lines (251 loc) · 8.65 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#target photoshop
app.bringToFront();
if (!app.documents.length) {
alert("请先打开一个 PSD 文件。");
throw new Error("No document open");
}
var doc = app.activeDocument;
// =====================================================
// 判断是否存在选区
// 有选区:按选区范围导出
// 无选区:按整个画布导出
// =====================================================
function getExportBounds() {
try {
var b = doc.selection.bounds;
return {
hasSelection: true,
left: b[0].as("px"),
top: b[1].as("px"),
right: b[2].as("px"),
bottom: b[3].as("px")
};
} catch (e) {
return {
hasSelection: false,
left: 0,
top: 0,
right: doc.width.as("px"),
bottom: doc.height.as("px")
};
}
}
var bounds = getExportBounds();
var exportWidth = bounds.right - bounds.left;
var exportHeight = bounds.bottom - bounds.top;
if (exportWidth <= 0 || exportHeight <= 0) {
alert("导出尺寸无效,请检查选区或画布。");
throw new Error("Invalid export bounds");
}
var modeText = bounds.hasSelection ? "选区范围导出 / Selection Bounds Export" : "画布范围导出 / Canvas Export";
var isSelectionMode = bounds.hasSelection;
// =====================================================
// 选择导出文件夹
// =====================================================
var outFolder = Folder.selectDialog(
"选择 PNG 导出文件夹\n\n" +
"导出模式:" + modeText + "\n" +
"导出尺寸:" + exportWidth + " × " + exportHeight + " px"
);
if (!outFolder) {
throw new Error("No output folder selected");
}
// =====================================================
// 获取当前选中的图层 ID
// =====================================================
function getSelectedLayerIDs() {
var selected = [];
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
try {
var desc = executeActionGet(ref);
if (desc.hasKey(stringIDToTypeID("targetLayers"))) {
var list = desc.getList(stringIDToTypeID("targetLayers"));
for (var i = 0; i < list.count; i++) {
var layerIndex = list.getReference(i).getIndex();
var ref2 = new ActionReference();
ref2.putIndex(charIDToTypeID("Lyr "), layerIndex);
var desc2 = executeActionGet(ref2);
selected.push(desc2.getInteger(stringIDToTypeID("layerID")));
}
}
} catch (e) {
selected.push(doc.activeLayer.id);
}
return selected;
}
// =====================================================
// 按 ID 查找图层 / 图层组
// =====================================================
function findLayerByID(container, id) {
for (var i = 0; i < container.layers.length; i++) {
var layer = container.layers[i];
if (layer.id === id) {
return layer;
}
if (layer.typename === "LayerSet") {
var found = findLayerByID(layer, id);
if (found) return found;
}
}
return null;
}
// =====================================================
// 可见性记录 / 恢复
// =====================================================
function saveVisibility(container, map) {
for (var i = 0; i < container.layers.length; i++) {
var layer = container.layers[i];
map[layer.id] = layer.visible;
if (layer.typename === "LayerSet") {
saveVisibility(layer, map);
}
}
}
function restoreVisibility(container, map) {
for (var i = 0; i < container.layers.length; i++) {
var layer = container.layers[i];
if (map.hasOwnProperty(layer.id)) {
try {
layer.visible = map[layer.id];
} catch (e) {}
}
if (layer.typename === "LayerSet") {
restoreVisibility(layer, map);
}
}
}
function hideAll(container) {
for (var i = 0; i < container.layers.length; i++) {
var layer = container.layers[i];
try {
layer.visible = false;
} catch (e) {}
if (layer.typename === "LayerSet") {
hideAll(layer);
}
}
}
// =====================================================
// 显示目标图层及其父级
// =====================================================
function showLayerAndParents(layer) {
var current = layer;
while (current && current.typename !== "Document") {
try {
current.visible = true;
} catch (e) {}
current = current.parent;
}
}
// 如果目标是图层组,恢复组内原本可见的子图层状态
function restoreGroupChildrenVisibility(layerSet, visibilityMap) {
for (var i = 0; i < layerSet.layers.length; i++) {
var layer = layerSet.layers[i];
if (visibilityMap.hasOwnProperty(layer.id)) {
try {
layer.visible = visibilityMap[layer.id];
} catch (e) {}
}
if (layer.typename === "LayerSet") {
restoreGroupChildrenVisibility(layer, visibilityMap);
}
}
}
// =====================================================
// 文件名清理
// =====================================================
function sanitizeFileName(name) {
return name.replace(/[\\\/\:\*\?\"\<\>\|]/g, "_");
}
// =====================================================
// 防止重名覆盖:如果文件已存在,自动追加编号
// =====================================================
function getUniqueFile(folder, baseName, ext) {
var file = new File(folder.fsName + "/" + baseName + ext);
var count = 1;
while (file.exists) {
file = new File(folder.fsName + "/" + baseName + "_" + count + ext);
count++;
}
return file;
}
// =====================================================
// 保存 PNG
// =====================================================
function savePNG(targetDoc, file) {
app.activeDocument = targetDoc;
var opts = new PNGSaveOptions();
opts.compression = 9;
opts.interlaced = false;
targetDoc.saveAs(file, opts, true, Extension.LOWERCASE);
}
// =====================================================
// Selection Mode:有选区时使用临时文档裁切导出
// =====================================================
function exportBySelection(layer, fileName) {
app.activeDocument = doc;
var tempDoc = doc.duplicate();
app.activeDocument = tempDoc;
tempDoc.crop([
UnitValue(bounds.left, "px"),
UnitValue(bounds.top, "px"),
UnitValue(bounds.right, "px"),
UnitValue(bounds.bottom, "px")
]);
var outFile = getUniqueFile(outFolder, fileName, ".png");
savePNG(tempDoc, outFile);
tempDoc.close(SaveOptions.DONOTSAVECHANGES);
}
// =====================================================
// Canvas Mode:无选区时直接按当前画布快速导出
// 不复制文档,不裁切,所以速度更快
// =====================================================
function exportByCanvas(layer, fileName) {
app.activeDocument = doc;
var outFile = getUniqueFile(outFolder, fileName, ".png");
savePNG(doc, outFile);
}
// =====================================================
// 主逻辑
// =====================================================
var selectedIDs = getSelectedLayerIDs();
if (!selectedIDs.length) {
alert("请先选中至少一个图层或图层组。");
throw new Error("No layers selected");
}
var visibilityMap = {};
saveVisibility(doc, visibilityMap);
var exportedCount = 0;
try {
for (var i = 0; i < selectedIDs.length; i++) {
app.activeDocument = doc;
var layer = findLayerByID(doc, selectedIDs[i]);
if (!layer) continue;
hideAll(doc);
showLayerAndParents(layer);
if (layer.typename === "LayerSet") {
restoreGroupChildrenVisibility(layer, visibilityMap);
}
var fileName = sanitizeFileName(layer.name);
if (isSelectionMode) {
exportBySelection(layer, fileName);
} else {
exportByCanvas(layer, fileName);
}
exportedCount++;
}
app.activeDocument = doc;
alert(
"导出完成。\n\n" +
"导出模式:" + modeText + "\n" +
"导出数量:" + exportedCount + " 个 PNG\n" +
"导出尺寸:" + exportWidth + " × " + exportHeight + " px"
);
} catch (err) {
alert("导出失败:" + err.message);
} finally {
app.activeDocument = doc;
restoreVisibility(doc, visibilityMap);
}