3 Replies Latest reply on Feb 18, 2010 12:41 PM by [Jongware]

    [CS3 AS] Change text but only first instance

    fundog9

      Hey there. So I'm trying to do a find / replace of a particular text string that I know appears multiple times within the document. Each instance of the string needs to be replaced by a slightly different string. The string being replaced is "Page #" and needs to be replaced by, for example, "Page 4" on first reference then "Page 6" on next reference.

       

      So, I was trying to figure out a way to replace only the first reference and then iterate the same replacement command. Or if there's a more elegant solution like somehow being able to pass an array of replacements, that would be awesome.

       

      Thoughts?

        • 1. Re: [CS3 AS] Change text but only first instance
          [Jongware] Most Valuable Participant

          (I don't know how to express it in AS, but:)

           

          Don't use ReplaceText. FindText does exactly what you need -- it returns an array of found items. If you have an array of replacements at hand -- and you better make sure it has exactly the same number of item! --, you can loop over the found items and replace them 'manually' with the corresponding one from your replacement list.

          There is a catch: you have to start changing the text from the end of the found text list and work your way back to the start. That's because you are physically changing the text in the InDesign document itself, and that means the indexes of the found text after the one you change are invalidated.

          • 2. Re: [CS3 AS] Change text but only first instance
            fundog9 Level 1

            Can you give me any code samples, even if not in AppleScript, to help me along with that? I know how to get an array of the found objects, but how do I target each and replace the text with what I want?

            • 3. Re: [CS3 AS] Change text but only first instance
              [Jongware] Most Valuable Participant

              Sure. This is what it looks like in Javascript.

              I use a GREP find to locate all number signs that are preceded with "Page " -- in this particular case, you want to find "Page #" but only change the hash mark. (And I missed a perfectly good opportunity to call it by its proper name "octothorp" ...)

               

              The number of found occurrences must exactly match the length of the Replacement list, otherwise you get the wrong numbers at the wrong places -- therefore I added a check before continuing.

               

              Notice I do the actual changing of # to number in reverse order -- this will always work correctly if you are replacing a 'contents' type of thing. The 'logical' way around, from front-to-end, messes up the found items list, because this points to locations inside the text, and if you insert two characters where there was one when it searched, the locations of all next items are off by one -- and so on.

               

              Javascript requires one more thing: the text that is replaced, list[f].contents, is of a type "String", and it does not accept a number, which is what the replacements array contains. Hence the explicit conversion to "String" -- it might not be necessary in AS.

               

              You can translate this to AS, and you can also simply use this Javascript (JS scripts work the same on both Windows and Mac ID). Fill the replacements array with the numbers you have and run the script. If you forgot or added a number, the script will notice this and exit; if you inserted a wrong number it'll never know ... so do check before and after running either Javascript or Applescript!

               

              var replacements = [
                   3, 1, 4, 1, 5, 9, 2, 6, 5
              ];
              
              app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat = "(?<=Page )#";
              list = app.activeDocument.findGrep();
              
              if (list.length != replacements.length)
              {
                   alert ("Found "+list.length+" items, expected "+replacements.length+"!!");
              } else
              {
                   for (f=list.length-1; f >= 0; f--)
                   {
                        list[f].contents = String(replacements[f]);
                   }
              }