How to get text from API through UXP

We are using InDesign to make templates/design, and then we use UXP to get the data and send it to a different system for visualization

For text I’m using stories, e.g.

// Every text-frame seems to be a story, so we can loop through them
for (let i = 0; i < app.activeDocument.stories.length; i += 1) {
  const story = app.activeDocument.stories.item(i)
  // and then we loop through all paragraphs in each frame
  for (let j = 0; j < story.paragraphs.length; j += 1) {
  	// and here we get content, paragraphStyle, etc
  }
}

Now this works just fine if done Paragraph Styles is used on everything, and every paragraph has the same style. If anything is changed, e.g. I left justify some text or add a different font to some of the text, then I have to go “hunting” for the data I need

I end up with lots of (recursive) code, e.g look through all attributes to check if paragraph.appliedFont is an object and if not look in paragraph.basedOn, and so on.

For text I cannot use paragraph.contents, since that only contains the plain text for the whole paragraph. If a different style/color/font is used on some of the text I can’t see that there. It seems like I’m missing some crucial way of getting data

So my question would be; is there a good/efficient way of looping through all textboxes and get all data used?

To better explain, here is an example (see screenshot):

From this one textbox I would like to get the following

The text “Line 1” – Based on Paragraph Style “Basic Paragraph” but overridden with left align
The text “Line 2” – Based on Paragraph Style “Basic Paragraph” but overridden with font size = 12
The text “Line 3” – Using Character Style “Basic Character Style” but overridden with font size = 14
The text “Line 4 With Different font” – Where “Line 2” is using Paragraph Style “Basic Paragraph” and “With Different font” is overridden with a different font and size

The output I want is rather simple, it would show what the text is based on, and then every override – be it justification, line height, color, etc. It could look like this based on the screenshot/example:

[
  {
    "textBoxId": 1,
    "paragraphs": [
      {
        "paragraph": {
          "content": "Line 1\n",
          "paragraphStyleId": 1,
          "justification": "LEFT_JUSTIFIED"
        }
      },
      {
        "paragraph": {
          "content": "Line 2\n",
          "paragraphStyleId": 1,
          "size": 12
        }
      },
      {
        "paragraph": {
          "content": "Line 3 ",
          "characterStyleId": 4,
          "size": 14
        }
      },
      {
        "paragraph": {
          "content": "With Different font",
          "paragraphStyleId": 1,
          "fontId": 12,
          "size": 12
        }
      }            
    ]
  }
]

Anyone know how to achieve something like this?

Have you looked at .textStyleRanges?
That would have all the uniquely formatted text.

const story_o = app.activeDocument.stories.item(i)
const allTextStyleRanges_a = story_o.textStyleRanges;
for (let j = 0; j < allTextStyleRanges_a.length; j += 1) {
  	let activeContent_s = allTextStyleRanges_a[j].content;
  	let activeStyle_s = allTextStyleRanges_a[j].appliedParagraphStyle;
  	// and other stuff
  }

jsw

1 Like

Ah, that looks great :slight_smile:

1 Like