2 Replies Latest reply on Nov 18, 2011 5:58 AM by KidPub

    JavaScript to jump to page.paragraph

    KidPub

      The motivation for the the script is that we receive review comments from our authors as a list or spreadsheet with page and paragraph references (p12, para4, change this to that) and it save a lot of time to be able to jump directly to that specific page when applying the edits. The paragraph is highlighted to make it easy to find on the page. It's assigned to Option-J here in the office.Thanks to Peter Kahrel for a snippet that creates a new text condition, and I'm sure there's some code from Harbs in there, too. Please feel free to enhance the script...it does only limited bounds checking, and thinks non-body items such as chapter titles are paragraphs, too.

       

      //DESCRIPTION: Jumps to a specified paragraph on a page. The location is specified  in a dialog box in the form page.paragraph

      //For example, to jump to the fourth paragraph on page 16, use 16.4

      //The specified paragraph is highligthed using conditional text. Tested in InDesign CS5.5.

      //

      //AUTHOR: Perry Donham, pd@kidpub.com, books.kidpub.com

      //Version: 1.0.0

       

       

       

       

      main();

      function main(){

         

      //We'll use conditional text to highlight the paragraph, which is only visible in Normal mode

      //

      //app.documents[0].layoutWindows[0].screenMode = ScreenModeOptions.previewOff;

       

       

      //This is the color we'll use (it's light green)

      //

      var highlightColor = [200,255,200];

       

       

      //Set up the condition

      //

      var highlightCondition = add_condition(app.documents[0], 'Edits', highlightColor);

       

       

      //Display the dialog

      //

      var d=app.dialogs.add({name:"Jump To Page.Paragraph:"});

       

       

      with (d.dialogColumns.add()) {

          var numberBox = textEditboxes.add({minWidth:60});

          }

                var userInteract =  app.scriptPreferences.userInteractionLevel;

                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                var result = d.show();

                app.scriptPreferences.userInteractionLevel = userInteract;

                if(!result){d.destroy();return}

       

       

      //Parse out the paragraph and page numbers using split. The form is page.paragraph

      //

      var pagepara = numberBox.editContents;

      var mySplitResult = pagepara.split('.');

      var selectedParagraph = mySplitResult[1];

      var selectedPage = mySplitResult[0];

       

       

      //Jump to the page

      //

      if (app.activeDocument.pages.length > selectedPage) {

      var thePage=app.activeDocument.pages[selectedPage - 1];

      app.activeWindow.activePage=thePage;

      }

      else {

      alert("The document does not have that many pages");

      return;

      }

       

       

      //Get the text frame on the page

      //

      var theFrame = thePage.textFrames[0]; //should be only one

       

       

      //Get the paragraph in the frame

      //

      if (theFrame.paragraphs.length > selectedParagraph) {

      var theParagraph = theFrame.paragraphs[selectedParagraph - 1];

      }

      else {

          alert("The page does not have that many paragraphs");

      return;

      }

       

       

      //Set the condition

      theParagraph.applyConditions([], true);

      theParagraph.applyConditions(highlightCondition);

       

       

      //We'll use conditional text to highlight the paragraph, which is only visible in Normal mode

      //

      app.documents[0].layoutWindows[0].screenMode = ScreenModeOptions.previewOff;

       

       

       

       

      //Drop the dialog

      //

      d.destroy();

       

       

      //This is from Peter Kahrel's 'deviations' script at www.kahrel.plus.com

      //

      function add_condition (doc, n, c)

                {

                if (doc.conditions.item (n) == null)

                          doc.conditions.add ({

                                              name: n,

                                              indicatorColor: c,

                                              indicatorMethod: ConditionIndicatorMethod.useHighlight});

                return doc.conditions.item (n)

                }

      }

        • 1. Re: JavaScript to jump to page.paragraph
          Green4ever Level 3

          Hi KidPub,

           

          Instead of applying condition to that paragraph select it using app.select(). I feel that selection is the best way. Because document is not modified unnecessarily.

           

           

          Thanks,

          Green4ever

          • 2. Re: JavaScript to jump to page.paragraph
            KidPub Level 1

            A selection would work, too, but you'd have to be careful not to tap the keyboard and lose your paragraph! We leave the conditional highlighting in the doc in our workflow; it helps the proofreader quickly scan to pages that have been changed.

             

            One small tweak. I modified the paragraph code so that if no paragraph was input in the dialog (ie 42 rather than 42.6) it jumps to the page, just like Command-J. That way the script can just replace the built-in jump-to-page and we don't have to think about which to use.

             

            //Get the paragraph in the frame

            //
            if (selectedParagraph != null) {
            if (theFrame.paragraphs.length > selectedParagraph){
            var theParagraph = theFrame.paragraphs[selectedParagraph - 1];
            //Set the condition
            theParagraph.applyConditions([], true);
            theParagraph.applyConditions(highlightCondition);
            }
            else {
            alert("The page does not have that many paragraphs");
            return;
            }
            }

            Perry

            KidPub Press