-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSaveLayers.jsx
199 lines (165 loc) · 5.01 KB
/
SaveLayers.jsx
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
// NAME:
// SaveLayers
// DESCRIPTION:
// Saves each layer in the active document to a PNG or JPG file named after the layer.
// These files will be created in the current document folder.
// REQUIRES:
// Adobe Photoshop CS2 or higher
// VERSIONS:
// 27 March 2013 by Robin Parmar (robin@robinparmar.com)
// preferences stored in object
// auto-increment file names to prevent collisions
// properly handles layer groups
// header added
// code comments added
// main() now has error catcher
// counts number of layers
// many little code improvements
// 26 Sept 2012 by Johannes on stackexchange
// original version
// enable double-clicking from Finder/Explorer (CS2 and higher)
#target photoshop
app.bringToFront();
function main() {
// two quick checks
if(!okDocument()) {
alert("Document must be a layered PSD.");
return;
}
var ok = confirm("This document contains " + activeDocument.layers.length + " top level layers.\nBe aware that large numbers of layers will take some time!\nContinue?");
if(!ok) return
// user preferences
prefs = new Object();
prefs.fileType = "";
prefs.fileQuality = 0;
prefs.filePath = app.activeDocument.path;
prefs.count = 0;
//instantiate dialogue
Dialog();
saveLayers(activeDocument);
alert("Saved " + prefs.count + " files.");
}
function saveLayers(ref) {
var len = ref.layers.length;
// rename layers top to bottom
for (var i = 0; i < len; i++) {
var layer = ref.layers[i];
if (layer.typename == 'LayerSet') {
// recurse if current layer is a group
saveLayers(layer);
} else {
// otherwise make sure the layer is visible and save it
for(var j = 0 ; j < len ; j++ ) {
if( ref.layers[j] == layer ) {
layer.visible = true;
} else {
layer.visible = false;
}
}
saveImage(layer.name);
}
}
}
function saveImage(layerName) {
var handle = getUniqueName(prefs.filePath + "/" + layerName);
prefs.count++;
if(prefs.fileType=="PNG") {
SavePNG(handle);
} else {
SaveJPEG(handle);
}
}
function getUniqueName(fileroot) {
// form a full file name
// if the file name exists, a numeric suffix will be added to disambiguate
var filename = fileroot;
for (var i=1; i<100; i++) {
var handle = File(filename + "." + prefs.fileType);
if(handle.exists) {
filename = fileroot + "-" + padder(i, 3);
} else {
return handle;
}
}
}
function padder(input, padLength) {
// pad the input with zeroes up to indicated length
var result = (new Array(padLength + 1 - input.toString().length)).join('0') + input;
return result;
}
function SavePNG(saveFile) {
pngSaveOptions = new PNGSaveOptions();
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
function SaveJPEG(saveFile) {
jpegSaveOptions = new JPEGSaveOptions();
jpegSaveOptions.quality = selectedQuality;
activeDocument.saveAs(saveFile, jpegSaveOptions, true, Extension.LOWERCASE);
}
function Dialog() {
// build dialogue
var dlg = new Window ('dialog', 'Select type');
dlg.saver = dlg.add("dropdownlist", undefined, "");
dlg.quality = dlg.add("dropdownlist", undefined, "");
// file type
var saveOpt = [];
saveOpt[0] = "PNG";
saveOpt[1] = "JPG";
for (var i=0, len=saveOpt.length; i<len; i++) {
dlg.saver.add ("item", "Save to " + saveOpt[i]);
};
// trigger function
dlg.saver.onChange = function() {
prefs.fileType = saveOpt[parseInt(this.selection)];
// turn on additional option for JPG
if(prefs.fileType==saveOpt[1]){
dlg.quality.show();
} else {
dlg.quality.hide();
}
};
// jpg quality
var qualityOpt = [];
for(var i=12; i>=1; i--) {
qualityOpt[i] = i;
dlg.quality.add ('item', "" + i);
};
// trigger function
dlg.quality.onChange = function() {
prefs.fileQuality = qualityOpt[12-parseInt(this.selection)];
};
// remainder of UI
var uiButtonRun = "Continue";
dlg.btnRun = dlg.add("button", undefined, uiButtonRun );
dlg.btnRun.onClick = function() {
this.parent.close(0); };
dlg.orientation = 'column';
dlg.saver.selection = dlg.saver.items[0] ;
dlg.quality.selection = dlg.quality.items[0] ;
dlg.center();
dlg.show();
}
function okDocument() {
// check that we have a valid document
if (!documents.length) return false;
var thisDoc = app.activeDocument;
var fileExt = decodeURI(thisDoc.name).replace(/^.*\./,'');
return fileExt.toLowerCase() == 'psd'
}
function wrapper() {
function showError(err) {
alert(err + ': on line ' + err.line, 'Script Error', true);
}
try {
// suspend history for CS3 or higher
if (parseInt(version, 10) >= 10) {
activeDocument.suspendHistory('Save Layers', 'main()');
} else {
main();
}
} catch(e) {
// report errors unless the user cancelled
if (e.number != 8007) showError(e);
}
}
wrapper();