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

reversing selected text or paragraph

Community Beginner ,
Oct 09, 2014 Oct 09, 2014

Copy link to clipboard

Copied

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

Views

1.2K

Translate

Translate

Report

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

Copy link to clipboard

Copied

#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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Please

When i try this script

found this error

Any help

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

In this line:

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

add [i] after myLines:

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Nothing changed peter

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

There was another line with the same problem:

 

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

 

Add [i] after myLines

 

P

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Thanks peter

Worked like a charm

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Great Thanks for your effort peter

worked like a charm 

Votes

Translate

Translate

Report

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