4 Replies Latest reply on Jul 17, 2009 3:47 AM by Peter Kahrel

    [JS] Help with syntax

    r_olsen Level 1

      Hi all,


      I'm looping through the links searching for a suffix. My current test doc has 5 links - 2 of these have the suffix. I want to add 3 of these links to an array. When the for loop has finished my array have 5 elements: 3 link objects and 2 undefined. What's causing the undefined?

       

      Thanks.

       

      Rasmus

       

      var oDoc = app.activeDocument;
      var oDocLinks = oDoc.links;
      
      var myLinks = new Array();
      
      for ( var idx = oDocLinks.length-1; idx >= 0; idx-- ) {
          $.writeln ( "Loop: " + oDocLinks[idx].name );
          if ( ! searchSuffix (oDocLinks[idx].name) )
              $.writeln ( "Inside If: " + oDocLinks[idx].name );
              myLinks[idx] = oDocLinks[idx];
      }
      
      $.writeln ( "Length of array:" + myLinks.length );
      
      function searchSuffix ( string ) {
          var oRegExp = new RegExp ( "x1a" );
          return oRegExp.test ( string.toLowerCase() )
      }
      
      exit();
      
        • 1. Re: [JS] Help with syntax
          Peter Kahrel Adobe Community Professional & MVP

          Rasmus,

           

          Your searchSuffix fails because .test() doesn't work the way you wrote it. Instead, try this:

           

          if (oDocLinks[idx].name.search (/x1a$/) > 0)

             myLinks[idx] = oDocLinks[idx];

           

          Peter

          • 2. Re: [JS] Help with syntax
            r_olsen Level 1

            Cheers Peter,

             

            Care to explain the newbie why his RegExp.test function fails?

             

             

            I can't get your regex /x1a$/ to work. Using this:

             $.writeln(oDocLinks[idx].name.toLowerCase().search ( /x1a$/) );
             

             

            the console writes:

            -1
            -1
            -1
            -1
            -1
            

            Skipping the $ modifier ( /x1a/ ) and it finds the suffix. It's strange that the $ modifier won't work?

            -1
            19
            21
            -1
            -1
            
            • 3. Re: [JS] Help with syntax
              Peter Kahrel Adobe Community Professional & MVP

              >Skipping the $ modifier ( /x1a/ ) and it finds the suffix. It's strange that the $ modifier won't work?

               

              Well, you called "x1a" a suffix so I assumed that you want to find it at the end of a string. That the regex works only when you leave out $ suggests that it isn't necessarily string-final (in which case it's not a suffix ).

               

              >Care to explain the newbie why his RegExp.test function fails?

               

              Mm. When I tried it yesterday it didn't work, but while writing up an example now, it does work. Sorry about that. You don't need to construct a new object, by the way: you could just as well use a literal regex like this:

               

              /x1a/.test (string.toLowerCase());

               

              It does exactly the same as what you had, it just saves you some typing. You could make it even shorter like this: /x1a/i.test (string); (the "i" after /x1a/ stands for "ignore case").

               

              Peter

              1 person found this helpful
              • 4. Re: [JS] Help with syntax
                r_olsen Level 1
                Well, you called "x1a" a suffix so I assumed that you want to find it at the end of a string. That the regex works only when you leave out $ suggests that it isn't necessarily string-final (in which case it's not a suffix ).

                 

                 

                In that case I guess you could call my definition of a suffix for a "almost-suffix" - you know, right before the .ext

                 

                [literal regex]

                 

                /x1a/.test (string.toLowerCase());

                 

                It does exactly the same as what you had, it just saves you some typing. You could make it even shorter like this: /x1a/i.test (string); (the "i" after /x1a/ stands for "ignore case").

                 

                Uh such sexy lines, newbie likes it a lot

                 

                BTW Thank you for your book on scripting InDesign. This was a perfect introduction to JS syntax and InDesign object model (although I'm still struggling to understand collections and .everyItem() and .getElements()).

                 

                Cheers