Sorting objects by number of points
Copy link to clipboard
Copied
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.
Explore related tutorials & articles
Copy link to clipboard
Copied
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;
};
Copy link to clipboard
Copied
I am, unfortunately, not a scripter. IIRC Organize is a Java script which I know even less about.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Arranging the object front to back, least points to most points, and then moving them along the X axis would be great. Putting them in grid would be even better as with a thousand or so objects spacing them out goes quickly into the overflow areas. In previous incanations I would sort by name and then go through various gyrations to get everything in rows. Right now my objects & layers are sorted like this.
Copy link to clipboard
Copied
In your layers panel, you have many layers, with a handfull of items on each layer. So what would the script be sorting? The layers, or the items while keeping them on the layer? Also, out of curiosity: why are you sorting by number of path points? Looks like an interesting project!
(Maybe show a screenshot of before sorting and after sorting to really show us.)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
As a note all the objects are the same area. Thanks to another script. This makes their height/width vary from object to object.

