Copy link to clipboard
Copied
Hello friends,
I am a "non-scriptor" for lack of a better term. I did a forum and Google search but wasn't able to come up with an answer.
I need a script to alphabetize a long list of items on a single layer. For example, in this screenshot, all of these elements are on one layer, where they need to stay. Is it possible to create a script that would re-order those items on that particular layer so that they are alphabetical? Also, I have many other layers, but I only need to be able to run the script on particular layers.
Thanks for your help.

Hi joet,
Ultra-lazy implementation (assuming non-locked items, non-weird names, etc)
...const cleanSort = function(x,y)
{
x = x.toLowerCase();
y = y.toLowerCase();
return x < y ? -1 : +(x!=y);
};
const sortInLayer = function(/*str*/name, o,a,s)
{
o = app.properties.activeDocument;
if( !o ){ alert( "No document." ); return; }
o = o.layers.itemByName(String(name));
if( !o || !o.isValid ){ alert("No layer with that name."); return; }
for
(
(a=(o=o.pageItems).everyItem().na
Copy link to clipboard
Copied
Hi joet,
Ultra-lazy implementation (assuming non-locked items, non-weird names, etc)
const cleanSort = function(x,y)
{
x = x.toLowerCase();
y = y.toLowerCase();
return x < y ? -1 : +(x!=y);
};
const sortInLayer = function(/*str*/name, o,a,s)
{
o = app.properties.activeDocument;
if( !o ){ alert( "No document." ); return; }
o = o.layers.itemByName(String(name));
if( !o || !o.isValid ){ alert("No layer with that name."); return; }
for
(
(a=(o=o.pageItems).everyItem().name).sort(cleanSort) ;
s = a.pop();
o.itemByName(s).bringToFront()
);
};
// Test.
// ---
sortInLayer( "Layer1" );
@+
Marc
Copy link to clipboard
Copied
Thank you Marc. Can I specify which layer that will run on, or is that what line 26 does? If so, I can replace "Layer1" with whatever layer name?
Copy link to clipboard
Copied
I can replace "Layer1" with whatever layer name?
Yes, that's the way it is supposed to work.
Marc
Copy link to clipboard
Copied
Hi Marc Autret,
How does this line work? I thought that you couldn't access the name property on everyItem() because it's a collection rather than an array.
(a=(o=o.pageItems).everyItem().name).sort(cleanSort) ;
Copy link to clipboard
Copied
Hi jakec,
I thought that you couldn't access the name property on everyItem() because it's a collection rather than an array.
everyItem() is not a collection, it's a plural specifier over a collection (pageItems is a collection.)
Now a plural specifier basically behaves like a singular specifier, but when you read a property it returns an array.
That's why pageItems.everyItem().name is an array of strings.
Best,
Marc
Find more inspiration, events, and resources on the new Adobe Community
Explore Now