16 Replies Latest reply: Jul 29, 2012 7:43 PM by ArchieOldie RSS

    Add 2 per entry in content or index (JS)

    Bhatt, Manish Community Member

      (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

        • 1. Re: Please help me!!
          John Hawkinson CommunityMVP

          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.

          • 2. Re: Please help me!!
            Bhatt, Manish Community Member

            Hi

             

            I have uploaded a screen short for your reference

             

            could be please suggest me every entry 2 number should be add if 18 is there i want 20 with javascript

             

             

            Screen shot 2012-06-05 at 9.17.26 AM.png

             

            Screen shot 2012-06-05 at 9.17.36 AM.png

            • 3. Re: Please help me!!
              John Hawkinson CommunityMVP

              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.

              • 4. Re: Add 2 per entry in content or index (JS)
                Trevorׅ Community Member

                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();
                }
                }
                
                • 5. Re: Add 2 per entry in content or index (JS)
                  Bhatt, Manish Community Member

                  Hi they an error see attachment file its not working please help me?

                   

                  Screen shot 2012-06-06 at 9.05.54 AM.png

                  • 6. Re: Add 2 per entry in content or index (JS)
                    Bhatt, Manish Community Member

                    all index and content page number set in manually so how we can add 2 numbers per entry for example:

                     

                    2, 3, 11-13

                     

                    I want result for this for example

                     

                    4, 6, 13-15

                     

                    please help me?

                    • 7. Re: Add 2 per entry in content or index (JS)
                      Trevorׅ Community Member

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

                      • 8. Re: Add 2 per entry in content or index (JS)
                        Bhatt, Manish Community Member

                        Oh sorry yes its working now thanks a lot of you

                        • 9. Re: Add 2 per entry in content or index (JS)
                          ArchieOldie Community Member

                          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!

                          • 10. Re: Add 2 per entry in content or index (JS)
                            John Hawkinson CommunityMVP

                            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();
                               }
                              }
                            }
                            


                            • 11. Re: Add 2 per entry in content or index (JS)
                              ArchieOldie Community Member

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

                              • 12. Re: Add 2 per entry in content or index (JS)
                                ArchieOldie Community Member

                                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

                                • 13. Re: Add 2 per entry in content or index (JS)
                                  John Hawkinson CommunityMVP

                                  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?

                                  • 14. Re: Add 2 per entry in content or index (JS)
                                    ArchieOldie Community Member

                                    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!

                                    • 15. Re: Add 2 per entry in content or index (JS)
                                      Trevorׅ Community Member

                                      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

                                      • 16. Re: Add 2 per entry in content or index (JS)
                                        ArchieOldie Community Member

                                        You're an ace. Thanks again.