Hey Bryan,
The script is used to group page items on a line. You can adjust the threshold however you may need. This gets the selected page items in the document and sorts them by their vertical position on the page. It then loops through all the selected page items, and for each page item, it checks if it should be added to a new group or not. If the page item is the first in a new group or if it is more than 18 points away from the previous page item in the group, then it creates a new group. Otherwise, it adds the page item to the current group. At the end of the loop, all the selected page items should be organized into groups based on their vertical position on the page.
// Set the Y coordinate threshold for grouping page items
var threshold = 18;
// Get the active document
var doc = app.activeDocument;
// Get the selected page items
var selectedPageItems = doc.selection;
// Sort the selected page items by their Y coordinate
selectedPageItems.sort(function(a, b) {
var bottomLeftPointA = a.geometricBounds[3];
var bottomLeftPointB = b.geometricBounds[3];
return bottomLeftPointA - bottomLeftPointB;
});
// Create a variable to store the current group
var currentGroup = null;
// Loop through all selected page items
for (var i = 0; i < selectedPageItems.length; i++) {
var pageItem = selectedPageItems[i];
// Get the bottom-left point of the page item
var bottomLeftPoint = pageItem.geometricBounds[3];
// Check if the page item should be added to a new group
if (currentGroup == null || bottomLeftPoint - currentGroup.geometricBounds[3] > threshold) {
// Create a new group
currentGroup = doc.groupItems.add();
}
// Add the page item to the current group
pageItem.move(currentGroup, ElementPlacement.PLACEATEND);
}
This doesn't solve your issue but may lead to a way that can integrate your previous script into this one. I could see this being done by applying your script to each group(line) in your selection, but I can not figure out how to get that loop to work. This is similar to @femkeblanco' script that @m1b pointed out, but I would like to see their take on this!