-
Notifications
You must be signed in to change notification settings - Fork 3
/
Add Break to Second to Bottom.js
64 lines (52 loc) · 1.46 KB
/
Add Break to Second to Bottom.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
// ----------- Add Break to Second to Bottom ----------- //
/*
Places a break before the second-to-last word in a frame.
Before:
_________________
| |
| I'm sorry |
|about the wait.|
| |
|_______________|
After:
_________________
| |
| I'm sorry |
| about |
| the wait. |
|_______________|
*/
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];
hasErrors = isError(textFrame);
if (!hasErrors) {
textFrame.contents = addBreakEnd(textFrame.contents);
};
}
}
function isError(obj) {
if (!(obj instanceof TextFrame)) {
alert('Please select some text frames and try again');
return true;
}
return false;
}
// replaces the last space with a line break
function addBreakEnd(str) {
str = str.trim();
var arr = str.split(' ');
return arr.length > 1 ?
arr.slice(0, arr.length - 2).join(' ') + '\n' + arr.slice(-2).join(' ') :
str;
}
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
main();