Skip to main content
joet082964
Inspiring
November 7, 2017
Answered

Layer Item Order

  • November 7, 2017
  • 5 replies
  • 1063 views

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 has been closed for replies.
Correct answer Marc Autret

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
November 7, 2017

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

joet082964
Inspiring
November 7, 2017

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?

Marc Autret
Legend
November 7, 2017

I can replace "Layer1" with whatever layer name?

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

Marc