4 Replies Latest reply on Apr 20, 2009 4:05 PM by joostvdsteen

    Find/Change random scripting

    Level 1

      Hi,

       

      I've never used InDesign scripting before and was looking for an find/change script which I found it's a standard sample script that makes it possible for me to change for example "aa" to "aå". What I want now is to make the script random.

      So if there are multible double a's in the text it some times changes the "aa" to "aå" but other times it changes to "åa" is there a way to edit this sample or to make a script my self, where do I have to start??

       

      Thanks in advance

        • 1. Re: Find/Change random scripting
          [Jongware] Most Valuable Participant

          I don't know what sample you were thinking of, but it's quite likely not meant to change items at random. That's a rare thing to ask Mind explaining why?

           

          In any case, the following script (CS3 or newer!) does the trick. The findText function is fed with the correct parameters: the text to be found and case sensitive on, so it will not find 'Aa' or 'aA' or 'AA'. Since the findText function returns an array (rather than 'a next item', one by one), we can loop over the found items and change either its first character or its second. We do that simply by setting either character #0 or character #1 of its contents to the a-ring, leaving the other character alone. The backward loop is to prevent messing up the document, should any of the code change the text.

           

          The math construction returns a random value between 0 and 2 inclusive, where 0 changes the first character, 1 the 2nd, and 2 does nothing (left out for brevity).

           

          app.findChangeTextOptions.caseSensitive = true;
          app.findTextPreferences = NothingEnum.nothing;
          app.findTextPreferences.findWhat = "aa";

          foundItems = app.activeDocument.findText();
          for (var i=foundItems.length-1; i>=0; i--)
          {
          switch (Math.floor(Math.random()*3))
          {
            case 0: foundItems[i].contents[0] = 'å'; break;
            case 1: foundItems[i].contents[1] = 'å'; break;
          }
          }

          Note: untested!

          • 2. Re: Find/Change random scripting
            Level 1

            Hi there,

             

            Thanks for the quick reply. And it sure is a rare thing to ask. I'm 

            working on an art project where double characters in a text shouldnt 

            look te same so by changing the type face in the right way and using a 

            script like this it should work out perfectly but...

            The script doesn't work I changed it to:

             

            app.findChangeTextOptions.caseSensitive = true;

            app.findTextPreferences = NothingEnum.nothing;

            app.findTextPreferences.findWhat = "aa";

            foundItems = app.activeDocument.findText();

            for (var i=foundItems.length-1; i>=0; i--)

            {

            switch (Math.floor(Math.random()*3))

            {

               case 0: foundItems+.contents[0] = 'å'; break;

               case 1: foundItems+.contents[1] = 'å'; break;

            }

            }

             

            But i get an error:

             

            Javascript Error!

             

            Error Number: 46

            Error String: 0 is read only

            Line: 9

            Source: case 0: founditems+.contents[0] = 'å';

            break;

             

            Don't know for sure but with some testing I found out it is talking 

            about the second 0 in that line. The one in the contents[0].

            What can I do?

             

            Best, Joost

            • 3. Re: Find/Change random scripting
              [Jongware] Most Valuable Participant

              Goeiemorgen,

               

              yeah, I just tried it and got the same error. It seems you cannot change a 'contents' array one character at a time (no doubt there is a perfect logical reason). I tried some random things, and settled for this:

               

                 case 0: foundItems[i].contents = 'aå'; break;
                 case 1: foundItems[i].contents = 'åa'; break;

              i.e., entirely replacing the contents with a new string. These two lines should be replaced in my original script.

              As it happens, I already showed you have to do the text change from last to first. You can see why if you replace the contents not with 2 characters again, but with only one -- or, for that matter, with the string 'howdidoody'.

              • 4. Re: Find/Change random scripting
                Level 1

                Great thanks!