-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractorjs.js
More file actions
381 lines (343 loc) · 10.3 KB
/
extractorjs.js
File metadata and controls
381 lines (343 loc) · 10.3 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
*
* Photoshop script to do the following:
*
* 1) Rasterize all layers
* 2) Export layer coordinates
* 3) Export layers into separate files
*
* Information is stored in JSON format
*
* Written by Anh Mai for Flipboard
*
**/
#target photoshop;
#include "utils/json2.js";
bringToFront();
///////////////////////////////////
// GLOBALS
///////////////////////////////////
var sourceFilePath = activeDocument.fullName.path + "/";
sourceFilePath = sourceFilePath.toString().match(/([^\.]+)/)[1]
var sourceFileName = activeDocument.name.match(/([^\.]+)/)[1]
var folder = Folder(sourceFilePath + sourceFileName);
// Create folder
if (!folder.exists)
folder.create();
// Crop parameters
var cropLoc =
{
"horizontal": "center", // left|center|right
"vertical": "center" // top|center|bottom
};
// Saving some settings
var originalUnits = preferences.rulerUnits;
// Fire off main function
// Suspending time (we are frozen in time!!)
var doc = activeDocument;
var visibleLayers = new Array();
var dimensions =
{
"width": doc.width.value,
"height": doc.height.value
};
var history = doc.activeHistoryState;
doc.suspendHistory("exportLayers", "main();");
doc.activeHistoryState = history;
function main()
{
// Check that we have at least one document opened
if (documents.length <= 0)
{
alert("You don't have any opened documents...");
return 'cancel';
}
preferences.rulerUnits = Units.PIXELS;
var jsonObject =
{
"crop" : cropLoc,
"dimensions": dimensions,
"foreground": new Array(),
"background": new Array()
};
// Rasterize all layers
// and crop out anything thats not
// in the actual canvas
cropAndRasterize(doc);
// Merge and flatten all linked layers
mergeLinked(doc);
getLayers(doc);
// Now we loop through each layer
// and save each one out
// and get its data
var len = visibleLayers.length;
for (var i = 0; i < len; i++)
{
var activeLayer = visibleLayers[i];
if (isEmpty(activeLayer) || !activeLayer.visible)
continue;
var jsonData = layerData(activeLayer);
// check if border
if (activeLayer.name.match(/^*Border/))
{
var fileName = activeLayer.name;
jsonObject[fileName] = new Array();
jsonData["file"] = fileName + ".png";
jsonObject[fileName].push(jsonData);
saveLayer(activeLayer, fileName);
}
else if (activeLayer.name == "important")
{
var newCrop = determineCrop(activeLayer);
jsonObject["crop"] = newCrop;
}
else if (activeLayer.name.match(/^*background*/))
{
var fileName = activeLayer.name;
jsonData["file"] = fileName + ".png";
jsonObject.background.push(jsonData);
saveLayer(activeLayer, fileName);
}
else if (activeLayer.name == "blurred")
{
var fileName = activeLayer.name;
jsonData["file"] = fileName + ".png";
jsonObject["blurred"] = jsonData;
saveLayer(activeLayer, fileName);
}
else
{
var fileName = "foreground" + i;
jsonData["file"] = fileName + ".png";
jsonObject.foreground.push(jsonData);
saveLayer(activeLayer, fileName);
}
}
// to finish, we save out the JSON object
saveJSON(jsonObject);
// Restoring original settings
preferences.rulerUnits = originalUnits;
}
function getLayers(parent)
{
// creates an array of all visible layers
// pull them out of their groups
var len = parent.layers.length;
for (var i = 0; i < len; i++)
{
var layer = parent.layers[i];
if (layer.visible)
{
if (layer.typename == "LayerSet")
{
getLayers(layer);
}
else
{
visibleLayers.push(layer);
}
}
}
}
function contains(item, arr)
{
var len = arr.length;
for (var i = 0; i < len; i++)
{
if (arr[i] === item)
return true;
}
return false;
}
// the ugliest function in the world
// used to merge linked layers into a single layer
// lots of work around to bypass javascript's
// inherent asynch
function mergeLinked(parent)
{
var set = []
var len = parent.layers.length;
var i = 0;
while (i < len)
{
var layer = parent.layers[i];
if (layer.typename == "LayerSet" && layer.visible)
{
mergeLinked(layer)
}
else if (layer.linkedLayers.length > 0)
{
if (!contains(layer, set))
{
var newSet = parent.layerSets.add();
for (var n = 0, nlen = layer.linkedLayers.length; n < nlen + 1; n++)
{
if (n == nlen)
{
layer.move(newSet, ElementPlacement.INSIDE);
newSet.merge();
}
else
{
if (contains(layer.linkedLayers[n], set))
{
newSet.remove();
break;
}
set.push(layer);
layer.linkedLayers[n].move(newSet, ElementPlacement.INSIDE);
}
}
}
}
len = parent.layers.length;
i = i + 1;
}
}
function cropAndRasterize(docu)
{
var len = docu.layers.length;
for (var i = 0; i < len; i++)
{
var layer = docu.layers[i];
// Rasterize the layer first
if (layer.visible)
{
if (layer.typename == "LayerSet")
{
cropAndRasterize(layer);
}
else
{
doc.activeLayer = layer;
doc.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
doc.crop(new Array(0,0,doc.width.value,doc.height.value));
}
}
}
}
// grabs all of the data for a layer and
// put them inside an object literal
function layerData(lay)
{
var bounds = lay.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
var fromLeft = bounds[0].value;
var fromTop = bounds[1].value;
var fromRight = doc.width.value - bounds[2].value;
var fromBottom = doc.height.value - bounds[3].value;
var snap = snapLocation(width, height, fromLeft, fromTop, fromRight, fromBottom);
var dataObject = {
"snap" : snap,
"width": width,
"height": height,
"fromLeft": fromLeft,
"fromTop": fromTop,
"fromRight": fromRight,
"fromBottom": fromBottom
};
if (lay.name.match(/^*background*/))
{
dataObject["ratio"] = (width / height);
}
return dataObject;
}
function determineCrop(lay)
{
var bounds = lay.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
var fromLeft = bounds[0].value;
var fromTop = bounds[1].value;
var fromRight = doc.width.value - bounds[2].value;
var fromBottom = doc.height.value - bounds[3].value;
var leftRight = Math.abs(fromRight - fromLeft);
var topBottom = Math.abs(fromBottom - fromTop);
var rowMiddle = (leftRight < (width / 5));
var colMiddle = (topBottom < (height / 5));
var cropObj = {}
var topBottomCenter = (height / 2) + fromTop;
var leftRightCenter = (width / 2) + fromLeft;
var fromLeftPercent = (leftRightCenter / dimensions.width * 100) + "%";
var fromTopPercent = (topBottomCenter / dimensions.height * 100) + "%";
cropObj["horizontal"] = fromLeftPercent;
cropObj["vertical"] = fromTopPercent;
return cropObj;
}
// determines the snap location
// based on input coordinates of the item
function snapLocation(width, height, left, top, right, bottom)
{
var leftRight = Math.abs(right - left);
var topBottom = Math.abs(bottom - top);
var rowMiddle = (leftRight <= width / 2);
var colMiddle = (topBottom <= height / 2);
if (rowMiddle && colMiddle)
return "snapToMiddle";
if (rowMiddle)
return "snapHorizontalMiddle";
if (colMiddle)
return "snapVerticalMiddle";
return "snapToCorners";
}
// creates a new document after selecting
// the appropriate layer, then save that
// new document as a separate PNG
// then close the new document
function saveLayer(lay, fileName)
{
// save dialog settings
// suppress all dialogs for this function
var originalDialog = displayDialogs;
displayDialogs = DialogModes.NO;
// makes sure we switch to the document
activeDocument = doc;
doc.activeLayer = lay;
var left = lay.bounds[0];
var top = lay.bounds[1];
var right = lay.bounds[2];
var bottom = lay.bounds[3];
var layWidth = right - left;
var layHeight = bottom - top;
doc.selection.select(Array(
Array(left, top),
Array(right, top),
Array(right, bottom),
Array(left, bottom)
));
doc.selection.copy();
// creates new document and switch to it
var newDoc = documents.add(layWidth, layHeight, 144, "tempDoc", NewDocumentMode.RGB);
newDoc.paste();
newDoc.backgroundLayer.remove();
var file = new File(folder + "/" + fileName + ".png");
saveForWeb(newDoc, file);
newDoc.close(SaveOptions.DONOTSAVECHANGES);
}
// the actual saving function
// saves with optimization for web
// NOTE: this assumes you only have
// one layer in the document!
function saveForWeb(doc, path)
{
var saveOptions = new ExportOptionsSaveForWeb;
saveOptions.format = SaveDocumentType.PNG;
saveOptions.PNG8 = false;
saveOptions.quality = 100;
doc.exportDocument(path, ExportType.SAVEFORWEB, saveOptions);
}
// saving our JSON object
function saveJSON(json)
{
var file = new File(folder + "/info.json");
file.open('w');
file.writeln(JSON.stringify(json, null, "\t"));
file.close();
}
// test if layer is empty
function isEmpty(lay)
{
var bound = lay.bounds;
return (bound[0].value == 0) && (bound[1].value == 0) && (bound[2].value == 0) && (bound[3].value == 0);
}