Skip to main content
Inspiring
July 11, 2025
Question

Paragraphs delivered to UXP script are suddenly "invalid." Why?

  • July 11, 2025
  • 2 replies
  • 890 views

I've written some JavaScript with the UXP facility to do some formatting. It works well... until I select one character too many. It's infuriating: The script iterates through all the paragraphs in a selection and does some styling; but midway through, the paragraphs are all deemed "invalid" by InDesign... but only if the selection goes beyond some invisible boundary.

 

And the start of the "invalid" paragraphs in the overall collection (in the selection) changes based on how many characters the selection goes beyond the arbitrary one. Every extra character seems to cause the invalid paragraphs to start one spot earlier in the overall collection.

 

Check this out:

The farther past the F in "for" the selection goes, the earlier the "invalid" paragraphs are encountered in the collection. Starting with "fo," the 50th paragraph in the selection is invalid. For every non-space additional character I include at the end, the earlier the selection's paragraphs get corrupted (49th, then 48th, and so forth).

 

Anybody ever see this kind of thing before? It has basically rendered scripting unusable for any kind of thorough processing.

2 replies

Peter Kahrel
Community Expert
Community Expert
August 12, 2025

@Thomas_Calvin -- Show us your script, maybe you overlooked something.

Peter Kahrel
Community Expert
Community Expert
July 12, 2025

The first thing I would try is traversing your collection back to front:

 

for (paraIndex = paras.length-1; paraIndex >= 0; paraIndex--)

 

Another thing is that you can speed up the script by converting the collection to an array:

 

var paras = app.selection[0].paragraphs.everyItem().getElements();

Inspiring
August 5, 2025

Thanks. Just tried converting the selection's paragraphs collection into an array, and got very different (and very wrong) results. When I do that, assigning a style to one paragraph assigns it to every paragraph in the selection. Changing back to accessing the paragraphs with item(index) produces correct results until the baffling "invalid" paragraph is hit.

 

I came into work today and executed the same code on the same lines of text, and this time no "invalid" paragraphs were encountered until I extended the selection further into the document. So it's pretty clear the the document's object model is getting corrupted during script execution somehow, even though I'm not adding or removing any paragraphs. Bad pointer? Who knows.

 

Also tried reversing the order, but this didn't eliminate the problem and isn't viable in my case anyway because sometimes I need to assign different styles based on the previous paragraph's style.

leo.r
Community Expert
Community Expert
August 5, 2025

do you have the same problem if, instead of dealing with a selection, you're working with paragraphs of a specific story (or some other predefined array of paragraphs)?