Skip to main content
Known Participant
November 27, 2022
Question

Sorting objects by number of points

  • November 27, 2022
  • 1 reply
  • 1751 views

Does anybody know of a script that will sort objects, compound and otherwise, by the number of points on the path? I know I can get the information from the Document Info>Objects, which I could then manually insert into the object name and sort the names, etc. I was hoping for something along the lines of the Organize script by John Wundes where it would arrange them high to low or some such.

This topic has been closed for replies.

1 reply

m1b
Community Expert
Community Expert
November 27, 2022

Hi @kylwell, you sound like you are a scripter? I've just written a quick script that sorts page items by number of path points. Perhaps you can adapt this to your needs? - Mark

var doc = app.activeDocument,
    items = [];

for (var i = 0; i < doc.pageItems.length; i++)
    if (doc.pageItems[i].parent.constructor.name !== 'CompoundPathItem')
        items.push(doc.pageItems[i]);

items.sort(sortByPathPoints);

// items array is now sorted by path point count
// ... do something?


/**
 * Sort by path points
 * @param {any PageItem} a - an Illustrator page item.
 * @param {any PageItem} b - an Illustrator page item.
 * @returns {Number}
 */
function sortByPathPoints(a, b) {
    return countPathPoints(a) < countPathPoints(b);
};


/**
 * Returns the number of path points of the item.
 * @param {PageItem} item - an Illustrator page item.
 * @returns {Number}
 */
function countPathPoints(item) {

    var count = 0;

    try {

        if (
            item != undefined
            && item.constructor.name == 'CompoundPathItem'
        )
            for (var i = 0; i < item.pathItems.length; i++)
                count += item.pathItems[i].pathPoints.length;

        else if (item.pathPoints != undefined)

            count += item.pathPoints.length;

    } catch (error) { }

    return count;

};
kylwellAuthor
Known Participant
November 28, 2022

I am, unfortunately, not a scripter. IIRC Organize is a Java script which I know even less about.

kylwellAuthor
Known Participant
November 29, 2022

2 screen shots here. One of the previous poster with objects tagged with a date then sorted alpha-numerically. That's the color one. Then where I am now. I'll happily drop everything into a single layer to get them sorted by the number of points for each object. This is an attempt to sort based on complexity. More points, shorter path length = more complex. The highest number of points I've found is about 22k. Lowest is 43.


As a note all the objects are the same area. Thanks to another script. This makes their height/width vary from object to object.