2 Replies Latest reply on Jun 22, 2017 11:41 PM by jb_alvarado

    app.selection[0] as variable cause error

    jb_alvarado Level 1

      Hello all,

       

      I have here a test function and I don't know why Indesign brings here a error:

       

      function textBlock(myObject) { 
          app.changeGrepPreferences = NothingEnum.nothing; 
          app.findGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences.findWhat = "\\[(.*)\\]";
          app.changeGrepPreferences.changeTo = "$1";
          numberList = myObject.changeGrep();
      }

      var selText = app.selection[0];

      for (var x = 0; x < selText.paragraphs.length; x++) {
          textBlock(selText.paragraphs.item(x));
      }

      When I remove the variable: selText and use directly app.selection[0] everything works correct.

      Have you an Idea why this is happen? To find this out I spend hours...

       

      In my real script I call a function with:

       

      myProcessText(app.selection[0]);

       

      function myProcessText(selText) {

           // do something ...

      }

       

      That is why I want to work with the variable.

        • 1. Re: app.selection[0] as variable cause error
          Marc Autret Level 4

          Hi jb,

           

          The problem comes from the fact that the find/change function alters the selection. So your selText variable—which is based on the original selection—becomes invalid as soon as its internal indices no longer match the current text range. In your loop, this likely happens when textBlock returns from its very first call (x==0). You could check my hypothesis using alert(selText.isValid), which I suspect to prompt false.

           

          The above issue seems to magically disappear when you explicitly reuse app.selection[0] rather than selText. That's because app.selection is a command that forces InDesign to recalculate the selection each time the interpreter meets it, so it takes into account changes introduced by the latest find/change.

           

          However this is a terribly time-consuming approach. Since you want to process every paragraph associated to the original text selection, why don't you just pass app.selection[0].paragraphs.everyItem() to your textBlock function? Something like:

           

          function textBlock(tx)
          { 
              app.changeGrepPreferences = NothingEnum.nothing;  
              app.findGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences.findWhat = "\\[(.+)\\]";
              app.changeGrepPreferences.changeTo = "$1";
              numberList = tx.changeGrep();
          }
          
          var selText = app.selection[0]; // some text selected
          
          textBlock(selText.paragraphs.everyItem());
          

           

          The key idea is, since the changeGrep method is supported by the Paragraph object, you may call it from a plural paragraph as well.

           

          [There are cases where this trick doesn't perfectly work, depending on the regular expression in use. Find/replace is not 100% safe when working on plural text specifiers.]

           

          @+

          Marc

          3 people found this helpful
          • 2. Re: app.selection[0] as variable cause error
            jb_alvarado Level 1

            Thank you Mark for your very good and helpful explanation! This script snip is part of my very fist indesign script, and yesterday I was getting crazy about this. Because I always thought my paragraph index is not changing, so way I get a invalid object. But now it makes totally sense!

             

            The everyItem commend I didn't know before, I will take a look in that to. But now with your point about time consuming operation I thought more in depth about my script, and many of my grep search/replace functions can work just with the hole selection and not per paragraph. I only need some text formats based on paragraphs. So this I will change.

             

            Regrads

            Jonathan