This code is modified from s.h's page : Scripts for Adobe Illustrator CS , please select some path items and test. // Thanks to Hiroyuki Sato // ----------------------------------------------------------------------- // codes copy from: http://shspage.com/aijs/en/#reverse // return the angle of the line drawn from p1 to p2 function getRad(p1, p2) { return Math.atan2(p2[1] - p1[1], p2[0] - p1[0]); } function getPnt(pnt, rad, dis) { return [pnt[0] + Math.cos(rad) * dis, pnt[1] + Math.sin(rad) * dis ]; } function extractPaths(s, paths) { for (var i = 0; i < s.length; i++) { if (s.typename == "PathItem") { paths.push(s); } else if (s.typename == "GroupItem") { // search for PathItems in GroupItem, recursively extractPaths(s.pageItems, paths); } else if (s.typename == "CompoundPathItem") { // searches for pathitems in CompoundPathItem, recursively // ( ### Grouped PathItems in CompoundPathItem are ignored ### ) extractPaths(s.pathItems, paths); } } return paths } // ----------------------------------------------------------------------------- function isEqual(p1, p2) { return p1[0] === p2[0] && p1[1] === p2[1] } function addPointBS(p, beginPoint, endPoint) { var ps = [], end, i; for (i = p.length - 1; i >= 0; i--) { with(p) { ps.unshift([anchor, leftDirection, rightDirection, pointType]); } i > 0 && p.remove(); } // set begin point p[0].rightDirection = p[0].leftDirection = p[0].anchor = beginPoint; // reset original points for (i = 0; i < ps.length; i++) { with(p.add()) { anchor = ps[0]; leftDirection = ps[1]; rightDirection = ps[2]; pointType = ps[3]; } } // modify original first and last points p[1].leftDirection = p[1].anchor; p[p.length - 1].rightDirection = p[p.length - 1].anchor; // set end point end = p.add(); end.rightDirection = end.leftDirection = end.anchor = endPoint; } function Projecting2Butt(pth) { var p = pth.pathPoints, begin = p[0], end = p[p.length - 1], beginAnchor = begin.anchor, beginRD = begin.rightDirection, endAnchor = end.anchor, endLD = end.leftDirection, rad, rad2, beginPoint, endPoint; if (isEqual(beginRD, beginAnchor)) { rad = getRad(p[1].leftDirection, beginAnchor); } else { rad = getRad(beginRD, beginAnchor); } if (isEqual(endLD, endAnchor)) { rad2 = getRad(p[p.length - 2].rightDirection, endAnchor); } else { rad2 = getRad(endLD, endAnchor); } beginPoint = getPnt(beginAnchor, rad, pth.strokeWidth / 2); endPoint = getPnt(endAnchor, rad2, pth.strokeWidth / 2); addPointBS(p, beginPoint, endPoint); } function main() { var paths = extractPaths(app.selection, []), i = 0; for (; i < paths.length; i++) { Projecting2Butt(paths); } } main();
... View more