Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Combine paths with same X position

Explorer ,
Jun 14, 2020 Jun 14, 2020

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-...

 

 

 

 

 

 

 

 

 

 

TOPICS
Scripting
915
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 14, 2020 Jun 14, 2020

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_"
...
Translate
Adobe
Guide ,
Jun 14, 2020 Jun 14, 2020

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]);
      }
  }
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2020 Jun 14, 2020

Error on line 8 "no such element" - can't see why in the script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2020 Jun 14, 2020

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);
      }
  }
}

 

 

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 14, 2020 Jun 14, 2020

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]);
      }
  }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2020 Jun 14, 2020

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? 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2020 Jun 14, 2020

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;

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2020 Jun 14, 2020

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2020 Jun 14, 2020

isGroupExists, this do nothing :-).

It is just extra variable. Left by mistake.

I have removed that variable and updated the script. 

Best regards
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 14, 2020 Jun 14, 2020
LATEST

Nice. So tidy!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines