Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Layer Item Order

Enthusiast ,
Nov 07, 2017 Nov 07, 2017

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.

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Nov 07, 2017 Nov 07, 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().na

...
Translate
Guide ,
Nov 07, 2017 Nov 07, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 07, 2017 Nov 07, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 07, 2017 Nov 07, 2017

I can replace "Layer1" with whatever layer name?

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

Marc

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 08, 2017 Nov 08, 2017

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) ;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 08, 2017 Nov 08, 2017
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines