Script to Generate Line Segments but Needs to Avoid Curved Lines and Multiple Line Segments.
Hello there!
I've developed a script to create line segments between adjacent selected objects. However, I'm facing two challenges: I want to eliminate curved segments, and I don't want multiple line segments to be generated on a single straight line with multiple anchor points. I've spent hours trying to solve this problem.

Can anyone provide some assistance? Your help is greatly appreciated!
if (app.documents.length > 0) {
var doc = app.activeDocument;
var strokeColor = new RGBColor();
strokeColor.red = 0;
strokeColor.green = 0;
strokeColor.blue = 0;
var strokeWidth = 0.5;
function areAnchorPointsEqual(point1, point2) {
return point1.anchor[0] === point2.anchor[0] && point1.anchor[1] === point2.anchor[1];
}
var selectedItems = doc.selection;
for (var i = 0; i < selectedItems.length; i++) {
var currentItem = selectedItems[i];
if (currentItem.typename === "GroupItem") {
// If the selected item is a group, iterate through its pageItems
for (var k = 0; k < currentItem.pageItems.length; k++) {
var groupItem = currentItem.pageItems[k];
processItem(groupItem);
}
} else {
processItem(currentItem);
}
}
function processItem(item) {
if (item.typename === "PathItem") {
var pathPoints = item.pathPoints;
for (var j = 0; j < pathPoints.length; j++) {
var startPoint = pathPoints[j];
var endPoint = pathPoints[(j + 1) % pathPoints.length];
if (!areAnchorPointsEqual(startPoint, endPoint)) {
var newLine = doc.pathItems.add();
newLine.setEntirePath([startPoint.anchor, endPoint.anchor]);
newLine.strokeColor = strokeColor;
newLine.strokeWidth = strokeWidth;
}
}
} else if (item.typename === "CompoundPathItem") {
var compoundPathItems = item.pathItems;
for (var l = 0; l < compoundPathItems.length; l++) {
var pathItem = compoundPathItems[l];
var pathPoints = pathItem.pathPoints;
for (var j = 0; j < pathPoints.length; j++) {
var startPoint = pathPoints[j];
var endPoint = pathPoints[(j + 1) % pathPoints.length];
if (!areAnchorPointsEqual(startPoint, endPoint)) {
var newLine = doc.pathItems.add();
newLine.setEntirePath([startPoint.anchor, endPoint.anchor]);
newLine.strokeColor = strokeColor;
newLine.strokeWidth = strokeWidth;
}
}
}
}
}
}
Thanks in Advance!
