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

Finding two different characters in same paragraph

New Here ,
Nov 15, 2017 Nov 15, 2017

Hello!

Last time i have asked for some help, experts here gave me huge advice and support (thank you)

Unfortunately, i was hoping to find some solution in existing topics, but solutions that i found (nested styles) won't really help me, i guess.

I am coming back here with another problem with work automation in InDesign: i'm trying to find two different characters within same paragraph: bold and regular, and make whole word bolded (just change this regular letter). Regular letter has no character style applied, just paragraph style. Sometimes word to change happens in beggining of paragraph, but sometimes in another place (there is no rule for that, unfortunately). Here is the situation:

In addition, paragraphs with this situation has different styles applied, and i don't want to change this. Is any way to fix this with script? Or GREP?

Thank you for any advice.

TOPICS
Scripting
1.2K
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

correct answers 1 Correct answer

Enthusiast , Nov 15, 2017 Nov 15, 2017

for (var i=app.selection[0].parentStory.words.length-1; i>=0; i--) {

    if (app.selection[0].parentStory.words.textStyleRanges.length > 1) {

        for (var j=app.selection[0].parentStory.words.textStyleRanges.length-1; j>=0; j--) {

            if (app.selection[0].parentStory.words.textStyleRanges.appliedCharacterStyle.name == "bold") {

                app.selection[0].parentStory.words.appliedCharacterStyle = "bold";

            }

        }

    }

}

Of course, not the only right way to do this, but...

...
Translate
Enthusiast ,
Nov 15, 2017 Nov 15, 2017

I don't know how to make it using JavaScript (read the (Mixed) charStyle), but, if you have a list of the words that must be in bold, GREP is the easiest way to deal with this problem.

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
LEGEND ,
Nov 15, 2017 Nov 15, 2017

Hi corullon! … 

Hi Krzysztofa,

Do you only want to search words not completely in "bold"?

… and of course, not find words totally in "bold"!

(^/)

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 ,
Nov 15, 2017 Nov 15, 2017

Krzysztof -- Something along these lines:

1. Get all words (e.g. \w+)

2. For each word, if it has two (or more) textStyleRanges and one is bold, make the whole word bold.

P.

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
Enthusiast ,
Nov 15, 2017 Nov 15, 2017

for (var i=app.selection[0].parentStory.words.length-1; i>=0; i--) {

    if (app.selection[0].parentStory.words.textStyleRanges.length > 1) {

        for (var j=app.selection[0].parentStory.words.textStyleRanges.length-1; j>=0; j--) {

            if (app.selection[0].parentStory.words.textStyleRanges.appliedCharacterStyle.name == "bold") {

                app.selection[0].parentStory.words.appliedCharacterStyle = "bold";

            }

        }

    }

}

Of course, not the only right way to do this, but... as I friend told me: if it's working; it's working.

Thanks, Peter!

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 ,
Nov 15, 2017 Nov 15, 2017

On reflection, this is probably quicker:

1. Find instances of bold

2. For each instance of bold, if its parent word contains another style, apply bold to the whole parent 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
LEGEND ,
Nov 15, 2017 Nov 15, 2017

Hi Peter,

How do you define here "parent word"? words[0] doesn't work!

(^/)

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
Enthusiast ,
Nov 15, 2017 Nov 15, 2017

Did you mean something like this:

var mySel = app.selection[0].parentStory;

app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

app.findGrepPreferences.findWhat = "\\w+";

app.findGrepPreferences.appliedCharacterStyle.name = "bold"

var myFound = mySel.findGrep();

app.findGrepPreferences = app.changeGrepPreferences = app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;

for (var i=myFound.length-1; i>=0; i--) {

    if (myFound.textStyleRanges.length > 1) {

        myFound.appliedCharacterStyle = app.activeDocument.characterStyles.itemByName("bold");

        }

    }

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
New Here ,
Nov 15, 2017 Nov 15, 2017

Yes! Thank you very much, works like a charm

I should study JavaScript more, it's so powerful tool...

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
LEGEND ,
Nov 15, 2017 Nov 15, 2017
LATEST

Peter,

I've effectively taken your 2nd way!

Some weeks ago, someone asked me to write a script to detect this kind of problem!

… and "only find" the 4 words with partial bold!

Capture d’écran 2017-11-15 à 17.24.56.png

This person knew the corrections needed to be "manual"! …

… Because, if the 3 first needed to be in bold, not the 4th [just the first letter in bold!

Capture d’écran 2017-11-15 à 18.12.12.png

So the script "jumps" [using a shortcut] through the words with problem, selecting one after the other (and only them: partial bolded) and the op can decide how to correct them: "Bold or not bold! … That is the question!!"

Make an "all replace" is, by experience, very dangerous! … and that wasn't my client's choice!

I discovered at this moment that [JS] considers "Peter)," as a "word" in the sentence "… Peter),"!

Rather boring! That's why I said ".words[0]" doesn't work correctly!

(^/) 

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
New Here ,
Nov 15, 2017 Nov 15, 2017

Peter Kahrel​ thank you sir! So, as i understood, there is no possible way to do this task with GREP?

lf.corullon​ i do not have list of these words it would be so easy to do. But i don't really know which words i should change. I think best method is, as Peter said, to search words "if its parent word contains another 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 ,
Nov 15, 2017 Nov 15, 2017

Speaking about stealing thunder. . .

Krzysztof -- Here's a script that looks for bold and makes parent words bold. Before you use it, in the first line, specify the name of your bold font. In the script it's 'Semibold', change to Bold, Black, 85 Roman, whatever.

bold = 'Semibold';

app.findGrepPreferences = null;

app.findGrepPreferences.fontStyle = bold;

b = app.documents[0].findGrep();

for (i = 0; i < b.length; i++) {

  word = b.words[0];

  ranges = word.textStyleRanges.everyItem().fontStyle;

  if (ranges.length && ranges.join('').indexOf(bold) > -1) {

    word.fontStyle = bold;

  }

}

Michel -- Why do you think that words[0] doesn't work?

P.

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