-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
254 lines (200 loc) · 8.1 KB
/
script.js
File metadata and controls
254 lines (200 loc) · 8.1 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
const batchId = "6";
const modelsPath = "S:\\work\\armor\\Skins\\all";
const mobilesPath = "S:\\work\\armor\\Skins\\mobiles\\Batch " + batchId;
const outPath = "S:\\work\\armor\\Skins\\out\\Batch " + batchId;
const transparentFileName = "sv".toLowerCase();
const transparentLayerName = "transparant";
const maskLayerName = "mask";
const photoLayerName = "photo";
const delimiter = "-";
const modelsDirectory = new Folder(modelsPath);
const mobilesDirectory = new Folder(mobilesPath);
const outputDirectory = new Folder(outPath);
if (checker()){
var totalTime = new TimeIt();
main();
alert(totalTime.getElapsed() + "Hours");
}
function main() {
const mobileList = mobilesDirectory.getFiles();
const modelsList = modelsDirectory.getFiles();
for (var i = 0; i < mobileList.length; ++i) {
if (mobileList[i] instanceof File && mobileList[i].hidden === false) {
var docRef = open(mobileList[i]);
var docName = docRef.name.replace(/\.[^.]+$/, '');
var f = new Folder(outputDirectory + "/" + docName);
if (!f.exists) {
f.create();
} else {
alert("Error + " + docName + " Exists");
continue;
}
for (var j = 0; j < modelsList.length; ++j) {
if (modelsList[j] instanceof File && modelsList[j].hidden === false) {
var transparentLayer = docRef.artLayers.getByName(transparentLayerName);
var maskLayer = docRef.artLayers.getByName(maskLayerName);
var photoLayer = docRef.artLayers.getByName(photoLayerName);
docRef.activeLayer = maskLayer;
var skinName = placeImage(modelsList[j]);
var saveName = docName + delimiter + skinName;
var transparent = skinName.toLowerCase().indexOf(transparentFileName) !== -1;
playAction('MobileSkins', 'CenterImage');
playAction('MobileSkins', 'CreateClippingMask');
// start transparent
if (transparent) {
maskLayer.opacity = 60;
}
save(docName, saveName + delimiter + "v");
photoLayer.visible = false;
playAction('MobileSkins', 'hideMaskLayerStyle');
save(docName, saveName + delimiter + "h");
photoLayer.visible = true;
playAction('MobileSkins', 'showMaskLayerStyle');
// end transparent
if (transparent) {
maskLayer.opacity = 100;
transparentLayer.visible = false;
}
docRef.layers[0].remove();
}
}
docRef.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
function save(internalFolder, name){
ExportJPG(internalFolder, name);
}
function checker() {
if (mobilesDirectory === null || modelsDirectory === null || outputDirectory === null) return false;
const mobileList = mobilesDirectory.getFiles();
for (var i = 0; i < mobileList.length; ++i) {
if (mobileList[i] instanceof File && mobileList[i].hidden === false) {
var docRef = open(mobileList[i]);
var docName = docRef.name.replace(/\.[^.]+$/, '');
try {
docRef.artLayers.getByName(transparentLayerName);
docRef.artLayers.getByName(maskLayerName);
docRef.artLayers.getByName(photoLayerName);
} catch (e) {
alert("Please Check layer names in document " + docName);
return false;
}
docRef.close(SaveOptions.DONOTSAVECHANGES);
}
}
return true;
}
function fitCurrentLayerToMobileSize(){
var doc = app.activeDocument;
var layer = doc.activeLayer;
// do nothing if layer is background or locked
if (layer.isBackgroundLayer || layer.allLocked || layer.pixelsLocked
|| layer.positionLocked || layer.transparentPixelsLocked) return;
// do nothing if layer is not normal artLayer or Smart Object
if (layer.kind !== LayerKind.NORMAL && layer.kind !== LayerKind.SMARTOBJECT) return;
// store the ruler
var defaultRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var bounds = app.activeDocument.activeLayer.bounds;
var layerWidth = bounds[2].as('px') - bounds[0].as('px');
var layerHeight = bounds[3].as('px') - bounds[1].as('px');
newHeight = 720; // here goes the required skin height
resizePercent = newHeight / layerHeight * 100;
app.activeDocument.activeLayer.resize(resizePercent, resizePercent, AnchorPosition.TOPLEFT);
app.preferences.rulerUnits = defaultRulerUnits;
return layer.name;
}
function placeImage(sourceFile) {
const idPlc = charIDToTypeID("Plc ");
const desc3 = new ActionDescriptor();
const idnull = charIDToTypeID("null");
desc3.putPath(idnull, sourceFile);
const idFTcs = charIDToTypeID("FTcs");
const idQCSt = charIDToTypeID("QCSt");
const idQcsa = charIDToTypeID("Qcsa");
desc3.putEnumerated(idFTcs, idQCSt, idQcsa);
executeAction(idPlc, desc3, DialogModes.NO);
return fitCurrentLayerToMobileSize();
}
function playAction(actionSet, actionName) {
const idPly = charIDToTypeID("Ply ");
const desc2 = new ActionDescriptor();
const idnull = charIDToTypeID("null");
const ref1 = new ActionReference();
const idActn = charIDToTypeID("Actn");
ref1.putName(idActn, actionName);
const idASet = charIDToTypeID("ASet");
ref1.putName(idASet, actionSet);
desc2.putReference(idnull, ref1);
executeAction(idPly, desc2, DialogModes.NO);
}
function TimeIt() {
this.startTime = new Date();
this.endTime = new Date();
this.start = function () {
this.startTime = new Date();
}
this.stop = function () {
this.endTime = new Date();
}
this.getTime = function () {
return ((this.endTime.getTime() - this.startTime.getTime()) / (1000*60*60));
}
this.getElapsed = function () {
this.endTime = new Date(); return this.getTime();
}
}
function ExportJPG(internalFolder, name){
var i;
// Confirm the document has already been saved and so has a path to use
try {
app.activeDocument.save()
} catch (e) {
alert("Could not export JPG as the document is not saved.\nPlease save and try again.");
return
}
// Store the active doc handle in variable
const originalDoc = app.activeDocument;
// Check there is at least 1 visible layer.
var foundVisible = false;
for (i = 0; i < originalDoc.layers.length; i++) {
if (originalDoc.layers[i].visible) {
foundVisible = true;
break
}
}
if (!foundVisible) {
alert("No visible layers found. JPG export failed.");
return
}
// Duplicate. We'll save the duplicate as a .png and close it.
const newDoc = originalDoc.duplicate();
// Photoshop must have a visible layer selected to merge visible layers, so we ensure there is one selected.
newDoc.activeLayer = newDoc.artLayers.add();
newDoc.activeLayer.visible = true;
// Merge the layers.
newDoc.mergeVisibleLayers();
// Remove all empty layers.
for (i = newDoc.layers.length - 1; i >= 0; i--) {
if (!newDoc.layers[i].visible) {
newDoc.layers[i].remove()
}
}
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
jpgOptions.embedColorProfile = true;
jpgOptions.formatOptions = FormatOptions.PROGRESSIVE;
if(jpgOptions.formatOptions == FormatOptions.PROGRESSIVE){
jpgOptions.scans = 5;
}
jpgOptions.matte = MatteType.NONE;
// Set up destination path.
savePath = File(outputDirectory + "/" + internalFolder + "/" + name + ".jpg");
// Save!
newDoc.saveAs(savePath, jpgOptions, false, Extension.LOWERCASE);
// Close the duplicate.
newDoc.close();
// Just in case, make sure the active document is the orignal one.
app.activeDocument = originalDoc
}