4 Replies Latest reply on Oct 10, 2014 6:31 AM by Sajeev Sridharan

    reversing selected text or paragraph

    content server Level 1

      hi all,

      i have a document that contains the lines reversed (for example, Hello All is written llA olleH), so how can i reverse a selected line and reverse the text inside the line (assuming i select each line before i run the script), and also how can i reverse the whole document without needing to select every single line at a time?

       

      thanks in advance

        • 1. Re: reversing selected text or paragraph
          Sajeev Sridharan Level 4

          #1: Selected Line or Paragraph

           

          First select line or paragraph before running the code,

           

              myString = app.selection[0].contents;
              myString = myString.split("").reverse().join("");
              app.selection[0].contents = myString;
          

           

          #2: for whole document

           

          Try yourself:

           

          //Logic

          1. loop no. of pages

          2. loop no of textframes

          3. loop no. of lines

          4. then paste the above code

          5. thats it you're done.

           

           

          Vandy

          • 2. Re: Re: reversing selected text or paragraph
            BEGINNER_X Level 3

            Hi,

             

            Hope you are new to scripting, so kindly use the below script.

             

            With the help of vandy coding,

             

            var myLines = app.activeDocument.stories.everyItem().lines.everyItem().getElements();
            
            for(i=0; i<myLines.length; i++)
            {
                if(myLines[i].contents.match(/^\r/) !=null)
                {
                    continue;
                    }
                
                app.select(myLines[i].characters.itemByRange(0, -2));
                alert("select line")
                myString = app.selection[0].contents;  
                myString = myString.split("").reverse().join("");  
                app.selection[0].contents = myString;  
                }
            

             

             

            Regards

            Siraj

            • 3. Re: Re: reversing selected text or paragraph
              content server Level 1

              Dear Sir,

              Thank you for your reply,

              The function ran correctly for couple of pages but we got the following error,

              Kindly, do you have any solution?

              Thank you in advance,

               

              error.png

              • 4. Re: Re: Re: reversing selected text or paragraph
                Sajeev Sridharan Level 4

                It seems empty textframes in your document, you put try/catch in the code.

                 

                var myLines = app.activeDocument.stories.everyItem().lines.everyItem().getElements();  
                  
                for(i=0; i<myLines.length; i++)  
                {  
                    if(myLines[i].contents.match(/^\r/) !=null)  
                    {  
                        continue;  
                    }  
                    try{
                        app.select(myLines[i].characters.itemByRange(0, -2));  
                        alert("select line")  
                        myString = app.selection[0].contents;    
                        myString = myString.split("").reverse().join("");    
                        app.selection[0].contents = myString;    
                    }catch(e){}
                }  
                

                 

                 

                Vandy