Try this script, it works by joining paths from end point to start point, but not with paths which have the same starting and ending points. However, because joining paths changes the path count because it merges two paths into one, the start/end points are subject to change during this "connect-the-dots" algorithm.
#target illustrator
function test(){
function overlapPointExists(){
var doc = app.activeDocument, thisPath, firstPt, lastPt, nextPath, firstPt_2, lastPt_2;
for(var i=0; i < doc.pathItems.length; i++){
thisPath = doc.pathItems[i];
if(!thisPath.closed){
firstPt = thisPath.pathPoints[0];
lastPt = thisPath.pathPoints[thisPath.pathPoints.length - 1];
for(var j=0; j < doc.pathItems.length; j++){
if(i != j){
nextPath = doc.pathItems[j];
firstPt_2 = nextPath.pathPoints[0];
lastPt_2 = nextPath.pathPoints[nextPath.pathPoints.length - 1];
if(lastPt.anchor[0] == firstPt_2.anchor[0] && lastPt.anchor[1] == firstPt_2.anchor[1]){
lastPt.selected = PathPointSelection.ANCHORPOINT;
firstPt_2.selected = PathPointSelection.ANCHORPOINT;
return true;
}
}
};
}
};
doc.selection = null;
return false;
};
while(overlapPointExists()){
app.executeMenuCommand("join");
app.activeDocument.selection = null;
}
app.activeDocument.selection = null;
};
test();
EDIT:
The script was way too slow when there weren't just a few paths in the document.
So here is one which will work based on a selection of paths. It works by you having your separate and ungrouped paths selected at first, then it groups them into a temporary group (stacking order may change if the items aren't in consecutive order), and it uses that group as an isolation container to act only on paths therein. After it runs through, the group gets ungrouped and an alert box tells you how long it all took. Comment out or remove this alert when it gets annoying.
#target illustrator
function test(){
function overlapPointExists(pathCollection){
var doc = app.activeDocument, thisPath, firstPt, lastPt, nextPath, firstPt_2, lastPt_2;
for(var i=0; i < pathCollection.pathItems.length; i++){
thisPath = pathCollection.pathItems[i];
if(!thisPath.closed){
firstPt = thisPath.pathPoints[0];
lastPt = thisPath.pathPoints[thisPath.pathPoints.length - 1];
for(var j=0; j < pathCollection.pathItems.length; j++){
if(i != j){
nextPath = pathCollection.pathItems[j];
firstPt_2 = nextPath.pathPoints[0];
lastPt_2 = nextPath.pathPoints[nextPath.pathPoints.length - 1];
if(lastPt.anchor[0] == firstPt_2.anchor[0] && lastPt.anchor[1] == firstPt_2.anchor[1]){
lastPt.selected = PathPointSelection.ANCHORPOINT;
firstPt_2.selected = PathPointSelection.ANCHORPOINT;
return true;
}
}
};
} else {
return false;
}
};
doc.selection = null;
return false;
};
if(app.activeDocument.selection == null || app.activeDocument.selection.length == 0){
alert("Please select something first.");
return;
}
var doc = app.activeDocument;
var start = (new Date().getTime());
var sel = doc.selection;
app.executeMenuCommand("group");
var pathGroup = sel[0].parent;
pathGroup.selected = false;
while(overlapPointExists(pathGroup)){
app.redraw();
app.executeMenuCommand("join");
doc.selection = null;
}
pathGroup.selected = true;
app.executeMenuCommand("ungroup");
doc.selection = null;
var end = (new Date().getTime());
alert("Process took " + (end - start) / 1000 + " seconds.");
};
test();