4 Replies Latest reply on Aug 2, 2011 8:05 AM by KevinAB

    Shuffle Array - Unalphabetize List

    KevinAB Level 1

      I previously posted this under the general InDesign section.  I was referred to the scripting section.

       

      I'm trying to take an alphabetized list and UNalphabetize it in a random order.

      (This will be in a paragraph style that uses a bulleted list)

       

      Example:

       

      • Alpha

      • Beta

      • Charley

      • Delta

       

      to

       

      • Charley

      • Alpha

      • Delta

      • Beta

       

      A friend told my I could use something like this (JavaScript), but he knows nothing about InDesign.  So, he can't help me impliment it. 

      Keep in mind, I've never used a script in InDesign.

       

      function shuffle(array) {
          var tmp, current, top = array.length;
      
          if(top) while(--top) {
              current = Math.floor(Math.random() * (top + 1));
              tmp = array[current];
              array[current] = array[top];
              array[top] = tmp;
          }
      
          return array;
      }
      

       

      Any help will be appreciated.  Thanks!

       

        • 1. Re: Shuffle Array - Unalphabetize List
          Marc Autret Level 4

          Hi KevinAB,

           

          Maybe you could try something like this:

           

          var mRAND = Math.random,
              LOCS = [LocationOptions.AFTER, LocationOptions.BEFORE];
              
          function shuffleSelectedParagraphs()
          {
              var s = app.selection && app.selection[0],
                  ps = s && s.hasOwnProperty('paragraphs') && s.paragraphs,
                  n = ps && ps.length,
                  i = n, j, k, d;
              
              if( n < 2 ) throw Error("Please select at least two paragraphs.");
              
              var sto = ps[0].parentStory,
                  ip0 = ps[0].insertionPoints.firstItem().index,
                  ip1 = ps[n-1].insertionPoints.lastItem().index,
                  EOF = ps[n-1].characters.lastItem().contents != '\r';
          
              if( s = s.hasOwnProperty('appliedFont') ) app.selection = [];
          
              if( EOF ) ps[n-1].insertionPoints.lastItem().contents = '\r';
          
              while( i-- )
                  {
                  do{ j = ~~(n*mRAND()) }while( i==j )
                  k = ~~(2*mRAND());
                  if( 2*k-1 != j-i ) ps[i].move(LOCS[k],ps[j]);
                  }
              
              if( EOF ) sto.characters.lastItem().contents = '';
              
              if( !s ) return;
              sto.insertionPoints[ip0].select();
              sto.insertionPoints[ip1].select(SelectionOptions.ADD_TO);
          }
          
          try{ shuffleSelectedParagraphs() }catch(_){alert(_.message);}
          

           

          (Select the paragraphs to shuffle and run the script.)

           

          @+

          Marc

          • 2. Re: Shuffle Array - Unalphabetize List
            KevinAB Level 1

            Marc,

             

            I appreciate you taking the time to try and help me, but I solved the problem last night (with some help from a friend).

            Here is the code we came up with:

             

            //Script will shuffle text in a selected textframe
            //Script by Kevin Boyette & Daniel Strong
            //August 1, 2011
            
            #target "InDesign-6.0" 
            #target "InDesign"
            
            app.scriptPreferences.version = 6.0; 
            
            var myDocument = app.activeDocument;
            var myPage = myDocument.pages.item(0);
            var myParagraphStyle = myDocument.paragraphStyles.item("Style1"); 
            var test = app.selection[0];
            
            if (test) {
                var test_array = test.contents.split("\r");
                test_array = shuffle(test_array);
                test.contents = test_array.join("\r");
                }
            else {
                alert("You must select an object before running this script.");
                }
            
            function shuffle(array) {
                var tmp, current, top = array.length;
            
                if(top) while(--top) {
                    current = Math.floor(Math.random() * (top + 1));
                    tmp = array[current];
                    array[current] = array[top];
                    array[top] = tmp;
                }
            
                return array;
            }
            
            • 3. Re: Shuffle Array - Unalphabetize List
              Harbs. Level 6

              Hi Kevin,

               

              That will work, but playing with text contents directly is dangerous because it will destroy any non-text objects in the text flow (i.e. tables, inlines, etc.).

               

              So keep in mind, that it's generally bad practice...

               

              Harbs

              • 4. Re: Shuffle Array - Unalphabetize List
                KevinAB Level 1

                Harbs,

                 

                Thanks for the tip -- I didn't know that! 

                Luckily, for what I'm working on, the script works great.  It's just textframes with a bunch of names in them.