Skip to main content
Inspiring
February 24, 2023
Question

String comparison returns false, but I don't know why

  • February 24, 2023
  • 1 reply
  • 790 views

Hi all

 

I'm hoping someone can see the error in the code below. I cannot. This very simple script is supposed to look at the last character of every line and, if it's a space, remove underlining from the space. It worked until I added the if statement (which I had to do because without it, the script was removing the underlining from a letter that preceded an automatic hyphen). As its written now, it fails to find the spaces at the end of lines.

 

I'm grateful (as always) for any help. Here's the script.

 

myLines = app.selection[0].lines;
     for (i=0; i<myLines.length; i++){
          if (myLines[i].characters[-1] === " ") {
               myLines[i].characters[-1].underline = false;
          }
     }
 
Just FYI, I tried using "==" vs. "===" with no change.
This topic has been closed for replies.

1 reply

brian_p_dts
Community Expert
Community Expert
February 24, 2023

You need to look at the character's contents property: 

 

myLines[i].characters[-1].contents

Inspiring
February 24, 2023

Thank you so much, Brian. That was it. I have a lot of experience with VBA, but I'm still JavaScript novice. I would never have thought to add ".content" after "characters[i]." Lesson learned.

brian_p_dts
Community Expert
Community Expert
February 24, 2023

Glad I could help. For more details, check out the InDesign DOM: .https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

A Character object has many properties, not just the actual value of the string. You are calling another one of those properties with .underline