Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Every word is reversed in situ, so the character styles remain as they are.
Copy link to clipboard
Copied
Sorry, I meant part of the word will have different CharStyles / local formatting:
Lorem Ipsum
Will become
meroL muspI
?
Copy link to clipboard
Copied
Oh, in that way. Yes, well, tough. Too bad.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Fair enough. I hadn't thought of character-specific styles.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi
Sorry for delaynig in reply , now I am confused
Where is the whole script that i can try ?
Copy link to clipboard
Copied
Hi @r28071715i111, first try my full script above. Then tell us how it went. - Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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):
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) :
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:
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
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
@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;
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Did you set the table direction correctly?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now