-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathendnotes_references_hyperlinks.jsx
More file actions
235 lines (191 loc) · 7.85 KB
/
endnotes_references_hyperlinks.jsx
File metadata and controls
235 lines (191 loc) · 7.85 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
//DESCRIPTION: Add two-way links between note numbers and note references.
// Peter Kahrel -- www.kahrel.plus.com
/*
Notes and references must be in the same story, but there can be more than
one story with notes and references.
*/
#targetengine endnote_hyperlinks;
(function () {
//-----------------------------------------------------------
// Return the path of the running script
function scriptPath() {
try {
return app.activeScript;
} catch (e) {
return File (e.fileName);
}
}
//----------------------------------------------------------
// Read any stored dialog settings
function read_history () {
var obj = {};
var f = File(scriptPath().fullName.replace(/\.jsx$/,'.txt'));
if (f.exists) {
obj = $.evalFile (f);
}
return obj;
}
//----------------------------------------------------------
// Store dialog settings
function write_history (obj) {
var f = File(scriptPath().fullName.replace(/\.jsx$/,'.txt'));
f.open ('w');
f.write (obj.toSource());
f.close();
}
//-----------------------------------------------------------
function errorM (m){
alert (m, 'Error', true);
exit();
}
//-----------------------------------------------------------
// Set up the Find/Change dialog
function setGrep (find, replace, options) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = find;
app.changeGrepPreferences.changeTo = replace;
if (options == undefined) options = {};
app.findGrepPreferences.appliedParagraphStyle = options.PS || null;
app.findGrepPreferences.appliedCharacterStyle = options.CS || null;
app.findChangeGrepOptions.properties ={
includeFootnotes: options.FN || false,
includeMasterPages: options.M || false,
includeHiddenLayers: options.HL || false,
includeLockedLayersForFind: options.LL || false,
includeLockedStoriesForFind: options.LS || false
}
}
//-----------------------------------------------------------
// A window to display messages while the script runs
function create_message_window (le) {
var w = Window.find ('palette', 'Linking notes and references');
if (w == null) {
w = new Window ('palette','Endnote to footnote', undefined, {closeButton: false});
w.txt = w.add ('statictext', undefined, ' ');
w.txt.characters = le;
}
return w;
}
//-------------------------------------------------------------
// The interface
function get_styles (doc) {
var styles = read_history ();
var w = new Window ('dialog {text: "Link notes and references", properties: {closeButton: false}}');
var c_styles = doc.characterStyles.everyItem().name;
c_styles.shift();
var p_styles = doc.paragraphStyles. everyItem().name;
var panel = w.add ('panel {alignChildren: "right"}');
var gr1 = panel.add ('group {_ : StaticText {text: "Character style:"}}');
var c_list = gr1.add ('dropdownlist', undefined, c_styles);
var gr2 = panel.add ('group {_ : StaticText {text: "Paragraph style:"}}');
var p_list = gr2.add ('dropdownlist', undefined, p_styles);
var buttons = w.add ('group {alignment: "right"}');
buttons.add ('button', undefined, 'Cancel', {name: 'cancel'});
buttons.add ('button', undefined, 'OK', {name: 'ok'});
c_list.preferredSize.width = p_list.preferredSize.width = 200;
var temp = c_list.find (styles.cstyle);
if (styles.cstyle == undefined || temp == null){
c_list.selection = 0;
} else {
c_list.selection = temp;
}
temp = p_list.find (styles.pstyle);
if (styles.pstyle == undefined || temp == null) {
p_list.selection = 0;
} else {
p_list.selection = temp;
}
if (w.show () == 2) {
exit ();
}
write_history ({
cstyle: c_list.selection.text,
pstyle: p_list.selection.text
});
return {
cstyle: doc.characterStyles.item (c_list.selection.text),
pstyle: doc.paragraphStyles.item (p_list.selection.text)
}
} // get_styles
//---------------------------------------------------------------
// Convert automatic paragraph numbering to text and delete the cross-references
function convert_numbering (pstyle) {
//~ app.findGrepPreferences = null;
//~ app.findGrepPreferences.appliedParagraphStyle = pstyle;
//~ app.findGrepPreferences.findWhat = '^.'
//~ var p = app.activeDocument.findGrep();
//~ for (var i = 0; i < p.length; i++)
//~ p[i].convertBulletsAndNumberingToText();
var hlinks = pstyle.parent.hyperlinks.everyItem().getElements();
for (var i = hlinks.length-1; i > -1; i--){
if (hlinks[i].source instanceof CrossReferenceSource && hlinks[i].destination.destinationText.appliedParagraphStyle.name == pstyle.name){
hlinks[i].destination.destinationText.paragraphs[0].convertBulletsAndNumberingToText();
hlinks[i].source.remove();
}
}
}
//---------------------------------------------------------------
// Make sure that each note is a single paragraph by replacing with a code (here %£%)
// hard returns which aren't followed by a digit
function fetch_notes (doc, pstyle) {
setGrep (/\r(?![\divxl])/.source, '%£%', {PS: pstyle});
doc.changeGrep();
app.findGrepPreferences.findWhat = '^[\\divxl]+';
return doc.findGrep();
}
//---------------------------------------------------------------
function fetch_refs (doc, cstyle) {
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '[\\divxl]+';
app.findGrepPreferences.appliedCharacterStyle = cstyle;
return doc.findGrep ();
}
//---------------------------------------------------------------
function process_story (story, data, showmessage) {
var notes = fetch_notes (story, data.pstyle);
var refs = fetch_refs (story, data.cstyle);
var doc = story.parent;
var id, cue_source, note_source, cue_anchor, note_anchor;
for (var i = refs.length-1; i > -1; i--) {
showmessage.txt.text = 'Linking... ' + i;
id = '_' + String(story.id) + '_' + String (i+1);
cue_source = doc.hyperlinkTextSources.add (refs[i]);
note_source = doc.hyperlinkTextSources.add (notes[i]);
cue_anchor = doc.hyperlinkTextDestinations.add (refs[i], {name: 'cue' + id});
note_anchor = doc.hyperlinkTextDestinations.add (notes[i], {name: 'note' + id});
doc.hyperlinks.add (cue_source, note_anchor, {name: 'HL_cue_to_note' + id});
doc.hyperlinks.add (note_source, cue_anchor, {name: 'HL_note_to_cue' + id});
}
}
//---------------------------------------------------------------
function endnote_links (data){
var doc = app.activeDocument;
// A display window showing progress
var showmessage = create_message_window (30);
showmessage.show();
showmessage.txt.text = 'Checking styles...';
convert_numbering (data.pstyle);
showmessage.txt.text = 'Collecting notes and references...'
// Find notes and references and compare length
var notes = fetch_notes (doc, data.pstyle);
var refs = fetch_refs (doc, data.cstyle);
if (notes.length != refs.length) {
alert ('Notes and references do not match:\r(' + notes.length + ' notes, ' + refs.length + ' references.)');
exit ();
}
// Now process each story separately
var stories = app.activeDocument.stories.everyItem().getElements();
for (var i = 0; i < stories.length; i++) {
process_story (stories[i], data, showmessage);
}
// Finish off at document level
// Restore any paragraph breaks
setGrep ('%£%', '\\r', {FN: true});
app.activeDocument.changeGrep();
try {showmessage.close()} catch(_){}
} // endnote_links
//---------------------------------------------------------------
if (confirm ('This script is destructive. \rMake sure you have saved the document \rand/or that you have a copy of the document.\r\rContinue?', true, 'WARNING')) {
endnote_links (get_styles (app.activeDocument));
}
}());