/
Remove Breaks.js
45 lines (36 loc) · 1.21 KB
/
Remove Breaks.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
// ----------- Remove Breaks ----------- //
/*
Remove all the line breaks (Command+Return on Mac, Ctrl+Return on PC) in a text frame
Very useful for when you've been manually balancing a paragraph and it looks worse than when you started
*/
function main() {
var hasErrors = false,
selections = app.selection;
for (var i = 0; i < selections.length && !hasErrors; i++) {
var textFrame = selections[i] instanceof InsertionPoint ?
selections[i].parentTextFrames[0] :
selections[i];
var frameHasErrors = isError(textFrame);
hasErrors = hasErrors || isError(textFrame);
if (!frameHasErrors) {
textFrame.contents = removeBreaks(textFrame);
};
}
if (hasErrors) alert('Please select some text frames and try again');
}
function isError(obj) {
if (!(obj instanceof TextFrame)) {
return true;
}
return false;
}
// replaces the first space with a line break
function removeBreaks(textFrame) {
return textFrame.contents.trim().replace(/\n/g, '');
}
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
main();