forked from josecm/PDFSessions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsavesessions.js
461 lines (415 loc) · 12.3 KB
/
savesessions.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
/*
save session is a java script for Acrobat Reader
based on save tabs by Andrey Kartashov found on
https://stackoverflow.com/questions/12689154/adobe-acrobat-reader-tabs-saving-and-autoloading
Put it in $HOME/.adobe/Acrobat/9.0/JavaScripts (or in
the equivalent program files folder under Windows,
%appdata%/adobe/acrobat/Privileged/DC/JavaScripts, or
C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\Javascripts)
and it will automatically
be loaded.
*/
var sessionManager = (function () {
var alert = {
nIcon: {
error: 0,
warning: 1,
question: 2,
status: 3
},
nType: {
ok: 0,
ok_cancel: 1,
yes_no: 2,
yes_no_cancel: 3
},
nButton: {
ok: 1,
cancel: 2,
no: 3,
yes: 4
}
};
var sessionManager = {
parentMenu: "Window",
openMenu: "&Open Session",
updateMenu: "&Update Session",
deleteMenu: "&Delete Session",
editMenu: "&Edit Session Data",
deleteAll: "&Remove All Sessions",
// Session Edit Dialog Definition
oDlgEditSession: {
strSessionData: '[]',
IsValidJSON: {}/*injected*/,
initialize: function (dialog) {
dialog.load({"sese": this.strSessionData});
},
commit: function (dialog) {
var data = dialog.store();
this.strSessionData = data["sese"];
},
validate: function (dialog) {
var data = dialog.store();
var error = this.IsValidJSON(data["sese"], true);
if (true !== error) {
app.alert(error.message);
return false;
}
return true;
},
other: function (dialog) { // other button
var data = dialog.store();
var message = '';
var error = this.IsValidJSON(data["sese"], true);
if (true !== error) {
message = error.message;
} else {
message = 'Valid JSON';
}
app.alert(message, true === error ? alert.nIcon.status : alert.nIcon.error);
},
description: {
name: "Session Edit", elements:
[
{
type: "view", elements:
[
{name: "Edit Session JSON", type: "static_text"},
{item_id: "sese", type: "edit_text", multiline: true, char_width: 120, char_height: 80},
{type: "ok_cancel_other", other_name: "Check Syntax"}
]
}
]
}
},
// Session Dialog Definition
oDlg: {
strName: "Session Name",
initialize: function (dialog) {
dialog.load({"usnm": this.strName});
},
commit: function (dialog) {
var data = dialog.store();
this.strName = data["usnm"];
},
description: {
name: "Session Dialog", elements:
[
{
type: "view", elements:
[
{name: "Enter session name:", type: "static_text"},
{item_id: "usnm", type: "edit_text", char_width: 40},
{type: "ok_cancel"}
]
}
]
}
},
/**
* Loads named session into menus
* @param name
*/
AddSessionToMenu: function (name) {
app.addMenuItem({
cName: "open" + name,
cUser: name,
cParent: this.openMenu,
cExec: "sessionManager.LoadTabs(\"" + name + "\");"
});
app.addMenuItem({
cName: "update" + name,
cUser: name,
cParent: this.updateMenu,
cExec: "sessionManager.UpdateTab(\"" + name + "\", true);"
});
app.addMenuItem({
cName: "del" + name,
cUser: name,
cParent: this.deleteMenu,
cExec: "sessionManager.DeleteTab(\"" + name + "\", true);"
});
app.addMenuItem({
cName: "edit" + name,
cUser: name,
cParent: this.editMenu,
cExec: "sessionManager.EditTab(\"" + name + "\", true);"
});
},
/**
* Hides named menu item
* @param name
*/
HideMenu: function (name) {
app.hideMenuItem("open" + name);
app.hideMenuItem("del" + name);
app.hideMenuItem("edit" + name);
app.hideMenuItem("update" + name);
},
/**
* Loading Saved Tabs
* @param name
*/
LoadTabs: function (name) {
var session = this.GetSession(name);
if (session == null || session === []) {
return;
}
var docs = this.trustedActiveDocs();
var paths = [];
for (var i = 0; i < docs.length; i++) {
paths.push(docs[i].path)
}
var doc = {};
// open and apply data
for (doc in session) {
try {
var d = app.openDoc(session[doc].path);
// if a session doc already open, do not change page, layout or zoom etc
if (-1 === paths.indexOf(session[doc].path)) {
// retained for compatibility
if ('undefined' !== typeof session[doc].pageNum) {
d.pageNum = session[doc].pageNum;
}
if ('undefined' !== typeof session[doc].layout) {
d.layout = session[doc].layout;
}
if ('undefined' !== typeof session[doc].zoom) {
d.zoom = session[doc].zoom;
}
// use viewstate if exists.
if ('undefined' !== typeof session[doc].viewState) {
d.viewState = eval(session[doc].viewState); //no other choices but eval.
}
}
} catch (e) {
console.println('applyViewState: ' + e);
}
}
//focus on doc tagged isFront
for (doc in session) {
try {
if (session[doc].isFront) {
app.openDoc(session[doc].path);
break;
}
} catch (e) {
console.println('bringToFront: ' + e);
}
}
},
/**
*
* @param name
* @param warn
*/
DeleteTab: function (name, warn) {
var proceed = true;
if (undefined !== warn && warn) {
if (app.alert("Are you sure you want to delete " + name + " ?", alert.nIcon.warning, alert.nType.yes_no, "Submit Validation") !== alert.nButton.yes) {
proceed = false;
}
}
if (proceed) {
this.HideMenu(name);
delete global.tabs_opened[name];
global.setPersistent("tabs_opened", true);
}
},
/**
* Function with trusted section returning opened documents
* @returns {Array|Object[]}
*/
trustedActiveDocs: app.trustedFunction(function () {
app.beginPriv();
var d = app.activeDocs;
app.endPriv();
return d;
}),
/**
* Loads session from global store
* @param name
* @returns {Array}
*/
GetSession: function (name) {
try {
if (global.tabs_opened[name]) {
return JSON.parse(global.tabs_opened[name]);
}
} catch (ee) {
console.println('GetSession: ' + ee);
}
return [];
},
/**
* Persists session data to global store.
* if addToMenu true, adds into menu
* @param sessionName
* @param sessionData
* @param addToMenu
*/
PersistSession: function (sessionName, sessionData, addToMenu) {
sessionData = (typeof sessionData !== 'undefined') ? sessionData : '[]';
addToMenu = (typeof addToMenu !== 'undefined') ? addToMenu : true;
global.tabs_opened[sessionName] = sessionData;
global.setPersistent("tabs_opened", true);
if (addToMenu) {
this.AddSessionToMenu(sessionName)
}
},
/**
* Gets JSON for all open docs
* @param trustedActiveDocs {Object}
* @returns {string}
*/
GetDocumentsJSON: function (trustedActiveDocs) {
var docs = [];
for (var i = 0; i < trustedActiveDocs.length; i++) {
docs.push(
{
'path': trustedActiveDocs[i].path,
'viewState': trustedActiveDocs[i].viewState.toSource(),
'isFront': currentDocPath() === trustedActiveDocs[i].path
}
)
}
return JSON.stringify(docs, null, 2);
},
/**
* Saving Tabs that are opened (session) data
*/
SaveSession: function () {
var d = this.trustedActiveDocs();
if (d.length === 0) {
app.alert("No documents opened to save session", alert.nIcon.status);
return;
}
if (app.execDialog(this.oDlg) === "ok") {
var nRslt = 4;
var savedSession = this.GetSession(this.oDlg.strName);
console.println('SaveSession: ' + savedSession);
var already_exists = (savedSession.length > 0);
if (already_exists) {
nRslt = app.alert("A session with name " + this.oDlg.strName + " already exists...\n\n" + "Do you want to continue?", alert.nIcon.question, alert.nType.yes_no, "Submit Validation");
}
if (nRslt === alert.nButton.yes) {
var session = this.oDlg.strName;
this.PersistSession(session, this.GetDocumentsJSON(d), !already_exists);
}
}
},
/**
* Updates existing session with currently opened docs and positions
*/
UpdateTab: function (name) {
var d = this.trustedActiveDocs();
if (d.length === 0) {
app.alert("No documents opened to update into session \"" + name + "\"", alert.nIcon.status);
return;
}
var nRslt = app.alert("Really update session \"" + name + "\" with current documents?\n\n" + "Do you want to continue?", alert.nIcon.question, alert.nType.yes_no, "Submit Validation");
if (nRslt === alert.nButton.yes) {
this.PersistSession(name, this.GetDocumentsJSON(d), false);
}
},
/**
* Allows to manually edit the JSON store for the named session.
* @param session
*/
EditTab: function (session) {
this.oDlgEditSession.strSessionData = global.tabs_opened[session];
this.oDlgEditSession.IsValidJSON = this.IsValidJSON;
if (app.execDialog(this.oDlgEditSession) === 'ok') {
var json = this.oDlgEditSession.strSessionData;
if (this.IsValidJSON(json)) {
this.PersistSession(session, json, false);
} else {
app.alert('Malformed JSON, not saving.');
}
}
},
/**
* Remove all saved sessions
*/
RemoveAllSessions: function () {
if (alert.nButton.yes === app.alert("Are you sure you want to delete all sessions?", alert.nIcon.question, alert.nType.yes_no, "Submit Validation")) {
for (var index in global.tabs_opened) {
this.DeleteTab(index, false);
}
global.tabs_opened = [];
global.setPersistent("tabs_opened", true);
}
},
/**
* Tests string for valid JSON structure
* @param str
* @param returnError
* @returns {boolean|*}
*/
IsValidJSON: function (str, returnError) {
returnError = (typeof returnError !== 'undefined') ? returnError : false;
try {
JSON.parse(str);
return true;
} catch (e) {
if (returnError) {
return e;
}
return false;
}
},
/**
* Add menu items and setup
*/
init: function () {
if (global.tabs_opened == null) {
console.println('init: ' + 'global.tabs_opened == null');
global.tabs_opened = [];
global.setPersistent("tabs_opened", true);
}
app.addMenuItem({
cName: "-",
cParent: this.parentMenu,
cExec: "void(0);"
});
app.addMenuItem({
cName: "&Save Session",
cParent: this.parentMenu,
cExec: "sessionManager.SaveSession();"
});
app.addSubMenu({
cName: this.openMenu,
cParent: this.parentMenu
});
app.addSubMenu({
cName: this.updateMenu,
cParent: this.parentMenu
});
app.addSubMenu({
cName: this.editMenu,
cParent: this.parentMenu
});
app.addSubMenu({
cName: this.deleteMenu,
cParent: this.parentMenu
});
app.addMenuItem({
cName: this.deleteAll,
cParent: this.parentMenu,
cExec: "sessionManager.RemoveAllSessions();"
});
for (key in global.tabs_opened) {
this.AddSessionToMenu(key);
}
return this;
}
};
/**
* get the path of the currently-viewed aka inFront document
* @returns {string}
*/
var currentDocPath = function () {
return this.path;
};
return sessionManager.init();
})();