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
2.9K
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

Any help Here

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

help here please

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
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

Just confirming, your situation is:

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

or

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

?

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

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

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

Interesting! I am very curious how you got a document with words that are reversed. Quite unique! I have written a script that will reverse each word. Give it a try and see if it helps.

- Mark

 

 

/**
 * @discussion https://community.adobe.com/t5/indesign-discussions/script-to-reverse-characters-whole-page-or-document/m-p/14003574
 */
app.doScript(reverseWords, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Script');


/**
 * Reverse order of characters of words of
 * (a) selection, if any, or of
 * (b) the entire active document.
 * @author m1b
 * @version 2023-08-13
 * @Param {Text|TextFrame|Document} [texts] - the text or contiainer to reverse (default: active document).
 */
function reverseWords(texts) {

    texts = texts || app.selection[0];

    if (!texts)
        texts = app.activeDocument.stories.everyItem().getElements();

    if (texts.hasOwnProperty('stories'))
        texts = texts.stories.everyItem().getElements();

    if (texts.constructor.name !== 'Array')
        texts = [texts];

    /* uncomment the following lines to customize the deinfition of "word"
        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.findWhat = '[[:alnum:]]+';
    */

    for (var i = 0; i < texts.length; i++) {

        if (
            !texts[i].isValid
            || !texts[i].hasOwnProperty('words')
        )
            continue;

        /* use the following definition of "words" if using findGrep */
        // var words = texts[i].findGrep();

        /* ... otherwise use Indesign's definition */
        var words = texts[i].words;

        for (var w = 0; w < words.length; w++) {

            var charCount = words[w].characters.length - 1;

            // add characters in reverse order
            for (var j = charCount; j >= 0; j--)
                words[w].characters[j].duplicate(LocationOptions.AT_END, words[w]);

            // remove the original text
            words[w].characters.itemByRange(0, charCount).remove();

        }

    }

};

 

 

Edit 2023-08-13: changed definition of "words" to use Indesign's normal definition.

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

Out of curiosity - why do you assume that numbers are not reversed as well? 

 

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

@Robert at ID-Tasker, because I had no idea what I was talking about! 🙂  I see now that numbers will be reversed as well.

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

I think everything between whitespaces should be reversed - as long as really each part is reversed individually... 

 

"ramy hello" is perfectly valid phrase ... 

 

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

I think you are probably right and so I have updated script.

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

Why not "move" instead of "duplicate" characters? You are going in reverse anyway so it should work?

 

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

I don't know why, except I couldn't get move to work (it was my first choice). It said something about not able to move text into itself I think.

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

Maybe it would work if you select as a destination InsertionPoint after the Word - instead of the Word itself? 

 

words[w].characters[j].move(LocationOptions.AT_END, words[w].insertionPoints[-1]);

 

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

I get:

Exception has occurred: 519
Text cannot be moved to its current location.
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

Right... 

 

How about referring to the character of the parent Story for the character to be moved - not the character of the word?

 

Something like:

 

word[w]. parentStory.characters[word[w].characters[j].index].move(...)

 

Or with "()" to get a reference to the character. 

 

But in the end it's probably unnecessary complication, that will work much slower than your initial deletion of the source part of the word?

 

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

Did you try to choose a different composer in the paragraph style settings > Fine Tuning?

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

The subject is that I installed a plugin that convert from pdf to indd that works only LTR , when i tried it on an arabic pdf , arabic characters are reversed , I want the script for this reason

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

Ah! So what happens to punctuation?

Is "hello ramy." written as (1) "olleh .ymar" or (2) "olleh ymar."

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

@r28071715i111 - can you post example screenshot?

 

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

Reversing the character order is the wrong approach.

There are ME versions of InDesign for a reason.

https://helpx.adobe.com/indesign/using/arabic-hebrew.html

 

 

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

The source is something extracted from the PDF - not the original INDD file... 

 

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

You are right @Dirk Becker, (and @Willi Adelberger too of course), but I am assuming the OP has reached for a last resort. It will not be pretty, but it may save them some time.

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

@m1b 

Mark -- Reversing each individual word (i.e. changing the character direction but not the paragraph direction) may not be the correct approach, but, just for interest's sake, there is a much shorter and quicker way to do it. Copying (or moving) characters as character objects is very slow. Just reverse the strings:

 

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

 

See also https://community.adobe.com/t5/indesign-discussions/reversing-selected-text-or-paragraph/m-p/6497724

 

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

But I think you are forgetting about one thing @Peter Kahrel - what if there are different CharStyles applied - or local formatting?

 

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