-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImages To Layers.jsx
305 lines (252 loc) · 6.31 KB
/
Images To Layers.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
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
// NAME:
// Images To Layers
// DESCRIPTION:
// Cheap alternative of "Load Files Into Stack" script that came with CS3.
// REQUIRES:
// Adobe Photoshop CS2
// Most current version always available at: https://github.com/...
// enable double-clicking from Finder/Explorer (CS2 and higher)
#target photoshop
app.bringToFront();
//
// Type definitions
//
//
// Global variables
//
var env = new Object();
env.profiling = false;
var prefs = new Object();
//
// Entry point
//
bootstrap();
//
// Processing logic
//
function main()
{
// user preferences
prefs = new Object();
prefs.srcFolder = Folder.myDocuments;
// show dialogue
if (showDialog()) {
// TODO: subfolders, extension filter
var files = prefs.srcFolder.getFiles();
loadLayers(files);
alert("Done!", "Finished", false);
}
else {
return "cancel";
}
}
function loadLayers(files)
{
if (files.length == 0) {
return;
}
var firstFile = open(files[0]);
var doc = documents.add(
firstFile.width,
firstFile.height,
firstFile.resolution,
"Untitled",
NewDocumentMode.RGB,
DocumentFill.TRANSPARENT,
1
);
copyLayers(firstFile, doc);
firstFile.close(SaveOptions.DONOTSAVECHANGES);
for (var i = 1; i < files.length; ++i) {
var file = open(files[i]);
copyLayers(file, doc);
file.close(SaveOptions.DONOTSAVECHANGES);
}
activeDocument = doc;
doc.layers[doc.layers.length - 1].remove();
doc.revealAll();
}
function copyLayers(src, dst)
{
activeDocument = src;
if ((src.layers.length == 1) && src.layers[0].isBackgroundLayer) {
var copy = src.layers[0].duplicate(dst, ElementPlacement.PLACEATBEGINNING);
activeDocument = dst;
copy.name = stripExt(src.name);
}
else {
for (var i = 0; i < src.layers.length; ++i) {
src.layers[i].duplicate(dst, ElementPlacement.PLACEATBEGINNING);
}
}
}
//
// User interface
//
function showDialog()
{
// read dialog resource
var rsrcFile = new File(env.scriptFileDirectory + "/dialog.json");
var rsrcString = loadResource(rsrcFile);
if (! rsrcString) {
return false;
}
// build dialogue
var dlg;
try {
dlg = new Window(rsrcString);
}
catch (e) {
alert("Dialog resource is corrupt! Please, redownload the script with all files.", "Error", true);
return false;
}
// destination path
dlg.funcArea.content.grpDest.txtDest.text = prefs.srcFolder.fsName;
dlg.funcArea.content.grpDest.btnDest.onClick = function() {
var newFilePath = Folder.selectDialog("Select destination folder", prefs.srcFolder);
if (newFilePath) {
prefs.srcFolder = newFilePath;
dlg.funcArea.content.grpDest.txtDest.text = newFilePath.fsName;
}
};
// buttons
dlg.funcArea.buttons.btnRun.onClick = function() {
dlg.close(1);
};
dlg.funcArea.buttons.btnCancel.onClick = function() {
dlg.close(0);
};
dlg.center();
return dlg.show();
}
//
// Bootstrapper (version support, getting additional environment settings, error handling...)
//
function bootstrap()
{
function showError(err) {
alert(err + ': on line ' + err.line, 'Script Error', true);
}
// initialisation of class methods
defineProfilerMethods();
try {
// setup the environment
env = new Object();
env.version = parseInt(app.version, 10);
if (env.version < 9) {
alert("Photoshop versions before CS2 are not supported!", "Error", true);
return "cancel";
}
env.cs3OrHigher = (env.version >= 10);
// get script's file name
if (env.cs3OrHigher) {
env.scriptFileName = $.fileName;
}
else {
try {
//throw new Error(); // doesn't provide the file name, at least in CS2
var illegal = RUNTIME_ERROR;
}
catch (e) {
env.scriptFileName = e.fileName;
}
}
env.scriptFileDirectory = (new File(env.scriptFileName)).parent;
// run the script itself
main();
}
catch(e) {
// report errors unless the user cancelled
if (e.number != 8007) showError(e);
return "cancel";
}
}
//
// Utilities
//
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 makeValidFileName(fileName, replaceSpaces)
{
var validName = fileName.replace(/^\s+|\s+$/gm, ''); // trim spaces
validName = validName.replace(/[\\\*\/\?:"\|<>]/g, ''); // remove characters not allowed in a file name
if (replaceSpaces) {
validName = validName.replace(/[ ]/g, '_'); // replace spaces with underscores, since some programs still may have troubles with them
}
return validName;
}
function stripExt(name)
{
var dotIdx = name.indexOf('.');
if (dotIdx >= 0) {
name = name.substring(0, dotIdx);
}
return name;
}
function formatString(text)
{
var args = Array.prototype.slice.call(arguments, 1);
return text.replace(/\{(\d+)\}/g, function(match, number) {
return (typeof args[number] != 'undefined') ? args[number] : match;
});
}
function loadResource(file)
{
var rsrcString;
if (! file.exists) {
alert("Resource file '" + file.name + "' for the export dialog is missing! Please, download the rest of the files that come with this script.", "Error", true);
return false;
}
try {
file.open("r");
if (file.error) throw file.error;
rsrcString = file.read();
if (file.error) throw file.error;
if (! file.close()) {
throw file.error;
}
}
catch (error) {
alert("Failed to read the resource file '" + file.name + "'!\n\nReason: " + error + "\n\nPlease, check it's available for reading and redownload it in case it became corrupted.", "Error", true);
return false;
}
return rsrcString;
}
function Profiler(enabled)
{
this.enabled = enabled;
if (this.enabled) {
this.startTime = new Date();
this.lastTime = this.startTime;
}
}
function defineProfilerMethods()
{
Profiler.prototype.getDuration = function(rememberAsLastCall, sinceLastCall)
{
if (this.enabled) {
var currentTime = new Date();
var lastTime = sinceLastCall ? this.lastTime : this.startTime;
if (rememberAsLastCall) {
this.lastTime = currentTime;
}
return new Date(currentTime.getTime() - lastTime.getTime());
}
}
Profiler.prototype.resetLastTime = function()
{
this.lastTime = new Date();
};
Profiler.prototype.format = function(duration)
{
var output = padder(duration.getUTCHours(), 2) + ":";
output += padder(duration.getUTCMinutes(), 2) + ":";
output += padder(duration.getUTCSeconds(), 2) + ".";
output += padder(duration.getUTCMilliseconds(), 3);
return output;
};
}