Skip navigation
Bhatt, Manish
Currently Being Moderated

Add 2 per entry in content or index (JS)

Jun 1, 2012 7:42 AM

(Retitled from "Please help me!!" by John Hawkinson)

Hi All,

 

I want to indesign add 2 number per every entry in content or index with javascript

 

For Example

 

2, 9, 10-12, 30-33, 45-48

 

I want this type of result:

 

4, 11, 12-14, 32-35, 47-50

 

 

Please help me please send me javascript ASASP

 
Replies
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    Jun 1, 2012 7:41 AM   in reply to Bhatt, Manish

    This is the your 4th post with the subject line "Please help me" (with varying exclamation points.)

    Please do not do this.

    If everyone did this, the forum would be completely useless.

     

    Please use the subject line to describe and summarize the intent of your post. Do not say "please help me." Of course everyone wants help.

    I will retitle this thread.

     

     

    I can't understand what you are asking. Perhaps you could restate your question with more words, and perhaps a screenshot or a more clear example.

     
    |
    Mark as:
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    Jun 4, 2012 9:49 PM   in reply to Bhatt, Manish

    Manish: It sounds like there's a much better way! Is the problem that you have your pages numbered wrong in the body of the document? You can change the starting page number of the document (or a subset thereof) by selecting the starting page icon in the Pages panel, and choose Numbering and Section options.

    You should be able to correct your page numbering problems in that way.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 3:56 AM   in reply to Bhatt, Manish

    I had this one under my sleeve

    Select the text and then run the script

     

     

    #target indesign
     
    app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Add 2 to page numbers");
    function main(){
     
    app.findGrepPreferences = app.changeGrepPreferences = null;
    myFinds= new Array;
    app.findGrepPreferences.findWhat = "\\d+";
    myFinds=app.selection[0].findGrep();
    for (c=0;c<myFinds.length;c++){
    p=2+1*myFinds[c].contents;
    q=p.toString();
     app.changeGrepPreferences.changeTo=q;
     myFinds[c].changeGrep();
    }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 10:47 PM   in reply to Bhatt, Manish

    Works fine for me, I think you didn't follow my instructions

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 2:24 PM   in reply to Trevorׅ

    Hi,

     

    Is there any way to alter this script so that it finds only numbers greater than a certain number? That is, I need to insert 2 pages after page 32, so every page number in the index that is greater than 32 needs to be incremented by 2, but pages 1-32 need to stay the same.

     

    Thanks in advance if you can help!

     
    |
    Mark as:
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    Jul 27, 2012 4:36 PM   in reply to ArchieOldie

    Is there any way to alter this script so that it finds only numbers greater than a certain number? That is, I need to insert 2 pages after page 32, so every page number in the index that is greater than 32 needs to be incremented by 2, but pages 1-32 need to stay the same.

    Yes.

     

    Something like this (untested):

     

    #target indesign
     
    app.doScript("main()", ScriptLanguage.javascript,
      undefined, UndoModes.FAST_ENTIRE_SCRIPT,
      "Add 2 to page numbers");
    function main(){
      var oldnum;
      app.findGrepPreferences = app.changeGrepPreferences = null;
      myFinds= new Array;
      app.findGrepPreferences.findWhat = "\\d+";
      myFinds=app.selection[0].findGrep();
      for (c=0;c<myFinds.length;c++) {
        oldnum=1*myFinds[c].contents;
        if (oldnum > 32) {
          p=2+oldnum;
          q=p.toString();
          app.changeGrepPreferences.changeTo=q;
          myFinds[c].changeGrep();
       }
      }
    }
    


     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 4:43 PM   in reply to John Hawkinson

    awesome; thank you very much. I will test it out and let you know.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 6:44 PM   in reply to John Hawkinson

    On this line:

     

      myFinds=app.selection[0].findGrep();

     

    I got the message "undefined is not an object"

     

    I'm out of my depth here; any ideas?

     

    Thanks,

    Vicki

     
    |
    Mark as:
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    Jul 27, 2012 8:29 PM   in reply to ArchieOldie

    Did you have anything selected when you ran the script?

    (See instructions for use higher up in the thread).

    Alternatively, did the original posted script work for you?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 8:44 PM   in reply to John Hawkinson

    Oh, I'm an idiot. No, I did not have anything selected. Why, why, why is it so hard to follow directions? In my haste I forgot to re-read before the test. Yes, once I selected the text the script worked beautifully. Thank you so much for your help!

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2012 6:25 PM   in reply to ArchieOldie

    ArchieOldie wrote:

     

    Oh, I'm an idiot. No, I did not have anything selected. Why, why, why is it so hard to follow directions? In my haste I forgot to re-read before the test. Yes, once I selected the text the script worked beautifully. Thank you so much for your help!

     

    Ok,

     

    I know my one line instruction "Select the text and then run the script" is a bit on the complicated side so I have added a catch for those in need of a nap.

     

    made a couple of other changes as well.

     

     

    #target indesign
     try {
           mySelection=app.selection[0];
      app.findGrepPreferences = app.changeGrepPreferences = null;
       var oldnum;
      app.findGrepPreferences.findWhat = "\\d+";
       myFinds= new Array;
     myFinds=mySelection.findGrep();
     
      pagesToAdd=parseInt(prompt ("Ammount to add to page numbers ?", "1", "Ammount to add to page numbers ?"));
      fromPage=parseInt(prompt ("Add "+pagesToAdd+" to page numbers from page ?", "1", "Add "+pagesToAdd+"Add "+pagesToAdd+" to page numbers from page ?"));
      untilPage=parseInt(prompt ("Add "+pagesToAdd+" to page numbers from page "+fromPage+" until page ?", app.documents[0].pages.length, "Add "+pagesToAdd+" to page numbers from page "+fromPage+" until page ?"));
     
    app.doScript("main()", ScriptLanguage.javascript,
      undefined, UndoModes.ENTIRE_SCRIPT,
      "Adjust index page numbers");
    function main(){
     
      myFinds=mySelection.findGrep();
      for (c=0;c<myFinds.length;c++) {
        oldnum=1*myFinds[c].contents;
        if (oldnum >= fromPage && oldnum <=untilPage) {
          p=pagesToAdd+oldnum;
          q=p.toString();
          app.changeGrepPreferences.changeTo=q;
          myFinds[c].changeGrep();
       }
      }
    }
    } 
    catch (noSelection) {
        alert("Did you realy wan't to adjust every number in your "+app.documents[0].pages.length
        +" page document!!!!\rMaybe you just wanted to change the index entries????\rHave a little nap and then select the text of the index and run the script again"); 
        }
    

     

    I made quite an advanced version of this script that also cleans up the index entries, checks that their in the right order (changes 3,6,4, 55-34 to 3, 4, 6, 34-55) using appropriate separators and spacing and dealing with numbers that are part of the entry and not the page number. I.e "WWII 1939 - 1945 34, 66, 36" will change to "WWII 1939 - 1945 36, 38, 68" if we add 2 to the pages and not "WWII 1941 - 1947 36, 38, 68"

     

    But sorry not giving that one away as a freebie for now.

     

    Regards

    Trevor

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 29, 2012 7:43 PM   in reply to Trevorׅ

    You're an ace. Thanks again.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points