5 Replies Latest reply on Aug 15, 2014 11:59 PM by Laubender

    Remove Duplicate Number

    creativejoan0425 Level 1

      the Script below is Remove a duplicate number, when myTextString = "2-, 3", it can remove the duplicate number "3", but when the myTextString = "02-, 03"; it can't remove the duplicate number, any expert can help??

                          var myTextString = "02-, 03";

                         var myEditPg = app.activeWindow.activePage.name;

                          var myAllPage = app.activeDocument.pages.length;

                          var myFirstPage =app.activeDocument.pages[0].name;

                          var myLastPage = app.activeDocument.pages[myAllPage-1].name;

                          var mySplitText = myTextString.split(",");

                          var myRangeArray = [];

                          for(var k=0; mySplitText.length>k; k++){

                              var myM = mySplitText[k].indexOf("-");

                          if(myM >= 0){

                                  var myF = mySplitText[k].substring(0, myM);

                                  var myL = mySplitText[k].substring(myM+1, mySplitText[k].length);

                               if(myL == ""){

                                      myL = myLastPage;

                                      }

                          if(myF.match(/^0+\d+/) || myL.match(/^0+\d+/)){

                              var zeroNum = new RegExp(/^0+/i);

                              var extractZeroNum = zeroNum.exec(myF);

                              for(var i=myF; myL>=i; i++)

                                  myRangeArray.push((extractZeroNum+i).slice(-myF.length));

                                  }

                         else{

                             for(var i=myF; 5>=i; i++)

                              myRangeArray.push(i);

                              }

                          }

                         else {

                              myRangeArray.push(mySplitText[k]);

                              }

                          }

      //~  alert(myRangeArray);

       

       

                          Array.prototype.unique = function (){ 

                              var r = new Array(); 

                              o:for(var i = 0, n = this.length; i < n; i++){ 

                              for(var x = 0, y = r.length; x < y; x++){

                              if(r[x]==this[i]) continue o;} 

                              r[r.length] = this[i];} 

                          return r; 

                          }

                          var myArray = myRangeArray;

                          myText = myArray.unique().join(', '); 

                          alert(myText);

        • 1. Re: Remove Duplicate Number
          [Jongware] Most Valuable Participant

          Can you point out the duplicate number in "2-, 3"? I don't seem to get it. Please show the desired output.

          • 2. Re: Remove Duplicate Number
            creativejoan0425 Level 1

            Open a document , and add 5 pages and then run the script

             

            從三星手機發送

            • 3. Re: Remove Duplicate Number
              Laubender Adobe Community Professional & MVP

              @creativejoan0425 – Hm, I think you have to explain a bit more what is going on here…

               

              If you have a string like that:
              "2-,3"

              compared to the following string:

              "002-,003"

               

              and if I turn to my magic crystal ball sitting always next to me, I can see an export dialog with strings in it. Or is it a print dialog? Can't make it out clearly… And that string "2-,3" you have provided together with a document of six pages will translate in an export or print command: "2-6,3". And InDesign is exactly doing this. Exporting or printing page 3 twice: 2,3,4,5,6,3

               

              Your goal now, my crystal ball is a bit unclear to that, is to fish out the duplicates and print all pages described in that string orderly in one row…

               

              And somehow that does not work with pages named like:

              "002-,003"

               

              Some questions about your code:
              myEditPg and myFirstPage are variables that are declared, but not used. Why?
              Your myRangeArray will return leading or trailing white space, if used:


              "2-,3" => 2,3,4,5,3

              "2-, 3" => 2,3,4,5, 3

              "2 - ,3" => 2 ,3,4,5,3

               

              So I would eliminate trailing whitespace in myTextString before doing anything.

               

              And: this whole topic is not about "Remove Duplicate Number".
              In fact it is about "Remove Duplicate Name".
              And here associative arrays as tool of choice come to my mind.

               

              Uwe

              • 4. Re: Remove Duplicate Number
                creativejoan0425 Level 1

                Hi Uwe,

                 

                Becasue it only a part of my Script, the whole script I use to press a shortcut and run the script which can record the active Page Name in the floating palette, the floating palette can also type the page no, but when print will check if there is any duplicate name for print, for inDesign if I type 1- in page Range, that mean from page 1 to end, so if in the palette I type 1-, 3, and if my activeDocument have 5 pages, that mean will print 1,2,3,4,5,3 so i want the result is 1,2,3,4,5, it ok when the page style is 1,2,3,4 ...

                but when the page style change to  01, 02, 03, 04.... it will not delete the duplicate name

                 

                myEditPg and myFirstPage not use, I forgot to delete


                Thanks

                Joan


                • 5. Re: Re: Remove Duplicate Number
                  Laubender Adobe Community Professional & MVP

                  @Joan – so I would work with "names" only. No stripping of leading zeros. Just a method to build an array of page names (without whitespace). Associated to the names the documentOffset value converted to a string. (EDIT. Changed my mind after writing the snippet below.)

                   

                  First of all I would eliminate unnecessary whitespace in the input string:

                   

                  var myExportOrPrintTextString = "2 - ,3".replace(/\s/g,"");
                  
                  

                   

                  Then I would run a modification of your split method to get an array of single names.

                  The output would be like that:

                   

                  var myResultArray = ["2","3","4","5","6","3"];
                  
                  

                   

                  The double number(s) are and should be still there! No whitespace around the strings!

                   

                  Then I would build an associative array and output the single pages as string like that:

                   

                  //That's the one YOU have to create:
                  var myResultArray = ["2","3","4","5","6","3"];
                  var myResultString = "";
                  var myUniqueArray = [];
                  
                  
                  //A loop through the result array holding the individual page names that should be
                  //exported or printed; double names are still there.
                  for(var n=0;n<myResultArray.length;n++){
                     
                      //Make sure, that only strings are built that are unique:
                      if(myUniqueArray[myResultArray[n]] == null){
                          myUniqueArray[myResultArray[n]] = myResultArray[n];
                          myResultString = myResultString+myUniqueArray[myResultArray[n]]+",";
                      };
                     
                      };
                  
                  //The last comma in the string would do no harm
                  //But we could eliminate it as well:
                  $.writeln(myResultString.replace(/,$/g,""));
                  
                  

                   

                  In this snippet there is no control, if there are any pages numbered more times than once.
                  InDesign would allow this, if you use different sections. It does also not check, if the named pages fed into are there at all.
                  You could begin a section with a page named "234".

                  This all could be handled by building an associative array with all the page names at the beginning of your script reading out the targetted document page names and do some checks.

                   

                  Uwe