Help - Adobe Illustrator script required

I’m looking for a simple script that will “join nearest points” and “fill with black” in a vector based program (Adobe Illustrator). I get architectural files (floor plans) that require the walls to be joined to form a closed path then filled in. Lines appear to intersect at the corners, but are not. Currently, I selected each corner intersections and “command-J” to join the points. Very tedious. Is it possible to simply “select all” then run a script?

Any help would be greatly appreciated. I’m using Illustrator CS5

Model: iMac Core2 Duo; 3.06 GHz
AppleScript: 1.1.1
Operating System: Mac OS X (10.6)

Speaking as a designer, and not a coder for a second, I don’t think your proposed solution would work. I haven’t used the newer versions of Illustrator, but I’ve spent quite a bit of time in versions up to CS2. From my experience, if you want to join two open ends, you have to have only those two points selected. Points don’t have to be anywhere near each other to be joined. So if you did a “select all” I doubt Illustrator would be smart enough to figure out which points went with each other. Now, if it were possible to confine the selection to two open points within a certain distance from each other (say 0.01 inches), it may be then possible to join them using AppleScript. Although I’ve run into fairly lackluster AppleScript support in CS2. Hopefully by CS5 they’ve improved things a bit in that regard.

My guess is this would be more trouble than it is worth, though it could be done. The first problem is that the join command (in CS4 at least) is not supported, and it is not recordable as an action. You can work around this with GUI scripting:

tell application "System Events"
	set frontmost of process "Adobe Illustrator" to true
	keystroke "j" using command down
end tell

That solved you need to find the paths to join. My guess is that when you start you have a bunch of lines that need to be joined, and very few if any closed paths. You would have to iterate through each path point of each path to find the ones to join and select them. So problem two is how to select the path points, which is done something like this:

tell application "Adobe Illustrator"
	set selection to {page item 1 of document 1, page item 2 of document 1}
	set selected of path point 1 of selection to anchor selected
	set selected of path point 1 of selection to right selected
end tell

Notice that you need to select the paths then set the correct selected property of each point in the path. The point you want to join needs to be anchor selected and the adjacent on needs a left or right selected. I think the others can be none but you cannot start out with none because that deselects the path.

However, each time you join two your list of paths is going to change so you will have to get a new list of the paths since when joining the paths you combine them into one path. This might be solved by iterating backward through a list. But you would still need to get a new list of path points each time you join them since the combined path will have more points than the original one.

In the end I think that with all the calls to Illustrator this would end up being slower than doing it by hand.

thanks everybody for taking the time to explore this.
Seems like it really isn’t worth the effort.
Let’s close the file on this one.


There is an Illustrator script a Japanese developer, Hiroyuki wrote “Join Reasonably” basically joins that overlap each other. I’ve used it a handful of times with success. There are other there which are very useful, check them out…

thanks for this Seth!

Have you tried Concatenate?

If you do it a lot, it’s probably worth the price


thanks Ian.
I’ll take a closer look at this.

Illustrator CS5.1 joins lines based on proximity when you select the paths you want to join. It’s still command J.

I’ve also, in the past, selected the lines I want to join, made it a live paint object, inverted the colour so they are filled, expanded, then inverted the colour again so that the paths are stroked once more, but joined.

Maybe no need for a script!