Replace not Format!

I have a script that process all text in my Indesign file, however the formatting gets messed up. Here is basically how I am doing it :point_down:

const documentTextframes = app.activeDocument.allPageItems.filter(item => item.constructorName === "TextFrame")

let count = 0
const intervalID = setInterval(async () => {

  if (count === documentTextframes.length) clearInterval(intervalID);
  var data = { "text": [`${documentTextframes[count].contents}`], "target_language": "spanish" };

    const response = await fetch(url, {
      method: "POST",
      body: JSON.stringify(data),
      headers
    })
    if (response.ok) {
      const translatedText = await response.json();
      documentTextframes[count].contents = translatedText 
    }

}, 2000)

I target and replace the Indesign text using :point_down:

documentTextframes.[count]conents.

When I view documentTextframes.[count]conents in UDT console, the text inside doesn’t carry any formatting syntax, just plain text. So I was thinking that when I replace it the formating would remain intact, but it’s not the case.

Nope, it won’t work that way.
The Text object in InDesign DOM holds the styling information and the text contents.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Text.html
And it kind of makes sense, when you think about it:
“This text has BOLD applied.”
“Ce texte a du GRAS appliqué.”
How would you define the bold part in the translation?

Ideally, the response from the server should also contain the styling information.
You could use textFrame.textStyleRanges to get a list of all the ranges of text that have the same formatting, then try to match them up with your response, but I don’t see how.

1 Like

Got it, okay thanks for the clarification