2 Replies Latest reply on Sep 17, 2012 5:43 AM by thor2112

    [JS] Finding layer and ignoring case

    thor2112 Level 1

      Hello,

       

      I need to get pageItems from a specific InDesign layer with javascript.  The problem is, the layer name isn't always the same case.  For example, sometimes it's "artwork", or it can be "Artwork", or it can be anything in-between.

       

      I found this works if it's all capped:

       

      app.activeDocument.layers.item('ARTWORK').pageItems;

       

      I was hoping there would be a way like so, but it doesn't work:

       

      app.activeDocument.layers.item(/ARTWORK/i).pageItems;

       

      Is the only way to loop through all the layers beforehand to find the correct character case?

       

      Thanks.

        • 1. Re: [JS] Finding layer and ignoring case
          -hans- Level 4

          Hi,

           

           

          layersstring = app.activeDocument.layers.everyItem().name.join('');
          getIndex = layersstring.toUpperCase().indexOf('ARTWORK');
          app.activeDocument.layers.itemByName(layersstring.slice(getIndex, getIndex+7)).pageItems;
          

           

          should also work.

           

          Guess my choice would be a 'normal' loop ....

           

           

          Hope it'll work

           

          Hans Gerd Claßen

          • 2. Re: [JS] Finding layer and ignoring case
            thor2112 Level 1

            Hi Hans,

             

            Thank you very much for your reply.

             

            That's very clever, and definitely works.  Thanks for sharing.  :-)

             

            I agree too - I'm thinking the loop would be best.  I ended up writing this:

             

            ---------------------------------------------

            $.writeln(findLayer(app.activeDocument.layers, 'ARTWORK').name);

             

            function findLayer(layerArray, capName) {

                for (var i = 0; i < layerArray.length; i++) {

                    var iLayer = layerArray[i];

             

             

                    if (iLayer.name.toUpperCase() === capName) {

                        return iLayer;

                    }

                }

             

                throw ('"' + capName + '" layer not found!');

            }

            ---------------------------------------------

             

            Thanks!