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

# Group Shapes by Row

Engaged ,
Dec 21, 2022 Dec 21, 2022

Copied

Hello,

Help with Script that will Group the shapes by row.  If you look at the sample file I'm hoping to get a script that will take what is there and result in 4 groups.

Thank you!

TOPICS
Scripting

Views

511

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
10 Replies 10
Guide ,
Dec 21, 2022 Dec 21, 2022

Copied

This was done on the hoof.  It requires the number of rows to be entered.

``````var items = app.activeDocument.pageItems;
var array = [];
for (var i = 0; i < items.length; i++) {
if (items[i].parent.parent == app.activeDocument &&
items[i].typename != "TextFrame") {
array.push(items[i]);
}
}
array.sort(function(a, b) {
return a.left == b.left ? a.top - b.top : a.left - b.left;
});
var n = prompt("Number of rows:");
for (var i = 0; i < n; i++) {
array[i].moveToEnd(group);
for (var j = array.length - 1; j > -1; j--) {
if (array[j] != array[i]) {
var b1 = array[i].geometricBounds;
var b2 = array[j].geometricBounds;
if (b2[1] >= b1[1] && b2[3] < b1[1] ||
b2[1] <= b1[1] && b2[1] > b1[3]) {
array[j].moveToEnd(group);
}
}
}
}``````

Report

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

Copied

I'm not sure if I am doing something wrong with your script but I cannot get it to work.  I enter the number of lines but nothing gets grouped.

Report

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

Copied

Bryan, I tried Femkes approach with a bunch of plain path objects, similar to what you showed in your initial post. The script worked fine.

You may want to provide another sample Illustrator file, so she can take a look and perhaps see what is the reason why it didn't work for you.

Report

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

Copied

Here is a better example

Report

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

Copied

I can't explain why the script would fail silently.  Unfortunately, I can't open your file because I use an old version of AI.

(1) Do you get an error message?

(2) Is at least one new group item added to your layers panel?

(3) You could try adding alert(array.length) before the prompt(); if the alert reads 0, then the problem is in the first half of the script.

Report

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

Copied

Here's another approach... To get the grouping right, I instead sorted the shapes from tallest to shortest, then checked each shape again all others in each group to see if they overlapped at all. If they do, I put them in that group. This may break on some edge cases but works for this particular file. I sent this to @BryanPagenkopf to check some other files so we'll see if it really works. Cheers ✌️

``````var doc = app.activeDocument;
groups = groupObjectsByLine(doc.selection);
if (groups) {
}

function groupObjectsByLine(sel) {
var groups = [];
// sort the selected page items by their height (tallest to shortest)
sel.sort(function (a, b) {
var aHeight = a.geometricBounds[3] - a.geometricBounds[1];
var bHeight = b.geometricBounds[3] - b.geometricBounds[1];
return bHeight - aHeight;
});
// check if each page item shares bounds with others
var item, placed;
while (sel.length > 0) {
item = sel.pop();
placed = false;
for (var i = 0; i < groups.length; i++) {
group = groups[i];
if (overlappingBounds(item, group)) {
item.move(group, ElementPlacement.PLACEATEND);
placed = true;
}
}
// if an item didn't fit into any current groups make a new group
if (!placed) {
groups.push(g);
item.move(g, ElementPlacement.PLACEATEND);
}
}
return groups;
}

function overlappingBounds(item, group) {
var top = item.geometricBounds[1];
var bottom = item.geometricBounds[3];
var gTop = group.geometricBounds[1];
var gBottom = group.geometricBounds[3];
if (bottom > gTop || top < gBottom) {
return false;
}
return true;
}``````

Report

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

Copied

Femke, I saved Bryan's second sample as an Illustrator CS5 file, so you can download and open it in your Illustrator version.

Meanwhile I tried jduncan's approach and as far as I can see it works fine.

Report

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

Copied

Thanks @Kurt Gold.  I tried the file you attached, and the script worked as expected.

Report

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

Copied

@femkeblanco, your script does work. Bryan sent me a version of the script in a direct message and linked to this post so I just assumed they were the same but that was not the case. The script you pasted above does work as advertised. I just wanted to set the record straight. Cheers!

Report

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

Copied

LATEST

Thanks @jduncan