-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrandChange.js
476 lines (371 loc) · 17.3 KB
/
BrandChange.js
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//GLOBALS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var allBrands = ['Westpac', 'Bank of Melbourne', 'Bank SA', 'St.George', 'Westpac Group', 'BT'];
var assetsRoot = {
'Bank of Melbourne': '05-BOM-Assets',
'Bank SA': '06-BSA-Assets',
'BT': '08-BT-Assets',
'St.George': '04-STG-Assets',
'Westpac': '03-WBC-Assets',
'Westpac Group': '07-WBG-Assets',
};
var UIRoot = {
'Bank of Melbourne': '03-BOM',
'Bank SA': '04-BSA',
'BT': '06-BT',
'St.George': '02-STG',
'Westpac': '01-WBC',
'Westpac Group': '05-WBG',
};
var brandID = {
'Bank of Melbourne': 'BOM',
'Bank SA': 'BSA',
'BT': 'BT',
'St.George': 'STG',
'Westpac': 'WBC',
'Westpac Group': 'WBG',
};
var REPORT = {
'assets': {
'changed': 0,
'failed': [],
},
'swatches': {
'changed': 0,
'failed': 0,
},
'styles': {
'changed': 0,
'failed': 0,
},
}
/**
* Try to guess current brand and store in properties
*
* @type {Object}
*/
var CurrentBrand = {
/**
* The place we store the current brand globally
*
* @type {String}
*/
ThisBrand: '',
/*
* GetCurrentBrand
*
* @param assetsRoot {array} All brand root folder strings
*/
get: function( assetsRoot ) {
var links = app.activeDocument.links; //get all asset links from the doc
var currentBrand = ''; //the brand to be determined
if( links.length > 0 ) { //if there are assets on the page
var path = links[0].filePath; //just look at each link one to sniff out what brand we are in.
for(var property in assetsRoot) {
if( assetsRoot.hasOwnProperty( property ) ) {
var _hasString = path.indexOf( ':' + assetsRoot[ property ] );
if( _hasString > 1 ) {
currentBrand = property;
}
}
}
}
this.ThisBrand = currentBrand; //setting globally
},
};
CurrentBrand.get( assetsRoot ); //try to guess current brand
//show dialog
var brandSelector = ShowMainDialog( CurrentBrand, assetsRoot, '', null );
var importDlg = brandSelector.show();
//Getting current brand ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(importDlg == true) { //if dialog has been clicked
var thisBrand = '';
var brand = '';
for(var brandName in allBrands) { //checking what we selected for current brand
var _active = brandSelector.currentBrands[ 'currentBrandRadios' + allBrands[ brandName ] ].value;
if(_active == true) {
thisBrand = allBrands[ brandName ];
}
}
CurrentBrand.ThisBrand = currentBrand = thisBrand; //set the current brand globally
for(var brandName in allBrands) { //why do I have to do this? Please give me the value of the radio button selection
if( allBrands[ brandName ] != currentBrand ) {
var _active = brandSelector.brands[ 'brandRadios' + allBrands[ brandName ] ].value;
if(_active == true) {
brand = allBrands[ brandName ];
}
}
}
if( brand == '' ) {
alert('Please choose a brand. We can\'t do any work if you don\'t choose one... :(');
}
else { //if dialog had a brand chosen
//Select Starter Pack folder ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var starterPackFolder = Folder.selectDialog("Please select the folder of the GUI-Starter-Pack");
if( starterPackFolder === null ) {
alert('We couldn\'t find the folder. Please try again.');
}
else {
//Check Starter Pack ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
starterPack = starterPackFolder + ''; //convert to string, man I miss ES7!
var newPath = starterPack + '/' + assetsRoot[ brand ] + '/';
var checkAsset = File( newPath + '03-Symbols/logo-phone.ai' ); //check one asset
var checkSwatch = File( newPath + '01-Colour-Swatches/swatches.ase' ); //check swatches
var checkKit = File( starterPack + '/01-Project-Templates/02-UI-Kits/' + UIRoot[ brand ] + '-UI-Kit.indt' ); //check UI-Kits
if( !checkAsset.exists || !checkSwatch.exists || !checkKit.exists ) {
alert('There is something wrong with your Starter Pack folder.\n' +
'It seems\n' +
(checkAsset.exists ? '' : '- some asssets are missing\n') +
(checkSwatch.exists ? '' : '- the swatches are missing\n') +
(checkKit.exists ? '' : '- the UI-Kit file is missing\n') + '\n' +
'Please download the latest here: http://gel.westpacgroup.com.au/downloads/visual-design/gui-starter-pack.zip'
);
}
else {
//Show progress bar ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
progressBar = new Window('window', 'Progress');
with(progressBar) {
progressBar.myProgressBar = add('progressbar', [0, 0, 400, 24], 0, 100);
}
progressBar.show(); //open progress bard window
var location = progressBar.location + ''; //update the progressbar position
location = location.split(',');
progressBar.location = location[0] + ',' + ( location[1] - 250 );
progressBar.visible = true;
progressBar.myProgressBar.value = 25; //update progress bar to give feedback that we are already pretty deep in the process (25% deep)
//Ask for Starter Pack location ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var secondDialog = ShowMainDialog( CurrentBrand, assetsRoot, brand, starterPack );
var secondDialogReturn = secondDialog.show(); //show dialog again this time with the options filled in
if(secondDialogReturn == true) { //second dialog has not been canceled
//Step 1 relink the assets /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var links = app.activeDocument.links; //get all asset links from the doc
for(j = links.length - 1; j >= 0; j--) {
var oldPath = links[j].filePath; //old path of the asset
var newFile = oldPath.split( assetsRoot[ currentBrand ] + ':' ); //split on Starter pack folder name
if( newFile.length > 1 ) { //only look for assets that are linked to the Starter Pack
newFile = newFile[1].split(':'); //take bit after starter pack path and replace : with /
file = newFile[ (newFile.length - 1) ];
newFile = newPath + newFile.join('/'); //build path
newFile = File( newFile ); //touch file to see if exists
if(newFile.exists) {
REPORT.assets.changed ++;
links[j].relink(newFile);
links[j].update();
}
else {
REPORT.assets.failed.push(file);
continue;
}
}
var step = 25 + ( ( 25 / links.length ) * j );
progressBar.myProgressBar.value = step; //update progress bar
}
//Step 2 load the swatches /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var newSwatch = File( newPath + '01-Colour-Swatches/swatches.ase' ); //load new swatches for new brand
if(newSwatch.exists) {
app.activeDocument.loadSwatches( newSwatch );
}
else {
//
}
progressBar.myProgressBar.value = 55; //update progress bar
//Step 3 switch the swatches ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var oldSwatches = app.activeDocument.swatches;
//delete and replace duplicates
for(var i = 0; i < oldSwatches.count(); i++) {
if( oldSwatches[i].parentColorGroup.name.slice( 0, brandID[ currentBrand ].length ) == brandID[ currentBrand ] ) {
try {
var name = oldSwatches[i].name + ' copy';
oldSwatches[i].remove( name );
i--;
REPORT.swatches.changed ++;
}
catch( error ) {
REPORT.swatches.failed ++;
}
}
var step = 55 + ( ( 15 / oldSwatches.count() ) * i );
progressBar.myProgressBar.value = step; //update progress bar
};
//delete all old swatches
for(var i = 0; i < oldSwatches.count(); i++) {
if( oldSwatches[i].parentColorGroup.name.slice( 0, brandID[ currentBrand ].length ) == brandID[ currentBrand ] ) {
try {
oldSwatches[i].remove();
i--;
REPORT.swatches.failed --;
}
catch( error ) {
//already captured
}
}
var step = 70 + ( ( 15 / oldSwatches.count() ) * i );
progressBar.myProgressBar.value = step; //update progress bar
};
//rename copied swatches
for(var i = 0; i < oldSwatches.count(); i++) {
if( oldSwatches[i].parentColorGroup.name.slice( 0, brandID[ brand ].length ) == brandID[ brand ] ) {
try {
newName = oldSwatches[i].name.replace( ' copy', '' );
oldSwatches[i].name = newName;
REPORT.swatches.changed ++;
}
catch( error ) {
REPORT.swatches.failed ++;
}
}
var step = 70 + ( ( 15 / oldSwatches.count() ) * i );
progressBar.myProgressBar.value = step; //update progress bar
};
//delete empty groups
var colorGroups = app.activeDocument.colorGroups;
for(var i = 0; i < colorGroups.length; i++) {
if( colorGroups[i].name.slice( 0, brandID[ currentBrand ].length ) === brandID[ currentBrand ] ) {
if( colorGroups[i].colorGroupSwatches.length === 0 ) {
colorGroups[i].remove();
i--;
REPORT.swatches.changed ++;
}
}
}
//Step 4 load styles ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var newStyles = File( starterPack + '/' + '01-Project-Templates/02-UI-Kits/' + UIRoot[ brand ] + '-UI-Kit.indt' ); //touch file to see if exists
if(newStyles.exists) {
app.activeDocument.importStyles(ImportFormat.TEXT_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 1)); //update progress bar
app.activeDocument.importStyles(ImportFormat.CHARACTER_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 2)); //update progress bar
app.activeDocument.importStyles(ImportFormat.PARAGRAPH_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 3)); //update progress bar
app.activeDocument.importStyles(ImportFormat.TOC_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 4)); //update progress bar
app.activeDocument.importStyles(ImportFormat.OBJECT_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 5)); //update progress bar
app.activeDocument.importStyles(ImportFormat.STROKE_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 6)); //update progress bar
app.activeDocument.importStyles(ImportFormat.TABLE_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 7)); //update progress bar
app.activeDocument.importStyles(ImportFormat.CELL_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 8)); //update progress bar
app.activeDocument.importStyles(ImportFormat.TABLE_AND_CELL_STYLES_FORMAT, newStyles, GlobalClashResolutionStrategy.LOAD_ALL_WITH_OVERWRITE);
progressBar.myProgressBar.value = (85 + (1.666666 * 9)); //update progress bar
REPORT.styles.changed += 9;
}
else {
REPORT.styles.failed -= 9;
}
progressBar.myProgressBar.value = 100; //update progress bar
progressBar.hide(); //close progress bar window
//Step 5 show success message //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
successMsg = new Window('dialog', 'Done!');
successMsg.alignChildren = 'left';
with(successMsg) {
successMsg.successMessage = add('statictext', undefined, 'We changed your brand from ' + currentBrand + ' to ' + brand );
successMsg.successMessage = add('statictext', undefined, 'Assets changed:\n' + REPORT.assets.changed, { multiline: true });
if( REPORT.assets.failed.length ) {
successMsg.successMessage = add('statictext', undefined, 'Assets failed:\n' + REPORT.assets.failed.join('\n'), { multiline: true });
}
successMsg.successMessage = add('statictext', undefined, 'Swatches changed:\n' + REPORT.swatches.changed, { multiline: true });
if( REPORT.swatches.failed.length ) {
successMsg.successMessage = add('statictext', undefined, 'Swatches failed:\n' + REPORT.swatches.failed, { multiline: true });
}
successMsg.successMessage = add('statictext', undefined, 'Styles changed:\n' + REPORT.styles.changed, { multiline: true });
if( REPORT.styles.failed.length ) {
successMsg.successMessage = add('statictext', undefined, 'Styles failed:\n' + REPORT.styles.failed, { multiline: true });
}
successMsg.buttonGrp = add('group');
successMsg.buttonGrp.orientation = 'row';
with(successMsg.buttonGrp) {
successMsg.buttonGrp.btnOK = add('button', undefined, 'OK');
}
}
successMsg.show();
}
}
}
}
}
//Helper methods ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* ShowMainDialog
*
* @param CurrentBrand {object} The current brand from our global var
* @param assetsRoot {array} All brand root folder strings
* @param brand {string} Brand to be selected
* @param SPpath {string} Path to Stater Pack
*/
function ShowMainDialog( CurrentBrand, assetsRoot, brand, SPpath ) {
var brandSelector = new Window('dialog', 'BRAND SWITCHER');
var currentBrand = CurrentBrand.ThisBrand;
with(brandSelector) {
brandSelector.srcFileTxt = add('statictext', undefined, '_________________________ SWITCH THE BRAND _______________________');
brandSelector.srcFileTxt.alignment = 'center';
brandSelector.srcFileTxt = add('statictext', undefined, 'Like switching Vegemite to Marmite');
brandSelector.srcFileTxt.alignment = 'center';
brandSelector.srcFileTxt = add('statictext', undefined, '');
brandSelector.alignChildren = 'left';
brandSelector.currentBrands = add('group');
brandSelector.currentBrands.orientation = 'column';
brandSelector.currentBrands.alignment = 'fill';
brandSelector.currentBrands.alignChildren = 'left';
brandSelector.currentBrands.srcFileTxt = add('statictext', undefined, '1. Please select the brand this document is in.');
if( brand === '' || CurrentBrand.ThisBrand === '' ) {
for(var property in assetsRoot) {
if( assetsRoot.hasOwnProperty( property ) ) {
brandSelector.currentBrands[ ('currentBrandRadios' + property) ] = add('radioButton', undefined, property, { name: property });
brandSelector.currentBrands[ ('currentBrandRadios' + property) ].alignment = 'fill';
if( property === currentBrand ) {
brandSelector.currentBrands[ ('currentBrandRadios' + property) ].value = true;
}
}
}
}
else {
brandSelector.currentBrands.srcFileTxt = add('statictext', undefined, currentBrand);
}
brandSelector.srcFileTxt = add('statictext', undefined, '');
brandSelector.brands = add('group');
brandSelector.brands.orientation = 'column';
brandSelector.brands.alignment = 'fill';
brandSelector.brands.alignChildren = 'left';
with(brandSelector.brands) {
brandSelector.brands.srcFileTxt = add('statictext', undefined, '2. Select a brand');
if( brand !== '' ) {
brandSelector.brands.srcFileTxt = add('statictext', undefined, brand);
}
else {
for(var property in assetsRoot) {
if( assetsRoot.hasOwnProperty( property ) ) {
brandSelector.brands[ ('brandRadios' + property) ] = add('radioButton', undefined, property, { name: property });
}
}
}
}
brandSelector.srcFileTxt = add('statictext', undefined, '');
brandSelector.buttonGrp = add('group');
brandSelector.buttonGrp.orientation = 'row';
brandSelector.buttonGrp.orientation = 'column';
brandSelector.buttonGrp.alignment = 'fill';
brandSelector.buttonGrp.alignChildren = 'left';
with(brandSelector.buttonGrp) {
brandSelector.buttonGrp.srcFileTxt = add('statictext', undefined, '3. Locate your GUI starter pack folder');
if( SPpath == null ) {
brandSelector.buttonGrp.btnOK = add('button', undefined, 'Choose', { name: 'OK' });
}
else {
brandSelector.buttonGrp.srcFileTxt = add('statictext', undefined, SPpath);
}
}
brandSelector.lastGrp = add('group');
brandSelector.lastGrp.orientation = 'row';
brandSelector.lastGrp.alignment = 'right';
brandSelector.lastGrp.alignChildren = 'right';
with(brandSelector.lastGrp) {
if( SPpath != null ) {
brandSelector.buttonGrp.btnOK = add('button', undefined, 'Switch the brand!', { name: 'Ok' });
}
brandSelector.lastGrp.btnCancel = add('button', undefined, 'Cancel');
}
};
brandSelector.center();
return brandSelector;
}