-
Notifications
You must be signed in to change notification settings - Fork 328
/
forceCloseOtherDocuments.jsx
142 lines (116 loc) · 4.36 KB
/
forceCloseOtherDocuments.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
/*
Author: Alexander Ladygin (i@ladygin.pro)
Program version: Adobe Illustrator CC+
Name: forceCloseOtherDocuments.jsx;
Copyright (c) 2018
www.ladyginpro.ru
*/
function getCustomNumbers ($str, items, returnItems) {
var __num = $str.replace(/ /g, '').replace(/[^-0-9^,]/gim,'').split(','),
$maxItems = items.length,
j = __num.length,
arr = [];
function getNumbersBetweenMinMax (min, max) {
var numbers = [];
for (var n = min; n <= max; n++) {
if (n < $maxItems) {
numbers.push(returnItems ? items[n] : n);
}
}
return numbers;
}
while (j--) {
if (__num[j].indexOf('-') > -1) {
var values = __num[j].split('-'),
min = parseInt(values[0]),
max = parseInt(values[1]);
if (!isNaN(min) && !isNaN(max)) arr = arr.concat(getNumbersBetweenMinMax(min - 1, max - 1));
}
else {
var __val = parseInt(__num[j]);
if (!isNaN(__val) && __val <= $maxItems) {
arr.push(returnItems ? items[__val - 1] : __val - 1);
}
}
}
return arr;
}
function __documentsClose (save_options, userOptions) {
// documentsDontClose => [Document] || [Array]
var options = {};
options.not = userOptions.not || [];
options.document = userOptions.document || null;
if (app.documents.length) {
save_options = (typeof save_options === 'string' && save_options.length ? save_options : 'PROMPTTOSAVECHANGES').toLowerCase();
if ((save_options.slice(0, 1) === 'n') || (save_options.slice(0, 1) === 'd')) save_options = 'DONOTSAVECHANGES';
else if (save_options.slice(0, 1) === 's') save_options = 'SAVECHANGES';
else save_options = 'PROMPTTOSAVECHANGES';
options.not = (options.not.typename === 'Document' ? [options.not] : options.not.typename === 'Documents' ? options.not : options.not);
var l = options.not.length;
if (!options.documents) {
var i = app.documents.length;
while (i--) {
var check = true;
for (var j = 0; j < l; j++) { if (app.documents[i] === options.not[j]) check = false; }
if (check) app.documents[i].close(SaveOptions[save_options.toUpperCase()]);
}
}
else {
options.document.close(SaveOptions[save_options.toUpperCase()]);
}
}
else {
throw new Error('Documents not found!');
}
};
function __documentsForceCloseOther (documentsDontClose) {
if (app.documents.length) {
documentsDontClose = documentsDontClose instanceof Array ? documentsDontClose.concat([app.activeDocument]) : [app.activeDocument];
return __documentsClose('not', {
not: documentsDontClose
});
}
else {
throw new Error('Documents not found!');
}
};
function getDocumentToList() {
var result = [], l = app.documents.length;
for (i = 0; i < l; i++) {
result.push((i + 1) + '. ' + app.documents[i].name);
}
return result;
}
var win = new Window('dialog', 'Force close others \u00A9 www.ladyginpro.ru', undefined);
win.orientation = 'column';
win.alignChildren = ['fill', 'fill'];
var panel = win.add('panel', undefined, 'Exclude documents:');
panel.orientation = 'column';
panel.alignChildren = ['fill', 'fill'];
panel.margins = 20;
var inputPlaceholder = panel.add('statictext', undefined, 'Example: "1, 2, 3" or "1, 2-5, 9"'),
inputDocuments = panel.add('edittext', undefined, 1);
var docGroup = panel.add('group');
docGroup.orientation = 'row';
var docPlaceholder = docGroup.add('statictext', undefined, 'Only read'),
docList = docGroup.add('dropdownlist', undefined, getDocumentToList());
docList.maximumSize = [115, 25];
docList.selection = 0;
var winButtons = win.add('group');
winButtons.orientation = 'row';
winButtons.alignChildren = ['fill', 'fill'];
var cancel = winButtons.add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () { win.close(); }
var ok = winButtons.add('button', undefined, 'OK');
ok.helpTip = 'Press Enter to Run';
ok.onClick = startAction;
ok.active = true;
win.center();
win.show();
function startAction() {
if (app.documents.length) {
__documentsForceCloseOther(getCustomNumbers(inputDocuments.text, app.documents, true));
}
win.close();
}