4 Replies Latest reply on Apr 18, 2016 5:52 AM by subieguy2

    Scripting for layers help.

    andy waterhouse

      please can someone help me? I have an illustrator file with text objects (not strokes) the text objects are numbers 1 to around 300, there are 2 * no.1,        2 * no. 2,         2* no. 3 and so on. This is used for matching numbered flaps up to numbered panels for 3D papercraft.

      the script I need help with involves sorting the numbers (which are all in 1 single layer in a random order) into numerical ascending order.


      his is so I can select numbers, say 1 - 50 (so 100 objects in total) and then add fill colour (red), then select 51- 100 and choose fill colour (blue) and so on, to make finding the numbered flaps and panels easier.

      I know it can be done by manually selecting them, this takes too long.


      please can someone point me in the right direction.




      so far its looking like this, thanks to the users who created the scripts i have found on this site.


      // carlos canto // 09/26/14

      // https://forums.adobe.com/thread/287812 



      var idoc = app.activeDocument;

      var ilayer = idoc.activeLayer;

      for (i=0; i<ilayer.layers.length; i++) {

          var layer = ilayer.layers[i];

          layer.name = layer.pageItems[0].name || layer.pageItems[0].contents;



      that one renames the layers to the object name




      then i run this one

      #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[i].layers.length === 0) {

        array_primary.push( lay[i].name );


        else {

        array_primary.push( lay[i].name );

        var array_secondary = [];

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



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

        // sort sublayers

        sort_layer (lay[i], 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 );





      and that puts them in the correct order


      now i have to select them in the layers panel, I thought I could just press shift while selecting, this is only selecting the layer and not the object, so I cannot change the colour of the object as it is not selected.


      is there a way around this?


      I have thought about editing this file (below) which works for filling the colours with a random swatch colour, I want to change the selection part of the script to incorporate choosing the layers in sequential groups of 10 or 20 then fill them with a colour (don't care what colour)


      can any one help.


      edit : ok I got that part now, collect in new layer







      A Javascript for Illustrator CS2 by James E. Talmage, modified by Harron K. Appleman




      Randomly fills selected paths with swatches already defined in the document, residing in the Swatches palette. HKA modification prevents non-deletable [none] and [registration] swatches from being used for the random fill.




      Make sure the [none] and [registration] swatches are the first two in the Swatches palette. (Swatches can be reordered by click-dragging.) Select one or more paths. Run the script.






      if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0 ){

        doc = app.activeDocument;

        for ( i = 0; i < doc.selection.length; i++ ){



        objectRef.filled = true;

        swatchIndex = Math.round( Math.random() * ( doc.swatches.length - 1 ) );

        if(swatchIndex > 1){

        objectRef.fillColor = doc.swatches[ swatchIndex ].color;



        {i = i-1};