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

How do you compare a paragraph's contents to a string?

Engaged ,
Jun 24, 2025 Jun 24, 2025

Seems simple, right? And yet this doesn't work (you can see the debugger showing that the contents are "NOTE"), even when I use the more-permissive "==" comparison:

 

Screenshot 2025-06-24 170516.png

TOPICS
Scripting , UXP Scripting
302
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 ,
Jun 24, 2025 Jun 24, 2025

Your paragraph also likely will have a return character \r in it too. Debug by inspecting currPara.contents with an extra quote around it. Be curious as to why behavior you would expect doesn't turn out to be true.

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 ,
Jun 25, 2025 Jun 25, 2025

Thanks for the reply. Yes, I suspected that it included the return character, and proved that was true by printing the character count (5). But then I tried comparing the first word to the string, and that also failed. So it's not clear what entity will match a string in quotes.

 

A Web search on the issue turns up nothing but wrong examples.

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
Valorous Hero ,
Jun 25, 2025 Jun 25, 2025

It's not as simple as it seems! 🙂

InDesign text may contain and, in most cases, does contain special (hidden) characters like anchored objects, indexes, xrefs, etc.

To handle them effectively by script, I wrote a couple of functions and posted them here.

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 ,
Jun 25, 2025 Jun 25, 2025

Thanks! Can't access that site, though. It times out.

 

Also, you'd think an API to a word-processing application would anticipate the need to compare strings. Especially if I ask for a word.

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 ,
Jun 25, 2025 Jun 25, 2025

You can test a regular expression against the contents:

 

/note/gi.test(myPara.contents)

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 ,
Jun 25, 2025 Jun 25, 2025
LATEST

Thanks. That's probably the most reliable way.

How would one extract a substring by index (from a paragraph) and compare it to a string? I'm not sure at what point (if any) there's a plain JavaScript string to be had for a direct comparison.

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 ,
Jun 25, 2025 Jun 25, 2025

Try == instead of ===

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
Valorous Hero ,
Jun 25, 2025 Jun 25, 2025

@Peter Kahrel He tried this and it didn't work: see the OP.

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 ,
Jun 25, 2025 Jun 25, 2025

Oops

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 ,
Jun 25, 2025 Jun 25, 2025

Ha ha, NP; it's not totally obvious how many = there are with some fonts.

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