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

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

Explorer ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

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.
TOPICS
Scripting

Views

354

Translate

Translate

Report

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 ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

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

 

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

Votes

Translate

Translate

Report

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
Explorer ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Explorer ,
Feb 24, 2023 Feb 24, 2023

Copy link to clipboard

Copied

LATEST

Got it. Now just to remember it next time I need to do something like this. It will help if I ever get Intellisence working on Visual Studio Code.

Votes

Translate

Translate

Report

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