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

Issue Indesign Script. Trying to mirror words

Participant ,
Feb 14, 2020 Feb 14, 2020

Copy link to clipboard

Copied

Hi

 

I’m learning to script and i write this script (I know, it’s a “nonsense” script).

 

The script tries to mirror all the words in a document, for example: nonsense > esnesnon.

 

//SCRIPT//

var g ={};
g.doc = app.activeDocument;

elementosDocumento();

function elementosDocumento() {
  for(var i = 0; i < g.doc.pages.length; i ++){ // First loop through the pages of the document
    var page = g.doc.pages.item(i); // variable que acumula la página
    var pageItems = page.allPageItems; // variable que acumula todos los elementos de la página
      for (var j = 0; j < pageItems.length; j ++){ // Second loop, through the items of the current page
            var currentItem = pageItems[j]; // variable que acumula el elemento actual
            /* Incluir código para actuar con el elemento*/
            var currentItemClass = currentItem.constructor.name;
            if (currentItemClass == "TextFrame") {
                  var textBox = currentItem.parentStory;
                  var textBoxParagraphs = textBox.paragraphs;
                  var selectedWord_Arr = new Array ();
                  var mirrorWord = "";
                  for (var i = 0 ; i < textBoxParagraphs.length ; i++) {
                      var wordsCount = textBoxParagraphs[i].words;
                      for (var s = 0; s < wordsCount.length ; s++) {
                          var myWord = wordsCount[s].characters;
                          for (var x = 0; x < myWord.length; x++) {
                              selectedWord_Arr[x] = myWord[x];
                              }
                          selectedWord_Arr.reverse();
                          for (var y = 0; y < selectedWord_Arr.length; y++) {
                            mirrorWord = mirrorWord + selectedWord_Arr[y].contents;
                          }
                          wordsCount[s].contents = mirrorWord;
                          selectedWord_Arr = [];
                          mirrorWord = "";
                      }
                  }
              }
            /*FIn incluir código actuar con el elemento*/
      }
  }
}

 

I have problems with the characters that are not letters or numbers. When it’s not a number or letter the script adds a number (see the captions) and the character ¿why?

 

Thanks in advance

 

Captura de pantalla 2020-02-14 a las 20.08.41.pngCaptura de pantalla 2020-02-14 a las 20.10.27.png

TOPICS
Scripting

Views

394

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 ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

Maybe try something like this:

 

var s = app.activeDocument.stories;
var w;

for (var i = 0 ; i < s.length ; i++) {
    w = s[i].words;
    //a reverse loop that works from the end to the beginning of each story
    for (var j = w.length-1; j > -1; j--){
        //get the insertion point at the end of each word and set its contents to the reversed string
        w[j].insertionPoints[w[j].length].contents = reverseChar(w[j])  
    }; 
}

//reverses the content of the provided word
function reverseChar(w){
    var rs="";
    var c = w.characters;
    for (var i = 0 ; i < c.length ; i++) {
        rs += c[c.length-(i+1)].contents
    }
    rs = " " + rs
    return rs
}

 

 

Screen Shot 3.pngScreen Shot 4.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
Community Expert ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

I have problems with the characters that are not letters or numbers. When it’s not a number or letter the script adds a number (see the captions) and the character ¿why?

 

Hi Nicosh,

you discovered that there are special characters with text that are represented by enumeration.

1397256787 is the number for SpecialCharacters.HAIR_SPACE.

 

See the list of special characters here:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#SpecialCharacters.html#d1e76630

 

You could test this if you select an insertion point in the text and run the following script:

app.selection[0].contents = SpecialCharacters.HAIR_SPACE;

 

If you like to move characters around better do not use the contents property, but use object character with method move();

 

Or see into this article by Marc Autret to understand what options are available when working with property contents:

Deciphering Special Characters in Text Entities
1. How do I check whether myCharacter.contents is a SpecialCharacters entity?
2. How can I safely convert a single Character (or any Text) into a clean JS string?

http://www.indiscripts.com/post/2016/12/indesign-scripting-forum-roundup-10#hd1sb2

 

Regards,
Uwe Laubender

( ACP )

 

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 ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

Hi Uwe, I think my script will work with text that contains various white space characters. Here I’ve added a few em and en spaces along with an em dash

 

Screen Shot 6.pngScreen Shot 5.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
Community Expert ,
Feb 15, 2020 Feb 15, 2020

Copy link to clipboard

Copied

Ok I see the problem when I use a special character that isn’t white space like a quote— my script will do this:

 

Screen Shot 7.png

 

Changing

 

rs += c[c.length-(i+1)].contents

 

to

 

 rs += c[c.length-(i+1)].texts[0].contents

 

 

fixes the problem—the quotes get reversed with the text

 

Screen Shot 8.png

 

Thanks!

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 ,
Feb 17, 2020 Feb 17, 2020

Copy link to clipboard

Copied

Still, I would do this with method move() of the Character class.

Why? Because formatting will move along and special characters like anchors for anchored objects or tables will move along as well. Not so if one uses property contents.

 

Btw: Word of the Text class sometimes can give strange results.

More about this by Marc Autret:

 

What Exactly is a Word?
Marc Autret | September 04, 2011
http://www.indiscripts.com/post/2011/09/what-exactly-is-a-word

 

Regards,
Uwe Laubender

( ACP )

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 ,
Feb 17, 2020 Feb 17, 2020

Copy link to clipboard

Copied

Not sure I’m following, wouldn’t you still have to create the new reversed word from the target word’s content before you move it?

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 ,
Feb 17, 2020 Feb 17, 2020

Copy link to clipboard

Copied

Hi Rob,

I thought about something like the below code where I do not use texts[0].words[n].contents, but move every single character. Depending on what should be considered a word separator one could get different results. In my screenshot below I am using two different word separators. A simple blank and a regular expression ( that could be refined 😞

 

ReverseCharactersInWords-1.PNG

 

Below the code that is working on a selected text frame or selected text with the scope on the story and every character in it:

 

var textFrame = app.selection[0];
var story = textFrame.parentStory;

/*
	let's define a word separator.
	This is a really simple one:
*/
var wordSeparator = " ";

var wordSeparator = /[ .!?,“”-]/;

var index = 0;

// Create a new text frame that can be thrown away later:
var tempFrame = app.documents[0].textFrames.add();

/*
	Always move the first character of the story.
	Change the insertion point of the target after a word separator character was moved.
	Do that until all characters are moved.
*/

while( story.characters.length != 0 )
{
	
	// Word separator detected
	// With a more ambitious word separator we could use a regular expression at this point
	// E.g. opening and closing quotes or any punctuation.
	
	if( story.characters[0].texts[0].contents.match( wordSeparator ) )
	{
		story.characters[0].move
		( 
			LocationOptions.AFTER , 
			tempFrame.parentStory.insertionPoints[-1] 
		);
		index = tempFrame.parentStory.characters.length ;
		continue
	};

	story.characters[0].move
	( 
		LocationOptions.BEFORE , 
		tempFrame.parentStory.insertionPoints[ index ] 
	);

};

/*
	Then move the whole new story back to its old text frame:
*/
tempFrame.parentStory.move
(
	LocationOptions.BEFORE ,
	story.insertionPoints[0]
);

/*
	Finally remove the temp text frame:
*/
tempFrame.remove();

 

Regards,
Uwe Laubender

( ACP )

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
Participant ,
Feb 18, 2020 Feb 18, 2020

Copy link to clipboard

Copied

Thank you everybody.

 

Thanks Uwe for your aditional info.

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
Contributor ,
Aug 12, 2023 Aug 12, 2023

Copy link to clipboard

Copied

Please

I used this script but I want this to work on whole document or whole page not select text frame only

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
Contributor ,
Aug 12, 2023 Aug 12, 2023

Copy link to clipboard

Copied

LATEST

Hi Laubender

Thanks for your script

But I want this script to apply on current page only or through whole document Not when on current selected text frame only

What should I do ?

Thanks in advance

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