10 Replies Latest reply on May 18, 2015 4:29 AM by selvam214

    Select page range

    selvam214 Level 1

      Hi,

      How to select page range and run our functions with the particular pages?

       

      Selva

        • 1. Re: Select page range
          Harbs. Level 6

          To address a page, you reference it like this:

          First page:

          var myPage = myDoc.pages.item(0);

          Second page:

          var myPage = myDoc.pages.item(1);

          and so on...

           

          If you want to loop through a lot of pages, it's most efficient to get an array of all document pages first like this:

          var docPages = myDoc.pages.everyItem().getElements();

           

          HTH,

          Harbs

          • 2. Re: Select page range
            selvam214 Level 1

            Hi Harbs,

            Thanks for this. If our function should run below method?

             

            Screen shot 2015-04-27 at 5.02.00 PM.png

            Selva

            • 3. Re: Select page range
              Harbs. Level 6

              You need to take the input and convert it into an array of numbers. Something like this would do the trick:

               

                DecomposeNumberRange = function (numberRange){//takes a string or array of strings and returns an array of numbers
                  if(!(numberRange instanceof Array)){
                    if(numberRange.constructor.name!="String"){return []}
                    numberRange = numberRange.split(",");
                  }
                  var retVal=[];
                  for(var i=0;i<numberRange.length;i++){
                    numberRange[i]=numberRange[i].split("-");
                    if(numberRange[i].length==1){
                      retVal.push(Number(numberRange[i][0]));
                    }
                    else{
                      var end=Number(numberRange[i][1]);
                      var start = Number(numberRange[i][0]);
                      while(start<=end){
                        retVal.push(start);
                        start++;
                      }
                    }
                  }
                  return retVal;
                }
              

               

              Then you'd do something like this:

              var range  = DecomposeNumberRange(input);
              var pages = myDoc.pages.everyItem().getElements();
              for(var i=0;i<range.length;i++){
                  var page = pages[range[i]];
                  //do your stuff on page;
              }
              
              • 4. Re: Select page range
                selvam214 Level 1

                Hi Harbs,

                I trying to select the longest line with in the particular page range (pages 1-6, should vary in every project), can you please workout this method.

                 

                  DecomposeNumberRange = function (numberRange){//takes a string or array of strings and returns an array of numbers 

                    if(!(numberRange instanceof Array)){ 

                      if(numberRange.constructor.name!="String"){return []} 

                      numberRange = numberRange.split(","); 

                    } 

                    var retVal=[]; 

                    for(var i=0;i<numberRange.length;i++){ 

                      numberRange[i]=numberRange[i].split("-"); 

                      if(numberRange[i].length==1){ 

                        retVal.push(Number(numberRange[i][0])); 

                      } 

                      else{ 

                        var end=Number(numberRange[i][1]); 

                        var start = Number(numberRange[i][0]); 

                        while(start<=end){ 

                          retVal.push(start); 

                          start++; 

                        } 

                      } 

                    } 

                    return retVal; 

                  }

                var range  = DecomposeNumberRange(input); 

                 

                // here is the original concept

                var pages = myDoc.pages.everyItem().getElements(); 

                for(var i=0;i<range.length;i++){ 

                    var page = pages[range[i]]; 

                function input(){

                        var docLines = app.activeDocument.stories.everyItem().lines.everyItem ().getElements(), 

                        l = docLines.length, 

                        lineLength, longestLine = docLines[l-1], 

                        longestLineLength = longestLine.characters.length; 

                    while(l--) { 

                        if ((lineLength = docLines[l].characters.length) > longestLineLength) { 

                                longestLineLength = lineLength; 

                                longestLine = docLines[l]; 

                        } 

                    } 

                    longestLine.select(); 

                    }

                }

                 

                 

                 

                 

                Selva

                • 5. Re: Select page range
                  selvam214 Level 1

                  Hi All,

                  Anyone can help for this.

                   

                  I have the font name changer script, but i don't want to run all pages. I need to run our tool to the particular page ranges (the range should vary in every file). May you please help for this.

                   

                   

                  app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;

                  myFind = ["Proxima Nova\tRegular","Proxima Nova\tBold","Proxima Nova\tBold Italic","Proxima Nova\tBold Italic","Proxima Nova\Regular Italic"];

                  incrementFontSize=1.5;

                  for(i=0; i<myFind.length; i++)

                  {

                      app.findTextPreferences = app.changeTextPreferences = null;

                      app.findChangeTextOptions.includeMasterPages = false;

                      app.findTextPreferences.appliedFont = myFind[i];

                      myFoundItems=app.activeDocument.findText();

                           for (r=0;r<myFoundItems.length;r++)

                           {

                              pt=myFoundItems[r].texts[0].pointSize;

                              ct=pt+incrementFontSize;

                              myFoundItems[r].texts[0].pointSize=ct;

                          }

                  }

                  app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;

                  alert ("Done");

                  • 6. Re: Select page range
                    selvam214 Level 1

                    Hi All,

                    Why the reason is, I have the file with teaser pages. No need to change the teaser pages and live only want to change.

                     

                    Thanks,

                    Selva.

                    • 7. Re: Select page range
                      selvam214 Level 1

                      Hi All,

                      Kindly help on this topic.

                       

                      Selva

                      • 8. Re: Select page range
                        S Hopkins Adobe Community Professional

                        The sample script below places emphasis on the dialog used to get the page range from the user. Run the script and then analyze the code. You should be able to apply much of this to your needs. Hope this helps.

                         

                        try {

                            //assumes document is open

                            var docRef = app.documents.item(0);

                            var maxPages = docRef.pages.count();

                          //provide variable to hold user's response

                          var userResponse = dialogWChoices ("Dialog Name", true, "Label here", maxPages);

                            //enter call to function that processes all pages

                            if (userResponse[0] == 0) {

                                myProcess (docRef, 0, maxPages);

                             } else {

                                 var minPageRef = userResponse[1] - 1;

                                 var maxPageRef = userResponse[2] - 1;

                                 myProcess (docRef, minPageRef, maxPageRef);

                             }

                        } catch (e) {

                          alert (e);

                        }

                        alert ("User entered " + userResponse[0] + " " + userResponse[1] + " " + userResponse[2]);

                         

                        //function to process range of pages

                        function myProcess (docRef, minPageRef, maxPageRef) {

                            var thisPage, thisFrame, docOffset;

                           for (var i = minPageRef; i <= maxPageRef; i++) {

                               thisPage = docRef.pages.item(i);

                               docOffset = (thisPage.documentOffset) + 1;

                            thisFrame = thisPage.textFrames.add({geometricBounds:[36, 72, 100, 300]});

                            thisFrame.contents = "Hello page " + docOffset

                           }

                        }

                        //function gets page range or "all" from user

                        function dialogWChoices (dlgName, cancelIt, dLabel, maxPages) {

                            var userCancelled = false;

                            var origValue = app.scriptPreferences.userInteractionLevel;

                          //make sure that user interaction levels will allow a dialog

                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                          //create the dialog

                          var dlgRef = app.dialogs.add({name:dlgName, canCel:cancelIt, label:dLabel});

                          //add a column

                          var dlgColumn1 = dlgRef.dialogColumns.add();

                            var dlgColumn2 = dlgRef.dialogColumns.add();

                            var dlgColumn3 = dlgRef.dialogColumns.add();

                          //add widgets for first column

                          dlgColumn1.staticTexts.add({staticLabel: "PageRange:"});

                          //add widgets to second column

                          var radioGroup = dlgColumn2.radiobuttonGroups.add();

                            radioGroup.radiobuttonControls.add({staticLabel:"All", checkedState:true});

                            radioGroup.radiobuttonControls.add({staticLabel:"Range", checkedState:true});

                            //add widgets to third column; put reference into a variable

                            var emptyRow = dlgColumn3.dialogRows.add();

                            emptyRow.staticTexts.add ({staticLabel: ""});

                            var inputRow = dlgColumn3.dialogRows.add();

                            var firstField = inputRow.integerEditboxes.add({minWidth:36, minimumValue:1,maximumValue:maxPages});

                            inputRow.staticTexts.add ({staticLabel: " thru "});

                            var secondField = inputRow.integerEditboxes.add({minWidth:36, minimumValue:1, maximumValue:maxPages});

                          //show the dialog and capture the result

                          if (dlgRef.show() == true) {

                                var firstVal = 0;

                                var secondVal = 0;

                                var selButton = radioGroup.selectedButton;

                                if (selButton == 1) {

                          var firstVal = firstField.editValue;

                          var secondVal = secondField.editValue;

                            }

                          } else {

                                userCancelled = true;

                          }

                               dlgRef.destroy();

                               app.scriptPreferences.userInteractionLevel = origValue;

                            if (userCancelled) {

                               throw ("User Cancelled");

                            }

                          //destroy the dialog; script doesn't get here if user cancels

                          return [selButton, firstVal, secondVal];

                        }

                        • 9. Re: Select page range
                          selvam214 Level 1

                          Hi Hopkins,

                          Thanks for the codes. I'll check and get back to you shortly.

                           

                          Selva.

                          • 10. Re: Select page range
                            selvam214 Level 1

                            Hello Hopkins,

                            I just wanted to tell you, your code is gorgeous! And your very talented!

                            Everything you create is just an absolute joy to look at. Especially the page range function.

                             

                             

                            Thanks for your understanding for what me want.

                            Selva

                             

                             

                             

                             

                             

                            Here my changes (for your review):

                            ///////////////////////////////////

                             

                            try {

                                //assumes document is open

                                var docRef = app.documents.item(0);

                                var maxPages = docRef.pages.count();

                              //provide variable to hold user's response

                              var userResponse = dialogWChoices ("Dialog Name", true, "Label here", maxPages);

                                //enter call to function that processes all pages

                                if (userResponse[0] == 0) {

                                    myProcess (docRef, 0, maxPages);

                                 } else {

                                     var minPageRef = userResponse[1] - 1;

                                     var maxPageRef = userResponse[2] - 1;

                                     myProcess (docRef, minPageRef, maxPageRef);

                                 }

                            } catch (e) {

                            }

                            alert ("Process Done!\rWith the selected pages.");

                            //function to process range of pages

                            function myProcess (docRef, minPageRef, maxPageRef) {

                                var thisPage, thisFrame, docOffset;

                                //our code start from here

                                var myStep = 1; 

                               for (var i = minPageRef; i <= maxPageRef; i++) {

                                   thisPage = docRef.pages.item(i);

                                   var myTables = thisPage.textFrames[0].tables.everyItem().getElements();

                                   for (j =0; j<myTables.length; j++){

                                       if(myTables[j].spaceAfter  + myStep<0){myTables[j].spaceAfter = 0; continue};

                                       myTables[j].spaceAfter = myTables[j].spaceAfter + myStep;

                                      } //end our code

                                 }

                            }

                            //function gets page range or "all" from user

                            function dialogWChoices (dlgName, cancelIt, dLabel, maxPages) {

                                var userCancelled = false;

                                var origValue = app.scriptPreferences.userInteractionLevel;

                              //make sure that user interaction levels will allow a dialog

                              app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                              //create the dialog

                              var dlgRef = app.dialogs.add({name:dlgName, canCel:cancelIt, label:dLabel});

                              //add a column

                              var dlgColumn1 = dlgRef.dialogColumns.add();

                                var dlgColumn2 = dlgRef.dialogColumns.add();

                                var dlgColumn3 = dlgRef.dialogColumns.add();

                              //add widgets for first column

                              dlgColumn1.staticTexts.add({staticLabel: "PageRange:"});

                              //add widgets to second column

                              var radioGroup = dlgColumn2.radiobuttonGroups.add();

                                radioGroup.radiobuttonControls.add({staticLabel:"All", checkedState:true});

                                radioGroup.radiobuttonControls.add({staticLabel:"Range", checkedState:true});

                                //add widgets to third column; put reference into a variable

                                var emptyRow = dlgColumn3.dialogRows.add();

                                emptyRow.staticTexts.add ({staticLabel: ""});

                                var inputRow = dlgColumn3.dialogRows.add();

                                var firstField = inputRow.integerEditboxes.add({minWidth:36, minimumValue:1,maximumValue:maxPages});

                                inputRow.staticTexts.add ({staticLabel: " thru "});

                                var secondField = inputRow.integerEditboxes.add({minWidth:36, minimumValue:1, maximumValue:maxPages});

                              //show the dialog and capture the result

                              if (dlgRef.show() == true) {

                                    var firstVal = 0;

                                    var secondVal = 0;

                                    var selButton = radioGroup.selectedButton;

                                    if (selButton == 1) {

                              var firstVal = firstField.editValue;

                              var secondVal = secondField.editValue;

                                }

                              } else {

                                    userCancelled = true;

                              }

                                   dlgRef.destroy();

                                   app.scriptPreferences.userInteractionLevel = origValue;

                                if (userCancelled) {

                                   throw ("User Cancelled");

                                }

                              //destroy the dialog; script doesn't get here if user cancels

                              return [selButton, firstVal, secondVal];

                            }

                            /////////////////////////////