-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathphotoshopCompositionComposer-v0.1.jsx
154 lines (123 loc) · 4.57 KB
/
photoshopCompositionComposer-v0.1.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
var userDisplayDialogsPref = app.displayDialogs;
app.displayDialogs = DialogModes.ALL;
var savePath;
app.displayDialogs = DialogModes.NO;
function getType(thing){
if(thing === null) return "[object Null]"; // special case
return Object.prototype.toString.call(thing);
}
function getCombinations(arr, n) {
if (n == 1) {
var ret = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
ret.push([arr[i][j]]);
}
}
return ret;
} else {
var ret = [];
for (var i = 0; i < arr.length; i++) {
var elem = arr.shift();
for (var j = 0; j < elem.length; j++) {
var childperm = getCombinations(arr.slice(), n - 1);
for (var k = 0; k < childperm.length; k++) {
ret.push([elem[j]].concat(childperm[k]));
}
}
}
return ret;
}
}
function showAllArtLayers() {
for(var i = 0; i < layerSets.length; i++) {
for(var z = 0; z < layerSets[i].artLayers.length; z++) {
layerSets[i].artLayers[z].visible = true;
}
}
}
function hideAllArtLayers() {
var layerSets = app.activeDocument.layerSets;
for(var i = 0; i < layerSets.length; i++) {
if(layerSets[i].artLayers.length) {
for(var z = 0; z < layerSets[i].artLayers.length; z++) {
layerSets[i].artLayers[z].visible = false;
}
} else {
for(var z = 0; z < layerSets[i].layerSets.length; z++) {
layerSets[i].layerSets[z].visible = false;
}
}
}
}
function getArtLayerCollectionCollection() {
var layerSets = app.activeDocument.layerSets,
artLayerCollectionCollection = [];
for(var i = 0; i < layerSets.length; i++) {
var artlayerCollection = [];
if(layerSets[i].artLayers.length) {
for(var z = 0; z < layerSets[i].artLayers.length; z++) {
if(layerSets[i].name.indexOf('__') !== 0)
artlayerCollection.push(layerSets[i].artLayers[z]);
}
} else {
for(var z = 0; z < layerSets[i].layerSets.length; z++) {
if(layerSets[i].name.indexOf('__') !== 0)
artlayerCollection.push(layerSets[i].layerSets[z]);
}
}
artLayerCollectionCollection.push(artlayerCollection);
}
return artLayerCollectionCollection;
}
function combine() {
var artLayerCollectionCollection = getArtLayerCollectionCollection(),
artLayerCollectionCollectionCombinations = getCombinations(artLayerCollectionCollection, getLayerSetsCount()),
continueConfirmation;
if(! artLayerCollectionCollectionCombinations.length) return alert('Script has aborted. No combinations found. Please make sure no empty groups are present.');
continueConfirmation = confirm(artLayerCollectionCollectionCombinations.length + " combinations found. Would you like to continue?");
if(! continueConfirmation ) return alert('Script has been aborted.');
savePath = Folder.selectDialog("Select an output folder");
var includePSDFiles = confirm('Would you like to include corresponding PSD documents?')
for(var i = 0; i < artLayerCollectionCollectionCombinations.length; i++) {
hideAllArtLayers();
var artLayerNames = [];
for(var z = 0; z < artLayerCollectionCollectionCombinations[i].length; z++) {
var artLayer = artLayerCollectionCollectionCombinations[i][z];
artLayer.visible = true;
artLayerNames.push(artLayer.parent.name);
artLayerNames.push(artLayer.name);
}
saveDocumentAsPNG(savePath + '/' + normalizeSaveFileName(artLayerNames.join('')).substr(0, 254));
if(includePSDFiles) saveDocumentAsPSD(savePath + '/' + normalizeSaveFileName(artLayer.parent.name + artLayerNames.join('')).substr(0, 254));
}
}
function getSmallestLayerSetCount() {
var count = null,
layerSets = app.activeDocument.layerSets;
for(var i = 0; i < layerSets.length; i++) {
var artLayers = layerSets[i].artLayers;
if(count === null) count = artLayers.length;
if(artLayers.length < count) count = artLayers.length;
}
return 1;
}
function getLayerSetsCount() {
var layerSets = app.activeDocument.layerSets,
count = 0;
for(var i = 0; i < layerSets.length; i++) {
if(layerSets[i].name.indexOf('__') !== 0) count++;
}
return count;
}
function normalizeSaveFileName(name) {
return name;
}
function saveDocumentAsPNG(path) {
app.activeDocument.saveAs(new File(path), new PNGSaveOptions());
}
function saveDocumentAsPSD(path) {
app.activeDocument.saveAs(new File(path), new PhotoshopSaveOptions());
}
combine();
app.displayDialogs = userDisplayDialogsPref;