-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdropdown.js
More file actions
executable file
·79 lines (67 loc) · 2.27 KB
/
dropdown.js
File metadata and controls
executable file
·79 lines (67 loc) · 2.27 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
// Many parts of this code borrowed from IndiSnip
// http://indisnip.wordpress.com/2010/08/24/findchange-missing-font-with-scripting/
//get unique Array elements
Array.prototype.unique = function () {
var r = new Array();
o:for (var i = 0, n = this.length; i < n; i++) {
for (var x = 0, y = r.length; x < y; x++) {
if (r[x]==this[i]) {
continue o;
}
}
r[r.length] = this[i];
}
return r;
}
//search inside array
Array.prototype.findIn = function(search){
var r = Array();
for (var i=0; i<this.length; i++) {
if (this[i].indexOf(search) != -1) {
r.push(this[i].substr(this[i].indexOf("\t") + 1, this[i].length));
}
}
return r;
};
Array.prototype.findID = function (str) {
for (var i = 0; i < this.length; i++) {
if (this[i].indexOf(str) === 0) {
return i;
}
}
return 0;
};
//FontSelect makes a font selection gui widget, and returns an object
//with the single method getFont, which can be called to get the selected font
function FontSelect(group, font) {
var splitFont = font.split('\t');
var fontFamily = splitFont[0];
var fontStyle = splitFont[1];
var sysFonts = app.fonts.everyItem();
var sysFontsList = sysFonts.fontFamily.unique();
sysFontsList.unshift("- Select Font Family -");
var fontFamilyId = sysFontsList.findID(fontFamily);
var availableFonts = group.add('dropdownlist', undefined, sysFontsList);
var availableStyles = group.add('dropdownlist');
availableStyles.minimumSize = [180,25];
availableFonts.onChange = function () {
availableStyles.removeAll();
var sysFontAvailableStyles = sysFonts.name.findIn(availableFonts.selection);
for (var i = 0; i < sysFontAvailableStyles.length; i++) {
availableStyles.add('item',sysFontAvailableStyles[i]);
}
fontStyleId = sysFontAvailableStyles.findID(fontStyle);
availableStyles.selection = fontStyleId;
}
availableFonts.selection = fontFamilyId;
return {
getFont: function () {
if (availableFonts.selection && availableStyles.selection) {
return availableFonts.selection.text + '\t' + availableStyles.selection.text;
}
else {
return font; //return the default font if no font selected
}
}
};
}