Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

reversing selected text or paragraph

Community Beginner ,
Oct 09, 2014 Oct 09, 2014

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

TOPICS
Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 09, 2014 Oct 09, 2014

#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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 09, 2014 Oct 09, 2014

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.contents.match(/^\r/) !=null)

    {

        continue;

        }

   

    app.select(myLines.characters.itemByRange(0, -2));

    alert("select line")

    myString = app.selection[0].contents; 

    myString = myString.split("").reverse().join(""); 

    app.selection[0].contents = myString; 

    }

Regards

Siraj

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 10, 2014 Oct 10, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 10, 2014 Oct 10, 2014

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.contents.match(/^\r/) !=null) 

    { 

        continue; 

    } 

    try{

        app.select(myLines.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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 12, 2023 Aug 12, 2023

Please

When i try this script

found this error

Any help

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2023 Aug 12, 2023

In this line:

if(myLines.contents.match(/^\r/) !=null)

add [i] after myLines:

if(myLines[i].contents.match(/^\r/) !=null)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 12, 2023 Aug 12, 2023

Nothing changed peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2023 Aug 12, 2023

There was another line with the same problem:

 

app.select(myLines.characters.itemByRange(0, -2)); 

 

Add [i] after myLines

 

P

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 12, 2023 Aug 12, 2023

Thanks peter

Worked like a charm

Excuse me : How can i let script apply on whole document rather than selecting each page

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 12, 2023 Aug 12, 2023

Then use this version:

 

myLines = app.activeDocument.stories.everyItem().lines.everyItem().getElements();
for (i=0; i<myLines.length; i++) {
  s = myLines[i].contents.slice(0,-1);
  if (s.length > 2) {
    myLines[i].contents = s.split("").reverse().join("") + '\r';
  }
}

 

P.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Aug 12, 2023 Aug 12, 2023
LATEST

Great Thanks for your effort peter

worked like a charm 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines