Copy link to clipboard
Copied
I have a series of vertical line paths across the artboard. Some have one or two lines above each other on the same X position.
What I'd like to do is combine each set of lines that 'line up' above each other as one object. The example has max 2 lines per X position, but I'd like to to it for any number on the same X.
As a bonus I'm looking for a way to select every second path once they are combined by X and reverse the path direction, so they alternate up, down, up... across the page.
test file: link
Thanks to @CharuRajput and @femkeblanco for the script to order the layers: https://community.adobe.com/t5/illustrator/organise-object-layer-order-based-on-object-x-position/m-...
1 Correct answer
Hi,
I have not properly tested. But you can try following snippet that will group all path with same x.
var items = app.activeDocument.pathItems;
for (var i = 0; i < items.length; i++) {
for (var j = items.length - 1; j > i; j--) {
if (truncate(items[i].left, 3) == truncate(items[j].left, 3)) {
try {
var group = app.activeDocument.groupItems["group_" + items[i].left];
} catch (e) {
var group = app.activeDocument.groupItems.add();
group.name = "group_"
...
Explore related tutorials & articles
Copy link to clipboard
Copied
Try this for grouping 2 paths with same X
var items = app.activeDocument.pathItems;
var groups = app.activeDocument.groupItems;
for (var i = 0; i < items.length; i++){
for (var j = items.length - 1; j > i; j--){
if (items[i].left == items[j].left){
var group = groups.add();
group.name = "group" + i;
items[i].moveToEnd(groups[group.name]);
items[j].moveToEnd(groups[group.name]);
}
}
}
Copy link to clipboard
Copied
Error on line 8 "no such element" - can't see why in the script.
Copy link to clipboard
Copied
Hi,
Did you try your script? I am getting an error while running the script at line number 8, I just updated your version
var items = app.activeDocument.pathItems;
for (var i = 0; i < items.length; i++){
for (var j = items.length - 1; j > i; j--){
if (items[i].left == items[j].left){
var group = app.activeDocument.groupItems.add();
group.name = "group" + i;
items[i].moveToEnd(group);
items[j].moveToEnd(group);
}
}
}
Copy link to clipboard
Copied
Sorry. There was an "app." missing. I don't need to make app explicit in CS6.
var items = app.activeDocument.pathItems;
var groups = app.activeDocument.groupItems;
for (var i = 0; i < items.length; i++){
for (var j = items.length - 1; j > i; j--){
if (items[i].left == items[j].left){
var group = groups.add();
group.name = "group" + i;
items[i].moveToEnd(groups[group.name]);
items[j].moveToEnd(groups[group.name]);
}
}
}
Copy link to clipboard
Copied
Thanks Charu & Femke. That works for two items at a time. Any more than two items on the same X are left out. Pretty close!
Running the script again doesn't group existing groups with items with the same .left - it just puts lone items in groups by themselves. I guess a groupItems isn't an item, so it wouldn't work.
If there was a quick way to make all the groups into compound paths, the loop could just be run again as many times as needed until all items with matching .left/x positions were collected into one item each?
Copy link to clipboard
Copied
Hi,
I have not properly tested. But you can try following snippet that will group all path with same x.
var items = app.activeDocument.pathItems;
for (var i = 0; i < items.length; i++) {
for (var j = items.length - 1; j > i; j--) {
if (truncate(items[i].left, 3) == truncate(items[j].left, 3)) {
try {
var group = app.activeDocument.groupItems["group_" + items[i].left];
} catch (e) {
var group = app.activeDocument.groupItems.add();
group.name = "group_" + items[i].left;
}
items[i].moveToEnd(group);
items[j].moveToEnd(group);
}
}
}
function truncate(number, digits) {
number = number * Math.pow(10, digits);
number = Math.round(number);
number = number * Math.pow(10, -digits);
return number;
}
Let us know if this works for you.
Edit : Remove extra line of code which is not required, var isGroupExists = false;
Copy link to clipboard
Copied
Hey it works. Running it once it groups two per X position, then running it again groups the third so there's single paths, groups of two and groups of three. I imagine this would work indefinitely.
I'm just a noob but what does "isGroupExists" do? You've defined it but it isn't implemented anywhere.
Copy link to clipboard
Copied
isGroupExists, this do nothing :-).
It is just extra variable. Left by mistake.
I have removed that variable and updated the script.
Copy link to clipboard
Copied
Nice. So tidy!

