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.

m1b
Community Expert
Community Expert
November 28, 2022

No worries. Well the script does the sorting, but once the items are sorted, what do you want to do with them? Do you mean sort them in layer order? If so, what if they are on different layers? Also, do you want them names for the number of points? It might be possible for me to adjust the script.

- Mark