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

Sorting Layers

Guest
Jun 21, 2011 Jun 21, 2011

Copy link to clipboard

Copied

I have a map of the US that is broken down into counties.  Each state has its own layer and the counties are sublayers.  I am currently going through and naming all of the counties, but I need to be able to alphabetize them.  Is there a way to do this?

Thanks

TOPICS
Scripting

Views

8.6K

Translate

Translate

Report

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
Adobe
Contributor ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Hi

I tried to do with scripting. This works on Illustrator CS4 OSX 10.6.7

[before]

D layer

C layer

B layer

..b sublayer

..a sublayer

..c sublayer

A layer

[after]

A layer

B layer

..a sublayer

..b sublayer

..c sublayer

C layer

D layer

#target "Illustrator"

/**

* sort layers (with sublayer)

*/

var doc = app.documents[0];

var lay = doc.layers;

var array_primary = [];

for (var i=0, iL=lay.length; i < iL ; i++) {

     if (lay.layers.length === 0) {

          array_primary.push( lay.name );

     }

     else {

          array_primary.push( lay.name );

          var array_secondary = [];

          for (var j=0, jL=lay.layers.length; j < jL ; j++) {

               array_secondary.push(lay.layers.name);

          };

          var result2 = array_secondary.sort( function (a,b) { return a > b });

          // sort sublayers

          sort_layer (lay, result2);

     }

};

var result1 = array_primary.sort( function(a,b) { return a > b } );

// sort layers

sort_layer (doc, result1);

function sort_layer (obj, array) {

     for (var ri=0, riL=array.length; ri < riL ; ri++) {

          obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );

     };

}

thank you

mg.

Votes

Translate

Translate

Report

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
Contributor ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Sorry, this script doesnt work with document include
  • Locked Layer
  • Hidden Layer

Please unlock and show the layers to run

thank you

mg.

Votes

Translate

Translate

Report

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
Guest
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Thanks for getting that to me. I am new to adobe scripting, can you walk methrough how to execute this?

Votes

Translate

Translate

Report

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
Guru ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Locate the creative suite 'ExtendScript Toolkit' app… On the mac this is installed with the suite under the 'Utilities' folder 'comm+shift+u' Not sure on the PC. Anyhow open it and paste the syntax posted into a new empty file. Save this as 'YourScriptName.jsx' the file extension is fairy important. Once saved move the file into illustrator's Presets/Scripts folder that should be in your main app's folder… Quit and restart your app it should now show up in the list of available scripts… You could also choose to test the script in the toolkit but thats up to you…

Votes

Translate

Translate

Report

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
Guest
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

It said error 23: & does not have a value.  Line 19

Votes

Translate

Translate

Report

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
Guru ,
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Try this…

#target "Illustrator" /*      sort layers (with sublayer) */ var doc = app.documents[0]; var lay = doc.layers; var array_primary = []; for (var i=0, iL=lay.length; i < iL ; i++) {      if (lay.layers.length === 0) {           array_primary.push( lay.name );      }      else {           array_primary.push( lay.name );           var array_secondary = [];           for (var j=0, jL=lay.layers.length; j < jL ; j++) {            array_secondary.push(lay.layers.name);           };           var result2 = array_secondary.sort( function (a,b) { return a > b });           // sort sublayers           sort_layer (lay, result2);      } }; var result1 = array_primary.sort( function(a,b) { return a > b } ); // sort layers sort_layer (doc, result1); function sort_layer (obj, array) {      for (var ri=0, riL=array.length; ri < riL ; ri++) {           obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );      }; }

It looks like some error has happen when posting in the forum… The '&nbs p;' is NOT wanted…

Votes

Translate

Translate

Report

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
Guest
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

Now I get this message  Error25: expected: ). Line22  Var result2 = array_secondary.sort(function (a,b) {return a > b});

Votes

Translate

Translate

Report

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
Guest
Jun 22, 2011 Jun 22, 2011

Copy link to clipboard

Copied

I also got an illegal use of the word return

Votes

Translate

Translate

Report

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
Explorer ,
Aug 01, 2019 Aug 01, 2019

Copy link to clipboard

Copied

This worked, however it places capitalized letters first in the sorting order for some reason. Can this be resolved and maintain sublayers as OP requested?  Very helpful thank you!!

For example if I have

A layer

C layer

b layer

D layer

the result is:

A layer

C Layer

D layer

b layer

Votes

Translate

Translate

Report

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
Advocate ,
Aug 03, 2019 Aug 03, 2019

Copy link to clipboard

Copied

LATEST

Salut!

var doc = activeDocument;
var lays = doc.layers;
var array_primary = [];
var array_secondary  = [];
var nom;
    for (var i = 0; i < lays.length; i++) {
    nom = lays.name;
      array_primary.push( [nom.toUpperCase(),nom]);
      if (lays.layers.length != 0) {
            for (var j = 0; j < lays.layers.length; j++) {
              nom = lays.layers.name;
              array_secondary.push([nom.toUpperCase(),nom]);
            }
            // sort sublayers
            sort_layer(lays, array_secondary.sort());
      }
    }
  // sort layers
  sort_layer(doc, array_primary.sort());

//--------------
function sort_layer (Relativeobj, tabs) {
    for (var ri = 0; ri < tabs.length ; ri++) {
        Relativeobj.layers[tabs[ri][1]].zOrder(ZOrderMethod.SENDTOBACK);
    }
}

LR

Votes

Translate

Translate

Report

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