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

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

Engaged ,
Jul 11, 2025 Jul 11, 2025

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:

invalidParagraph.png

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.

TOPICS
Scripting , UXP Scripting
536
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 ,
Jul 12, 2025 Jul 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();

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 05, 2025 Aug 05, 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.

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 05, 2025 Aug 05, 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)? 

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 05, 2025 Aug 05, 2025

The document is one story, so there's really no other unit of work that makes sense in my application. The purpose of the script is to process selected text deliberately. The content of the starting point influences the script's interpretation of what follows.

 

Further experiments show that, immediately after selection, all the paragraphs are "valid." I added a validation function that iterates through the whole collection and checks them.

 

During subsequent processing, some paragraphs become "invalid" after I start assigning styles to earlier ones... but only if the selection crosses some invisible boundary. And it's certainly not a memory issue, because the selection is very small. In this screen grab, I execute my script successfully. Then I undo everything it did, extend the selection by one character, and re-run it; then some of the paragraphs have become invalid. The number of invalid paragraphs increases as the selection is extended toward the end.

 

I tried adding a couple more return characters to the end of the story and not selecting them, but this made no difference. So encroaching on the end of the story doesn't seem to be a factor here.

 

Update: As a last resort I tried re-assigning the paragraph-collection variable on every iteration of the for loop:

 

paras = app.selection[0].paragraphs
 
which worked... for a while. But a few executions later, and I'm back to paragraphs being "invalidated" if you do anything to any text.
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 08, 2025 Aug 08, 2025

Do you see the same problem in ExtendScript?

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 08, 2025 Aug 08, 2025

Well, I updated my comment above, but to make it clear: The workaround I thought was totally effective (avoiding saving any reference to the selection's paragraph collection) was not. Before long, I was hitting "invalid" paragraphs again.

 

It appears that there's a defect in the scripting engine or underlying object model, where performing any operation (such as deleting some characters or even just applying a style) invalidates the model and disables the script from that time on. Where the "invalid" contents begin is related to the length (or at least the end-point) of the selection, even when the selection is just a few lines. This strikes me as a bad index or bad pointer somewhere, but of course that's out of our view.

 

As it stands, I don't see how the scripting/plug-in facility is usable.

 

To answer your question, Peter, no; I've moved on from ExtendScript because I wanted to use the UXP debugger. My script won't run under the ancient version (3) of JavaScript used by ExtendScript.

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 08, 2025 Aug 08, 2025
quote

I've moved on from ExtendScript because I wanted to use the UXP debugger.


By @Thomas_Calvin

 

As far as I understand, anything you're doing with InDesign's UXP at this point equates to beta-testing an unfinished product (if I'm wrong, I'm sure others will correct me).

 

So if you'll try to achieve what you need via ExtendScript, you'll find out if you discovered a UXP bug or if there's a general issue that's present in ExtendScript as well.

 

I think VSCode has debugging facilities for ExtendScript (but I'm not an expert in this area).

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 11, 2025 Aug 11, 2025

Thanks for the reply. This is not what I gather from the Adobe docs. They consider ExtendScript deprecated.

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 11, 2025 Aug 11, 2025

They consider ExtendScript deprecated.

 

Not sure who they is, but ExtendScript is being maintained —the API for version 20.0.0.95 is here:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.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
Engaged ,
Aug 11, 2025 Aug 11, 2025

The (perplexing) landing page for the API: developer.adobe.com/indesign/dom/api/

 

And I don't remember what other areas I rummaged through while scrounging for documentation. If you do a search on the question, ExtendScript is widely believed to be deprecated. The fact is that it's based on an absurdly ancient version of JavaScript is a given.

 

I think I mostly started here: Overview - UXP for Adobe InDesign

 

The messaging is that CEP is deprecated.

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 11, 2025 Aug 11, 2025

UXP is still work in progress, and unfortunately the documentation, such as it is, isn't being updated as UXP develops. The first link you gave, to the OMV, is for ID 18.0. It's mentioned there that as of InDesign 18.0 the ESTK no longer reliably connects to InDesign. In my experience that's not the case, in ID 2025 it still connects fine (on Windows) and its object-model viewer works as it always did (i.e. warts an all). On MacOS you have the ESTK extension for VS Code, which since recently sports a well-working OMV. The second link, the UXP overview, was last updated in 2023, like other pages I saw last week. All out of date.

 

It's true that ExtendScript is based on an old JavaScript version, but that doesn't mean that you can't use it. In fact, it is very much usable.

 

As for deprecation, how long ExtendScript and CEP will be supported is not clear, probably even Adobe don't know how long they'll be with us. UXP (both scripting and plug-ins) is by no means finished and won't be for a while, so ES and CEP can't be cut off. Another reason for continued support is the zillions of ExtendScript scripts out in the world which Adobe wouldn't want to disable.

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 11, 2025 Aug 11, 2025

Thanks for all that. I started with ExtendScript but gathered from the doc or comments somewhere that to use the debugger I needed to use the UXP dev tool; not to mention I wanted to use more-current JavaScript.

 

I've gotten quite a bit done with UXP and the VS Code plug-in, along with the UXP debugger (despite it pretending to provide an editor.... which doesn't change save the file when you tell it to). But this invalid-paragraph bug is pretty crippling, at least in terms of delivering a reliable plug-in to my team.

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 11, 2025 Aug 11, 2025
quote

The messaging is that CEP is deprecated.


By @Thomas_Calvin

 

In addition to the thorough explanations by @Peter Kahrel:

 

CEP is a plug-in architecture (to put it in simple terms). The overwhelming majority of InDesign scripters never use it anyway. The deprecation of CEP will largely pass unnoticed in the world of InDesign scripting (and, again, others will correct me if I'm wrong).

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 11, 2025 Aug 11, 2025

Thanks. When you're trying to absorb all of this info as quickly as possible, and facing two divergent toolsets, it's not easy to keep the names straight.

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, 2025 Aug 12, 2025
LATEST

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

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