-
Notifications
You must be signed in to change notification settings - Fork 1
/
prevent-multiple-opens.jsx
80 lines (67 loc) · 1.97 KB
/
prevent-multiple-opens.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
#targetengine "preventMultipleOpens"
var eventListenerOpen = app.addEventListener("afterOpen", createTxtFile);
var eventListenerClose = app.addEventListener("beforeClose", deleteTxtFile);
var userName = (app.userName != "Unknown User Name") ? app.userName : "unknown user";
function customAlert(message, delaySeconds, title) {
title = title || 'Alert';
var alertWindow = new Window('palette', title);
var control_text = alertWindow.add('edittext', [0, 0, 400, 100], message, {multiline: false});
if(delaySeconds == 0){
var control_close = alertWindow.add('button', undefined, 'Close');
control_close.onClick = function(){
if(alertWindow){
alertWindow.hide();
app.activeDocument.close();
}
};
}
alertWindow.show();
alertWindow.update();
if(delaySeconds > 0){
$.sleep(delaySeconds * 1000);
alertWindow.hide();
alertWindow = null;
}
}
function createTxtFile(event) {
try {
var doc = event.parent;
var txtFilePath = decodeURI(doc.fullName.absoluteURI.replace(/indd$/i, "txt"));
if (txtFilePath != null) {
var txtFile = new File(txtFilePath);
txtFile.encoding = "UTF-8";
if (!txtFile.exists) {
txtFile.open("w");
txtFile.write(userName);
txtFile.close();
}
else {
txtFile.open("r");
var userNameSaved = txtFile.read();
txtFile.close();
if (userNameSaved != userName) {
customAlert('This Document is already opened by ' + userNameSaved, 0, 'Warning!');
}
}
}
}
catch(err) {}
}
function deleteTxtFile(event) {
try {
var doc = event.parent;
var txtFilePath = decodeURI(doc.fullName.absoluteURI.replace(/indd$/i, "txt"));
if (txtFilePath != null) {
var txtFile = new File(txtFilePath);
if (txtFile.exists) {
txtFile.open("r");
var userNameSaved = txtFile.read();
txtFile.close();
if (userNameSaved == userName) {
txtFile.remove();
}
}
}
}
catch(err) {}
}