-
Notifications
You must be signed in to change notification settings - Fork 0
/
orphan_remove.js
249 lines (206 loc) · 8.17 KB
/
orphan_remove.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
// <copyright file="orphan_remove.js">
// Copyright (c) Michał Katański 2015. All Right Reserved
// </copyright>
//
// <author>Michał Katański</author>
// <date>2015-04-28</date>
// <summary>Script for InDesign CS6 - CC 2014. Removes orphans from text</summary>
#targetengine "session"
//________________________________________________________________________________________________
// GLOBAL VARIABLES
var config = {
scriptTitle: 'Remove Orphans v.0.2.3',
UIText: {
mainWindow: {
footerText: 'https://github.com/mkatanski/Remove-Orphans-Script',
dropLabel: 'Text language: '
},
progressWindow: {
message: 'Processing text... please wait.'
},
finalWindow: {
Title: 'Script finished successfully!',
noSelection: 'Orphans has been removed in selected elements.',
selection: 'Orphans has been removed in whole document.'
},
general: {
okBtn: 'OK',
cancelBtn: 'Cancel',
closeBtn: 'Close',
noDocError: 'No open document found. Script terminated.',
wrongAppError: 'Invalid application. Script terminated.'
}
},
allowedApplications: ['Adobe InDesign'],
allowedElementTypes: [
'Cell', 'Character', 'Column', 'Line', 'Paragraph', 'Row', 'Story', 'Table', 'Text',
'TextColumn', 'TextFrame', 'TextStyleRange', 'TextPath', 'Word'
],
chars: {
"Polish": ['a', 'i', 'o', 'u', 'w', 'z', 'A', 'I', 'O', 'U', 'W', 'Z'],
"Czech": ['a', 'i', 'k', 'o', 's', 'u', 'v', 'z', 'A', 'I', 'K', 'O', 'S', 'U', 'V', 'Z']
}
};
//________________________________________________________________________________________________
// HELPERS & PROTOTYPES
/**
* Get element index from array
*
* @param element element to find
* @returns {number} index of element or -1 if not exists
*/
Array.prototype.indexOf = function (element) {
var length = this.length;
for (var i = 0; i < length; i++) {
if (this[i] === element) {
return i;
}
}
return -1;
};
//________________________________________________________________________________________________
// CORE METHODS
/**
* Append text frames objects to selection list
*
* @param curObject to seek for text frames objects
* @param length number of text frames objects in document
* @param selectedElements array of selected elements
*/
function appendTextFramesToSelection(curObject, length, selectedElements) {
for (var tfIndex = 0; tfIndex < length; tfIndex++) {
if (curObject.textFrames[tfIndex].contents.length > 1) {
selectedElements.push(curObject.textFrames[tfIndex]);
}
}
return selectedElements;
}
/**
* If application is not allowed or there is no open document terminate script
*/
function checkCurrentApplication() {
if (config.allowedApplications.indexOf(app.name) === -1) {
alert(config.UIText.general.wrongAppError);
exit();
}
if (app.documents.length === 0) {
alert(config.UIText.general.noDocError);
exit();
}
}
/**
* Get all selected text elements
*/
function getSelectedElements() {
var selectedElements = [],
selectionLength = app.selection.length;
for (var x = 0; x < selectionLength; x++) {
var elementType = app.selection[x].constructor.name,
selContentsLength = app.selection[x].contents.length,
appSelection = app.selection[x];
if (config.allowedElementTypes.indexOf(elementType) > -1 && selContentsLength > 1) {
selectedElements.push(appSelection)
}
if (elementType == 'Group') {
var textFramesLength = appSelection.textFrames.length,
groupsLength = appSelection.groups.length;
selectedElements = appendTextFramesToSelection(appSelection, textFramesLength, selectedElements);
for (var gIndex = 0; gIndex < groupsLength; gIndex++) {
selectedElements = appendTextFramesToSelection(
appSelection.groups[gIndex],
textFramesLength,
selectedElements);
}
}
}
return selectedElements;
}
function buildGreps() {
var charsLength = config.selectedChars.length,
greps = [];
for (var charIndex = 0; charIndex < charsLength; charIndex++) {
var cchar = config.selectedChars[charIndex];
greps.push({
find: '\\<' + cchar + '\\s+',
changeTo: cchar + '~S'
});
}
return greps;
}
function doCompute() {
var finishWindowMessage = '',
progressWindow = createProgressWindow(),
selectedElements = getSelectedElements(),
selElementsLength = selectedElements.length,
greps = buildGreps();
progressWindow.show();
for (var i = 0; i < greps.length; i++) {
app.findGrepPreferences.findWhat = greps[i].find;
app.changeGrepPreferences.changeTo = greps[i].changeTo;
app.changeGrepPreferences.noBreak = false;
if (selElementsLength > 0) {
// apply to only selected elements
for (var elemIndex = 0; elemIndex < selElementsLength; elemIndex++) {
selectedElements[elemIndex].changeGrep();
}
finishWindowMessage = config.UIText.finalWindow.selection;
} else {
// apply to document
app.activeDocument.changeGrep();
finishWindowMessage = config.UIText.finalWindow.noSelection;
}
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
}
progressWindow.hide();
createFinishWindow(finishWindowMessage).show();
}
//________________________________________________________________________________________________
// UI CONSTRUCTORS
function createMainWindow() {
var languages = [];
for (var key in config.chars) {
languages.push(key);
}
var window = new Window('window', config.scriptTitle, undefined, {closeButton: false});
window.alignChildren = 'right';
var mainGroup = window.add('group');
mainGroup.add('statictext', undefined, config.UIText.mainWindow.dropLabel);
var group = mainGroup.add('group {alignChildren: "left", orientation: "stack"}');
var list = group.add('dropdownlist', [0, 0, 240, 20], languages);
list.minimumSize.width = 220;
list.selection = 0;
var buttons = window.add('group');
okButton = buttons.add('button', undefined, config.UIText.general.okBtn, {name: 'ok'});
cancelButton = buttons.add('button', undefined, config.UIText.general.cancelBtn, {name: 'cancel'});
var footer = window.add('group');
footer.add('statictext', undefined, config.UIText.mainWindow.footerText);
cancelButton.onClick = function () {
window.hide();
};
okButton.onClick = function () {
config.selectedChars = config.chars[list.selection];
window.hide();
doCompute();
};
return window;
}
function createProgressWindow() {
var window = new Window('palette', config.scriptTitle, undefined, {closeButton: false});
window.add('statictext', undefined, config.UIText.progressWindow.message);
return window;
}
function createFinishWindow(message) {
var window = new Window('dialog', config.scriptTitle, undefined, {closeButton: true});
window.add('statictext', undefined, config.UIText.finalWindow.Title);
window.add('statictext', undefined, message);
closeBtn = window.add('button', undefined, config.UIText.general.closeBtn, {name: 'close'});
closeBtn.onClick = function () {
window.hide();
};
return window;
}
//________________________________________________________________________________________________
// SCRIPT CONSTRUCTOR
checkCurrentApplication();
createMainWindow().show();