-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex_from_charstyles.jsx
286 lines (249 loc) · 8.57 KB
/
index_from_charstyles.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
//DESCRIPTION: Add index markers to text formatted with selected character styles
// Peter Kahrel -- www.kahrel.plus.com
/*
To have the script swap items on a space character and insert a comma (John Keats > Keats, John),
add @@ to the character style's name.
To get multiple levels of topic from a condition's name, use %% as a separator.
If the found string contains two or more spaces, by default it will be split on the first space.
To designate another space, add a non-joiner before it.
*/
(function () {
var errors = false;
function errorM (m) {
alert (m, 'Error', true);
exit();
}
function getCondition (doc) {
var condition = doc.conditions.item('index_from_charstyles_error');
if (!condition.isValid) {
condition = doc.conditions.add ({name: 'index_from_charstyles_error'});
}
return condition;
}
//~ function progress () {
//~ var w = new Window ('palette {text: "Index character styles", alignChildren: ["left", "top"]}');
//~ w.fname = w.add ('statictext {characters: 40}');
//~ return w;
//~ }
function scriptPath () {
try {
return app.activeScript;
} catch (e) {
return File (e.fileName);
}
}
function saveData (obj) {
var f = File (scriptPath().fullName.replace (/\.jsx?(bin)?$/, '.txt'));
f.open ('w');
f.write (obj.toSource());
f.close ();
}
function getPrevious () {
var f = File (scriptPath().fullName.replace (/\.jsx?(bin)?$/, '.txt'));
var obj = {};
if (f.exists) {
obj = $.evalFile (f);
}
return obj;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
function addPageReference (topic, found) {
var ip = found.insertionPoints[0].index;
var pRef = topic.pageReferences.add (found.insertionPoints[0], PageReferenceType.CURRENT_PAGE);
if (Math.abs (pRef.sourceText.index - ip) > 0) {
try {
found.parentStory.characters[pRef.sourceText.index].move (LocationOptions.AFTER, found.insertionPoints[0]);
} catch (_) {
// . . .
}
}
}
function index_documents (data) {
var i, j, k;
var new_topic;
var found;
var doc;
var prompt;
var swap;
var target;
//var displ = progress();
//displ.show();
function addTopic (index, path, found, swap) {
var name = found.contents;
var space;
var n;
var t = index;
if (data.prefix_stylename == 'Style names nest topics') {
var parts = path.split('%%');
for (var i = 0; i < parts.length; i++) {
t = t.topics.add (parts[i].replace(/@@/g,''));
}
}
try {
if (!swap || !/\s/.test(name)) {
n = name.replace(/@@/g,'');
} else if (name.indexOf('\u200C ') < 0) {
space = name.indexOf(' ')
n = name.slice(space+1) + ', ' + name.slice(0,space);
} else {
space = name.indexOf('\u200C ');
n = name.slice(space+2) + ', ' + name.slice(0,space);
}
if (data.prefix_stylename == 'Style names are prefixes') {
n = path + '##' + n;
}
return t.topics.add (n);
} catch (_) {
found.appliedConditions = [getCondition(doc)];
errors = true;
}
}
for (i = app.documents.length-1; i >= 0; i--) {
doc = app.documents[i];
//displ.fname.text = app.documents[i].name;
if (data.replace_index == true && doc.indexes.length > 0) {
doc.indexes[0].topics.everyItem().remove();
}
if (doc.indexes.length == 0) {
doc.indexes.add();
}
for (j = 0; j < data.styles.length; j++) {
//prompt = app.documents[i].name + ': ' + data.styles[j];
//displ.fname.text = prompt;
try {
if (data.styleType == 'Character styles') {
app.findGrepPreferences.appliedCharacterStyle = doc.characterStyles.itemByID (data.styles[j]);
swap = /@@$/.test(doc.characterStyles.itemByID (data.styles[j]).name);
} else {
app.findGrepPreferences.appliedConditions = [doc.conditions.itemByID (data.styles[j])];
swap = /@@$/.test(doc.conditions.itemByID (data.styles[j]).name);
}
found = doc.findGrep();
for (k = found.length-1; k > -1; k--) {
//displ.fname.text = prompt + ' ' + k;
new_topic = addTopic (doc.indexes[0], data.paths[j], found[k], swap)
addPageReference (new_topic, found[k]);
}
} catch (_) {
//$.bp();
// The requested character style/condition is not in the document
}
}
}
//~ try {
//~ displ.close();
//~ } catch (_) {
//~ //
//~ }
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
function getData () {
var temp, styles;
function buildListCharacterStyles (scope, list, str, path) {
styles = scope.characterStyles.everyItem().getElements();
for (var i = 0; i < styles.length; i++) {
if (styles[i] != app.documents[0].characterStyles[0]) {
temp = list.add ('item', styles[i].name + (str == '' ? '' : ' ('+str+')'));
temp.id = styles[i].id; // Add property so we can easily get a handle on the style later
temp.path = path + styles[i].name;
}
}
for (var j = 0; j < scope.characterStyleGroups.length; j++) {
buildListCharacterStyles (scope.characterStyleGroups[j], list, str + (str == '' ? '' : ':') + scope.characterStyleGroups[j].name, path + scope.characterStyleGroups[j].name +'%%');
}
}
function buildListConditions (list) {
var c = app.documents[0].conditions.everyItem().getElements();
for (var i = 0; i < c.length; i++) {
temp = list.add ('item', c[i].name);
temp.id = c[i].id;
temp.path = c[i].name;
}
}
function getSelected (list) {
var o = {IDs: [], names: [], paths: []};
for (var i = list.selection.length-1; i >= 0; i--) {
o.IDs.push (list.selection[i].id);
o.paths.push (list.selection[i].path);
o.names.push (list.selection[i].text);
}
return o;
}
var w = new Window ('dialog', 'Style content to index', undefined, {closeButton: false});
w.alignChildren = ['fill','fill'];
w.dummy = w.add ('group');
w.dummy.alignChildren = ['fill','fill'];
w.styleList = w.dummy.add ('listbox {properties: {multiselect: true}}');
w.styleList.preferredSize.height = 200;
w.styleType = w.add ('dropdownlist', undefined, ['Character styles', 'Conditions']);
w.styleType.title = 'Target:';
w.styleType.titleLayout = {characters: 5};
w.prefix = w.add ('dropdownlist', undefined, ['Content only', 'Style names nest topics', 'Style names are prefixes']);
w.prefix.title = 'Mode:';
w.prefix.titleLayout = {characters: 5};
w.replace = w.add ('checkbox {text: "Replace existing index"}');
w.buttons = w.add ('group {orientation: "row", alignChildren: ["right", "bottom"]}');
w.buttons.add ('button {text: "OK"}');
w.buttons.add ('button {text: "Cancel"}');
w.styleList.onChange = function () {
if (w.styleList.selection == null) {
w.styleList.selection = 0;
}
}
w.styleType.onChange = function () {
w.styleList.removeAll();
switch (w.styleType.selection.text) {
case 'Character styles': buildListCharacterStyles (app.activeDocument, w.styleList, '', ''); break;
case 'Conditions': buildListConditions (w.styleList);
}
if (w.styleList.selection == null) {
w.styleList.selection = 0;
}
}
w.onShow = function () {
var previous = getPrevious();
w.replace.value = previous.replace_index;
w.prefix.selection = w.prefix.find (previous.prefix_stylename) || 0;
w.styleType.selection = w.styleType.find (previous.styleType) || 0;
if (w.styleList.children.length) {
w.styleList.selection[0].selected = false;
}
if (previous.names) {
for (var i = 0; i < previous.names.length; i++) {
if (w.styleList.find (previous.names[i]) !== null) {
w.styleList.find (previous.names[i]).selected = true;
}
}
} else {
w.styleList.selection = 0;
}
}
if (w.show () == 1) {
if (w.styleList.selection == null) {
alert ('Nothing selected made.'); exit();
}
var selectedStyles = getSelected (w.styleList)
var obj = {
styleType: w.styleType.selection.text,
styles: selectedStyles.IDs,
names: selectedStyles.names,
paths: selectedStyles.paths,
replace_index: w.replace.value,
prefix_stylename: w.prefix.selection.text
};
saveData (obj);
w.close();
return obj;
}
exit ();
}
if (!app.documents.length) exit();
var data = getData ();
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
index_documents (data);
if (errors) {
alert ('One or more errors encountered. Look for instances of the condition "index_from_charstyles_error", fix the problems, and run the script again.')
} else {
alert ('Done.');
}
}());