Skip to main content
joet082964
Inspiring
Nov 7, 2017 4:49 PM UTC
Answered

Layer Item Order

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.

This topic is closed to new replies. Start a new post to keep the conversation going.
Marc Autret
Marc AutretCorrect answer
Legend
Nov 7, 2017 7:42 PM UTC

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

5 replies

Marc Autret
Marc AutretCorrect answer
Legend
Nov 7, 2017 7:42 PM UTC

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

Translate
joet082964
Inspiring
Nov 7, 2017 7:48 PM UTC

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?

Translate
Marc Autret
Legend
Nov 7, 2017 8:38 PM UTC

I can replace "Layer1" with whatever layer name?

Yes, that's the way it is supposed to work.

Marc

Translate