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
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
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
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,
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
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Nothing changed peter
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
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
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.
Copy link to clipboard
Copied
Great Thanks for your effort peter
worked like a charm