-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathTrimOpenEnds.jsx
executable file
·194 lines (173 loc) · 5.25 KB
/
TrimOpenEnds.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
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
/*
TrimOpenEnds.jsx for Adobe Illustrator
Description: Removes the ends of open paths up to their intersection points
Date: January, 2023
Modification date: February, 2023
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
0.1.1 Added undo action on error
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
if (!isCorrectEnv('version:16', 'selection')) return;
var paths = getPaths(selection);
if (!paths.length) return;
addIntersectPoints();
if (selection.length !== paths.length) {
selection = null;
app.redraw();
alert('The paths problem\n'
+ 'Due to the Illustrator error, resulting paths get split.\n'
+ 'Please try to change or move the paths slightly before run script.', 'Script Error');
app.undo();
return;
}
for (var i = selection.length - 1; i >= 0; i--) {
var item = selection[i],
arr = get(selection);
arr.splice(i, 1);
var otherPts = getPoints(arr);
rmvPoints(item, otherPts);
}
}
// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Wrong app version\nSorry, script only works in Illustrator v.' + rqdVers + ' and later', 'Script error');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Nothing selected\nPlease, select two or more paths', 'Script error');
return false;
}
break;
}
}
return true;
}
// Get paths from selection
function getPaths(coll) {
var out = [];
for (var i = 0; i < coll.length; i++) {
var item = coll[i];
if (isType(item, 'group') && item.pageItems.length) {
out = [].concat(out, getPaths(item.pageItems));
} else if (isType(item, '^path') && item.stroked && item.strokeWidth > 0) {
item.filled = false;
out.push(item);
} else {
item.selected = false;
}
}
return out;
}
// Check the item typename by short name
function isType(item, type) {
var regexp = new RegExp(type, 'i');
return regexp.test(item.typename);
}
// Convert collection into standard Array
function get(coll) {
var out = [];
for (var i = 0, len = coll.length; i < len; i++) {
out.push(coll[i]);
}
return out;
}
// Get path anchors
function getPoints(coll) {
var out = [];
for (var i = 0, len = coll.length; i < len; i++) {
var item = coll[i];
if (!isType(item, '^path')) continue;
var pp = item.pathPoints;
for (var j = 0, jlen = pp.length; j < jlen; j++) {
out.push(pp[j].anchor);
}
}
return out;
}
// Add intersection points to lines
function addIntersectPoints() {
app.executeMenuCommand('group');
app.executeMenuCommand('Make Planet X');
selection[0].translate(0, 0); // Update view
app.executeMenuCommand('Expand Planet X');
try {
app.executeMenuCommand('ungroup');
app.executeMenuCommand('ungroup');
} catch (err) {}
}
// Remove start and end points
function rmvPoints(item, pts) {
if (!isType(item, '^path')) return;
var len = item.pathPoints.length,
newPP = [],
i = 0;
for (i = 0; i < len; i++) {
var pt = item.pathPoints[i];
if (pts.length && compareCoord(pt.anchor, pts)) {
newPP.push(i); // Add new point index
}
}
var nLen = newPP.length;
if (!nLen) return;
for (i = len - 1; i >= 0; i--) {
// For two paths remove only points at the end
if (i < newPP[0] || (nLen > 1 && i > newPP[nLen - 1])) {
item.pathPoints[i].remove();
}
}
}
// Find coordinates match
function compareCoord(p, coord) {
for (var i = 0, len = coord.length; i < len; i++) {
if (p[0].toFixed(2) == coord[i][0].toFixed(2)
&& p[1].toFixed(2) == coord[i][1].toFixed(2)) {
coord.splice(i, 1);
return true;
}
}
return false;
}
// Run script
try {
main();
} catch (err) {}