1 Reply Latest reply on May 27, 2009 6:47 AM by [Jongware]

    Automatically change all number by some factor

    Ola K

      Hi,

       

      I've been trying to make a Javascript for CS3 that can help in the following situation:

      all figure numbers must chnage by "+1". For example "FIG 2" changes to "FIG 3".

       

      1) Is there a way to do this using GREP?

       

      2) I thought there isn't, so I tried this script, but got stuck with no results. Do you guys know of any existing script on the subject? Otherwise, can you tell me where did I go wrong?

       

      Thanks a lot,

      Ola

       

       

      myDoc = app.activeDocument;


      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      app.findGrepPreferences.findWhat = "FIG \\d+";
      app.findChangeGrepOptions.includeFootnotes = true;
      app.findChangeGrepOptions.includeHiddenLayers = false;
      app.findChangeGrepOptions.includeLockedLayersForFind = false;
      app.findChangeGrepOptions.includeLockedStoriesForFind = false;
      app.findChangeGrepOptions.includeMasterPages = false;

      var myFoundItems = myDoc.findGrep();
      var myDoneItems = [];

      fig = "FIG "

      for (i=myFoundItems.length; i>0; i--)
      {
      string = myFoundItems[i].contents;
      if (string
      num = string.slice(5, string.length);
      newNum = num*1;
      newNum = newNum-1;
      newNum = newNum+"";
      newString = fig+newNum;

      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      app.findGrepPreferences.findWhat = "FIG "+string;
      app.findGrepPreferences.changeTo = "FIG "+newString;


      }

        • 1. Re: Automatically change all number by some factor
          [Jongware] Most Valuable Participant

          "GINM" -- Grep Is Not Magic :-)

           

          Your script looks like it should work but the reason it does nothing is you cannot "insert" your new string using GREP. Simply stating

           

          myFoundItems[i].contents = newstring;

           

          should work. (I think you could make it work with the GREP thing, but it'd replace 'fig 2' with 'fig 3' and then 'fig 3' with 'fig 4'.)

          Perhaps you should test this line

           

          num = string.slice(5, string.length);

          as well -- usually JS starts counting at 0, so the number after "fig " starts at position 4. I'm not too sure about the slice command but I think you got it wrong.