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

Script to reverse characters whole page or document

Engaged ,
Aug 12, 2023 Aug 12, 2023

Hi All

I have a document that have text frames containes characters are in reverse order

Ex : ( olleh ymar ) intstead of  ( hello ramy )

I want to script that change these reverse to be correct sentences in the curret page or in whole document

Thanks in advance

 

TOPICS
Scripting
4.2K
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 13, 2023 Aug 13, 2023

Every word is reversed in situ, so the character styles remain as they are.

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
LEGEND ,
Aug 13, 2023 Aug 13, 2023

Sorry, I meant part of the word will have different CharStyles / local formatting: 

 

Lorem Ipsum

 

Will become

 

meroL muspI

 

 

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 13, 2023 Aug 13, 2023

Oh, in that way. Yes, well, tough. Too bad.

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 13, 2023 Aug 13, 2023

Thanks @Peter Kahrel, that would be much faster definitely, and would be a good choice in most situations but, like @Robert at ID-Tasker, and with so little information about the OP's exact situation, I was assuming that every character may have had its own character styling.

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 13, 2023 Aug 13, 2023

Fair enough. I hadn't thought of character-specific styles.

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 13, 2023 Aug 13, 2023

To change the character direction in each word but not the order of words -- i.e. olleh ymar > hello ramy -- use this:

 

words = app.documents[0].stories.everyItem().words.everyItem().getElements();
for (var i = words.length-1; i >= 0; i--) {
  // Use either of the following two:
  //words[i].characterDirection = CharacterDirectionOptions.RIGHT_TO_LEFT_DIRECTION;
  words[i].characterDirection = CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION;
}

 

To change the character direction and the order of the words (olleh ymar > ramy hello), use this:

app.documents[0].stories
  .everyItem()
  .words.everyItem().characterDirection = 
    CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION;

 (change the last line to RIGHT_TO_LEFT if needed)

 

For both methods to work the composer must be set to one of the World composers.

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 13, 2023 Aug 13, 2023

That is a promising approach Peter! All depends on the specific properties of the OP's converted-from-pdf text I think. I'm quite curious to see how this project goes.

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 20, 2023 Aug 20, 2023

Hi

Sorry for delaynig in reply , now I am confused

Where is the whole script that i can try ?

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 20, 2023 Aug 20, 2023

Hi @r28071715i111, first try my full script above. Then tell us how it went. - Mark

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 20, 2023 Aug 20, 2023

Hi m1b

I tried your first script and that is the result 

1) some of the characters are not correct

2) text in the above table still no change ( reversed )

 

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 ,
Aug 20, 2023 Aug 20, 2023

Hi @r28071715i111 , here is a version for reversing text in tables cells:

//Reverse Characters in All Document Tables
app.doScript(CatchMyError, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Reverse Characters in Doc Tables");

// Error Catcher for Function
function CatchMyError() {
  try {
    DoReverseAllTables();
  } catch (myError) {
    alert(myError, "ERROR!");
    exit();
  }
}

function DoReverseAllTables() {
    //Get All Tables Table Style Apply
    var myDoc = app.activeDocument;
    var tables = myDoc.stories.everyItem().tables.everyItem().getElements();
        
    // Iterate through each table in the document
    for (var tableIndex = 0; tableIndex < tables.length; tableIndex++) {
      var table = tables[tableIndex];

      // Iterate through each cell in the table
      for (var rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
        var row = table.rows[rowIndex];

        for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
          var cell = row.cells[cellIndex];
          var textFrames = cell.texts; // Get the text frames in the cell

          // Iterate through each text frame in the cell
          for (var textFrameIndex = 0; textFrameIndex < textFrames.length; textFrameIndex++) {
            var textFrame = textFrames[textFrameIndex];
            var text = textFrame.contents;

            // Reverse the character direction
            var reversedText = reverseCharacterDirectionTables(text);
            // Set the reversed text back to the text frame
            textFrame.contents = reversedText;        
          }
        }
      }
    }
   //wait a maximum of 10 second for the active process.
  app.activeDocument.activeProcess.waitForProcess(10);
  alert("Character direction reversed in all tables!","Finished");
}

function reverseCharacterDirectionTables(text) {
  var reversedText = "";
  for (var i = text.length - 1; i >= 0; i--) {
    reversedText += text.charAt(i);
  }
  return reversedText;
}
Best
Mohammad Hasanin
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
LEGEND ,
Aug 20, 2023 Aug 20, 2023

I'm not JS guy, so... 

 

Why are you iterating through Cells looking for TextFrames? 

 

You can get them from:

 

myDoc.Stories 

 

Even when the TextFrame is inside a Cell - it's ParentStory is still part of the Stories of the Document - so there is no point iterating Cells.

 

And if you are just iterating through all Cells - why can't you use JS's .everyItem() to get all Cells from the tableT and then process this "collection"?

 

Like I've said, I'm not JS guy so just asking... 

 

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 ,
Aug 20, 2023 Aug 20, 2023

Hi @Robert at ID-Tasker 
Actually, this is very good Question, now try the following simple script :

 

words = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().texts.everyItem().words.everyItem().getElements();
for (var i = words.length-1; i >= 0; i--) {
  words[i].contents = words[i].contents.split('').reverse().join('');
}

 

the previous will work with latin/English but not with Arabic, it shouid simply do what the complicated script done!, but unlucky this is not true!, look at the wrong results here (Before/After):
Results 1.png
The Results is not correct , the words in wrong direction (المهام جدول) it must be (جدول المهام)
also (تجربة هذه) must be (هذه تجربة) to be in the correct direction!
now look for the results for my complicated script!, that will correctly make the right direct for the letters (Before/After) :
Correct Results 1.png

Try it your Self , here is link to download test project :
https://mega.nz/file/Kjh0FLCK#ehO8wDxKH9wD38EViit-RtJq4ku4sT08DFl48YNzs5M 

actually i spent a lot of hours try to figure out how to solve this, maybe there are another way i didnt try it!

Best
Mohammad Hasanin
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
LEGEND ,
Aug 21, 2023 Aug 21, 2023

But in your previous script - long version - you are using different method to reverse characters in the Words?? Maybe tuis is why new version isn't working correctly? 

 

I was just pointing out that there is a quicker method to get all the Texts.

 

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 22, 2023 Aug 22, 2023

Hi @M.Hasanin, it looks like your scripting is going very well! Can you see why some Arabic words, when characters are reversed, are broken?

 

Like these, marked by the OP:

Screenshot 2023-08-20 172116.png

 

I cannot read Arabic, so I'm no help, but I'm very curious about what is going on here. Something to do with opentype substitution rules?

- Mark

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 ,
Aug 23, 2023 Aug 23, 2023

@m1b 
Hi mark, Thank you, actually the broken characters caused because of the InDesign Plugin PDF2ID also Alternative Plugin MARKZWARE Cause Same Problem, so the editor will need to fix it manually after reversing the text.

Best
Mohammad Hasanin
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 21, 2023 Aug 21, 2023

@Robert at ID-Tasker is right, you can get the words in all the cells in one go. So you need three steps to get all words in a document: the level-1 stories, the tables, and the footnotes. The script I posted then needs the addition of tables and footnotes:

 

words = app.documents[0].
  stories.everyItem().
  words.everyItem().getElements();

tables = app.documents[0].stories.everyItem().tables;
if (tables.length > 0) {
  words = words.concat (tables.everyItem().
    cells.everyItem().
    texts.everyItem().
    words.everyItem().getElements()
  );
}

fnotes = app.documents[0].stories.everyItem().footnotes;
  if (fnotes.length > 0) {
    words = words.concat (fnotes.everyItem().
      texts.everyItem().
      words.everyItem().getElements()
  );
}


for (i = words.length-1; i >= 0; i--) {
  // Use either of the following two:
  //words[i].characterDirection = 
	  //CharacterDirectionOptions.RIGHT_TO_LEFT_DIRECTION;
  words[i].characterDirection = 
	  CharacterDirectionOptions.LEFT_TO_RIGHT_DIRECTION;
}

 

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 ,
Aug 23, 2023 Aug 23, 2023

Hi @Peter Kahrel  thank you, your updated script very good for one hit reversing, but using character direction is not the right choice, it reverse the characters wrongly, if you use :

      words[i].contents = words[i].contents.split('').reverse().join('');

it will work but will swap the locations of word, so simple table will be table simple, etc also in arabic

Best
Mohammad Hasanin
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 24, 2023 Aug 24, 2023
LATEST

Did you set the table direction correctly?

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