4 Replies Latest reply on Jul 1, 2017 2:47 PM by wespor

    Shuffle Words

    wespor

      Hi, I need to shuffle words in paragraphs. I found the script below but it shuffles paragraphs themselves, I'm not quite savvy enough to modify it myself just yet. Any pointers?

       

      I use InDesign to efficiently plan social media posts for many accounts and repeating hashtags can get you shadowbanned. I have a list of 100~ relevant tags I'd like to randomize and copy/paste selections from.

       

      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);}  
      
        • 1. Re: Shuffle Words
          [Jongware] Most Valuable Participant

          Not a solution but a reasonable workaround for the current script: put each word in a separate paragraph by changing all spaces to a return. Run the script. Change returns back to spaces.

          • 2. Re: Shuffle Words
            Trevorׅ Adobe Community Professional

            A somewhat sloppy implementation.

             

            function shufflePargraphWords(paragraph) {
                paragraph = app.selection[0];
                if (!paragraph || !paragraph.hasOwnProperty('paragraphs')) {
                    return 'no selection';
                }
                paragraph = paragraph.paragraphs[0];
                var a, temp, n, words, firstWord, x, level;
                paragraph.insertionPoints[-1].contents = ' ';
                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat = '\\S+\\s*';
                words = paragraph.findGrep().slice();
                len = words.length;
                while (len) {
                    x = (Math.random() * len--) | 0;
                    temp = words[len];
                    words[len] = words[x];
                    words[x] = temp;
                }
                len = words.length;
                n = len - 1;
                level = $.level;
                $.level = 0;
                while (len--) {
                    try {
                        words[len].move(LocationOptions.AT_END, paragraph);
                    } catch (e) {}
                }
                $.level = level;
                if (paragraph.characters[-1].contents === ' ') {
                    paragraph.characters[-1].remove();
                }
                return 'Shuffled';
            }
            app.doScript(shufflePargraphWords, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Shuffled Words');
            

             

            HTH

             

            Trevor

            • 3. Re: Shuffle Words
              Marc Autret Level 4

              Hi wespor,

               

              To keep more consistent with the original shuffle-paragraph routine—http://forums.adobe.com/message/3834317#3834317—you may also try this:

               

              function shuffleSelectedWords()
              {    
                  const mRAND = Math.random;
              
                  var s = app.properties.selection && app.selection[0],
                      ws = s && s.hasOwnProperty('words') && s.words,
                      n = ws && ws.length;
              
                  if( n < 2 ) throw Error("Please select at least two words.");
                      
                  var sto = ws[0].parentStory,
                      ip0 = ws[0].insertionPoints.firstItem().index,
                      ip1 = ws[n-1].insertionPoints.lastItem().index;
                  
                  if( s=s.hasOwnProperty('appliedFont') ) app.selection = [];
              
                  ws = ws.everyItem().contents;
                  for( var t ; n ; (t=ws.splice(~~(n--*mRAND()),1)[0]), ws.push(t) );
              
                  ws = ws.join(' ');
                  sto.insertionPoints.itemByRange(ip0,ip1).contents = ws;
              
                  if( !s ) return;
                  sto.insertionPoints[ip0].select();    
                  sto.insertionPoints[ip0+ws.length].select(SelectionOptions.ADD_TO);    
              }
              
              try{ shuffleSelectedWords() }catch(_){ alert(_.message) }
              

               

              Note. — This code should be a bit faster since it doesn't hit the DOM during the shuffling stage (to the price of losing character style ranges.)

               

              @+

              Marc

              • 4. Re: Shuffle Words
                wespor Level 1

                Thank you, that is awesome! Trying to figure out how to reverse engineer that and sort by Alphabetical order next. I need to shuffle between the two.