-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSearch Replace Fonts.jsx
429 lines (348 loc) · 12.6 KB
/
Search Replace Fonts.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
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
/**
* "Simple" utility to search & replace fonts in a project.
*
* TO USE:
* - Open the panel & hit "Load Fonts" to get installed system fonts
* - If a text layer is selected, it'll set the "Search" dropdown to this font
* - Select a text layer and press "Get Selected Font" to set the "Search" font
* - Change the "Replace" font
* - Press "Replace" to replace all layers with "Search" font to the "Replace" font
* - If you have layers selected, it will only search in these layers
* - If you have no layers selected, it will search all layers in the comp
*
* Note:
* - this may not work in every case. Fonts are weird.
*
* Font PSName from https://github.com/ten-A/Extend_Script_experimentals,
* licensed under MIT:
*
* The MIT License (MIT)
* Copyright (c) 2016 Ten
* Permission is hereby granted, free of charge, to any person obtaining a copy of those softwares and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author Zack Lovatt <zack@lova.tt>
* @version 0.1.0
*/
(function searchReplaceFonts(thisObj) {
var ui = createUI(thisObj);
ui.show();
/**
* Builds UI
*
* @returns {Window | Panel} Created window
*/
function createUI(thisObj) {
var win =
thisObj instanceof Panel
? thisObj
: new Window("palette", "Search/Replace Fonts", undefined, {
resizeable: true
});
win.orientation = "column";
win.margins = 5;
win.spacing = 5;
var pnlDropdowns = win.add("panel");
pnlDropdowns.preferredSize.width = 300;
pnlDropdowns.alignChildren = "fill";
var grpSearch = pnlDropdowns.add("group");
var stSearch = grpSearch.add("staticText", undefined, "Search:");
stSearch.preferredSize.width = 50;
var ddlSearch = grpSearch.add("dropdownlist", undefined, [
"Press 'Load Fonts!'"
]);
ddlSearch.alignment = ["fill", "fill"];
ddlSearch.selection = 0;
var grpReplace = pnlDropdowns.add("group");
var stReplace = grpReplace.add("staticText", undefined, "Replace:");
stReplace.preferredSize.width = 50;
var ddlReplace = grpReplace.add("dropdownlist", undefined, [
"Press 'Load Fonts!'"
]);
ddlReplace.alignment = ["fill", "fill"];
ddlReplace.selection = 0;
var grpButtons = win.add("group");
var btnLoad = grpButtons.add("button", undefined, "Load Font List");
btnLoad.helpTip = "Refresh Fonts";
btnLoad.onClick = function () {
var fonts = _getInstalledFonts();
if (fonts.length === 0) {
alert("Couldn't load fonts!");
return;
}
ddlSearch.removeAll();
ddlReplace.removeAll();
for (var ii = 0, il = fonts.length; ii < il; ii++) {
var font = fonts[ii];
ddlSearch.add("item", font);
ddlReplace.add("item", font);
}
// Check whether there's a selected layer; if so, get its idx in the list
var selectedFontName = _getSelectedFontName();
if (selectedFontName) {
var selectedFontIndex = _quickArrayIndexOf(fonts, selectedFontName);
ddlSearch.selection = selectedFontIndex;
ddlReplace.selection = selectedFontIndex;
} else {
ddlSearch.selection = 0;
ddlReplace.selection = 0;
}
};
var btnGet = grpButtons.add("button", undefined, "Get Selected Font");
btnGet.helpTip = "Get the font of the currently-selected layer";
btnGet.onClick = function () {
var selectedFontName = _getSelectedFontName();
$.writeln(selectedFontName);
if (!selectedFontName) {
alert("Select a text layer!");
return;
}
var itemsText = _getListItemsText(ddlSearch);
var selectedFontIndex = _quickArrayIndexOf(itemsText, selectedFontName);
if (selectedFontIndex > -1) {
ddlSearch.selection = selectedFontIndex;
}
};
var btnReplace = grpButtons.add("button", undefined, "Replace");
btnReplace.helpTip = "Replace font in selected layer(s)";
btnReplace.onClick = function () {
var ddlSearchItem = ddlSearch.selection;
if (!ddlSearchItem) {
alert("Select font to search!");
return;
}
var ddlReplaceItem = ddlReplace.selection;
if (!ddlReplaceItem) {
alert("Select font to replace!");
return;
}
var searchFont = ddlSearchItem.text;
var replaceFont = ddlReplaceItem.text;
_replaceFonts(searchFont, replaceFont);
};
return win;
}
/**
* Replaces fonts in selected layers that match search & replace names
*
* @param {string} search Font name to search for
* @param {string} replace Font name to replace with
*/
function _replaceFonts(search, replace) {
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Select a text layer or open a comp!");
return;
}
app.beginUndoGroup("Search/Replace Fonts");
try {
var layers = _getSelectedLayersOrAll(comp);
for (var ii = 0, il = layers.length; ii < il; ii++) {
var layer = layers[ii];
var font = _getLayerFontName(layer);
if (!font || font !== search) {
continue;
}
var sourceText = layer.text.sourceText;
var textDoc = sourceText.value;
textDoc.font = replace;
sourceText.setValue(textDoc);
}
} catch (e) {
alert(e);
} finally {
app.endUndoGroup();
}
}
/**
* Gets the font name for the selected layer, if text layer
*
* @return {string} Selected layer font name
*/
function _getSelectedFontName() {
$.writeln("--> _getSelectedFontName");
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
return;
}
var layer = comp.selectedLayers[0];
var layerFontName = _getLayerFontName(layer);
$.writeln("<-- _getSelectedFontName: " + layerFontName);
return layerFontName;
}
/**
* Gets an array of strings from a listbox's items
*
* @param {List} list SUI List
* @return {string[]} Collect of list labels
*/
function _getListItemsText(list) {
var itemsText = [];
var items = list.items;
for (var ii = 0, il = items.length; ii < il; ii++) {
var item = items[ii];
itemsText.push(item.text);
}
return itemsText;
}
/**
* Gets the font name for a given layer
*
* @param {Layer} layer Layer to get name from
* @return {string} Layer name
*/
function _getLayerFontName(layer) {
$.writeln("--> _getLayerFontName: " + (layer ? layer.name : "NO LAYER"));
if (!(layer && layer instanceof TextLayer)) {
return;
}
var sourceText = layer.text.sourceText;
var textDoc = sourceText.value;
var layerFontName = textDoc.font;
$.writeln("<-- _getLayerFontName: " + layerFontName);
return layerFontName;
}
/**
* Gets PS names from fonts in a folder
*
* @param {Folder} folder Folder to search
* @param {string} filter File filter
* @returns {string[]} Array of font PS names
*/
function _getPSNamesFromFolder(folder) {
$.writeln("--> _getPSNamesFromFolder: " + folder.fsName);
var psNames = [];
var files = folder.getFiles();
for (var ii = 0, il = files.length; ii < il; ii++) {
var file = files[ii];
if (!(file instanceof File)) {
continue;
}
var psName = _getFontPSName(file);
if (!psName) {
continue;
}
psNames.push(psName);
}
$.writeln("<-- _getPSNamesFromFolder: " + psNames.length);
return psNames;
}
/**
* Gets font PS name from a file
*
* @author Ten <https://github.com/ten-A/Extend_Script_experimentals>
* @copyright 2016 Ten
* @param {File} fontFile Font file to get info from
* @returns {string | undefined} PS Name for file
*/
function _getFontPSName(fontFile) {
function getPSName(r){function e(r){var e=256*r.readch().charCodeAt(0);return e+=r.readch().charCodeAt(0)}function a(r){for(var e=0,a=16777216,t=0;t<4;t++)e+=r.readch().charCodeAt(0)*a,a/=256;return e}function t(r){var e=r.readch().charCodeAt(0),a=e<<8;return a+=e=r.readch().charCodeAt(0)}try{var c=function(e){for(j=0;j<e;j++){for(dat=["",0,0,0],i=0;i<4;i++)dat[0]+=r.readch();if(dat[1]=a(r),dat[2]=a(r),dat[3]=a(r),"name"==dat[0])return dat}}(function(){var e,a=["",0,0,0,0],c=r.readch();if("O"==c)for(a[0]=c,i=0;i<3;i++)a[0]+=r.readch();else for(a[0]="0x0"+c.charCodeAt(0).toString(16),i=0;i<3;i++)1==(e=(c=r.readch()).charCodeAt(0).toString(16)).length&&(e="0"+e),a[0]+=e;return a[1]=t(r),a[2]=t(r),a[3]=t(r),a[4]=t(r),a}()[1]);if(!c){return}r.seek(c[2]);for(var d=[],h="",o=0,n=(e(r),e(r)),f=e(r),i=0;i<n&&6!=(d=[e(r),e(r),e(r),e(r),e(r),e(r)+f+c[2]])[3];i++);if(r.seek(d[5]),0==(h+=r.readch()).charCodeAt(0))for(h=r.readch(),o=1;o<d[4]/2;o++)r.readch(),h+=r.readch();else for(o=0;o<d[4] - 1;o++)h+=r.readch();return h}catch(r){alert(r)}}
var psName;
fontFile.encoding = "BINARY";
try {
if (fontFile.open("r")) {
psName = getPSName(fontFile);
}
} catch (e) {
} finally {
fontFile.close();
}
return psName;
}
/**
* Gets system font folder paths
*
* @return {string[]} Paths to system font folders
*/
function _getFontFolderPaths() {
var userFontFolder = "~/Library/Fonts";
var systemFontFolder = "/System/Library/Fonts";
if ($.os.indexOf("Windows") > -1) {
userFontFolder = "~/AppData/Local/Microsoft/Windows/Fonts";
systemFontFolder = "C:/Windows/Fonts";
}
return [userFontFolder, systemFontFolder];
}
/**
* Gets all installed fonts on your system
*
* @return {string[]}
*/
function _getInstalledFonts() {
$.writeln("--> _getInstalledFonts");
var fontFolders = _getFontFolderPaths();
var fonts = [];
for (var ii = 0, il = fontFolders.length; ii < il; ii++) {
var fontFolderPath = fontFolders[ii];
var fontFolder = new Folder(fontFolderPath);
if (!fontFolder.exists) {
continue;
}
var folderFonts = _getPSNamesFromFolder(fontFolder);
fonts = fonts.concat(folderFonts);
}
// Sort the list
fonts.sort(function (a, b) {
return a > b;
});
// Deduplicate the list
var deduplicatedFonts = _deduplicateArray(fonts);
$.writeln("<-- _getInstalledFonts: " + deduplicatedFonts.length);
return deduplicatedFonts;
}
/**
* Gets the selected layers in a given comp, or all
*
* @param {CompItem} comp Comp to get layers from
* @return {Layer[]} Layers
*/
function _getSelectedLayersOrAll(comp) {
var layers = comp.selectedLayers;
if (layers.length === 0) {
for (var ii = 1, il = comp.numLayers; ii <= il; ii++) {
var layer = comp.layer(ii);
layers.push(layer);
}
}
return layers;
}
/**
* Naive array deduplication method, ensuring each item only appears once
*
* @param {any[]} array Array to deduplicate
* @return {any[]} Deduplicated array
*/
function _deduplicateArray(array) {
$.writeln("--> _deduplicateArray: " + array.length);
var deduplicated = [];
var arrayObj = {};
for (var ii = 0, il = array.length; ii < il; ii++) {
var item = array[ii];
// If we've found it, skip
if (arrayObj.hasOwnProperty(item)) {
continue;
}
arrayObj[item] = true;
deduplicated.push(item);
}
$.writeln("<-- _deduplicateArray: " + deduplicated.length);
return deduplicated;
}
/**
* Quick & easy array.indexOf
*
* @param {any[]} array Array to search
* @param {any} searchElement Element to search in array
* @return {number} Index of element in array
*/
function _quickArrayIndexOf(array, searchElement) {
for (var ii = 0, il = array.length; ii < il; ii++) {
var element = array[ii];
if (element === searchElement) {
return ii;
}
}
return -1;
}
})(this);