Skip to main content
Participating Frequently
April 15, 2024
Question

GREP find & change question

  • April 15, 2024
  • 4 replies
  • 1112 views

Hi,

 

is there a way, by using grep, to look for words, whose first letter has no character style and de rest does?

For exemple: community or guidelines or searching

 

I hope there is, because that would help me alot.

 

Regards

Arne

This topic has been closed for replies.

4 replies

puurprintAuthor
Participating Frequently
April 23, 2024

Ok guys, you were so fast 😉

I've tested both scripts and they both do the job!

m1b
Brainiac
April 15, 2024

Hi @puurprint, I've written a script, along the same lines as @Eugene Tyson has done, but using the `textStyleRanges` property of Words. You could try this also. It is very simple and was super quick to write, but on a large document may be very inefficient. All it does is, when a word has multiple text style ranges, consolidate the word's contents into the last style and remove the earlier ones.

 

It works, with fontStyles, eg. Bold, Italic, All Caps, etc. and also Character Styles.

- Mark

/**
 * Normalize Word Styles.js
 * Finds all words in document and, if any word has multiple
 * text styles, apply the last to the whole word.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/grep-find-amp-change-question/m-p/14555455
 */
function main() {

    var doc = app.activeDocument;
    var everyStory = doc.stories.everyItem();
    var words = everyStory.words.everyItem().words.everyItem().getElements();

    if (everyStory.tables.everyItem().cells.everyItem().words.length)
        words = words.concat(everyStory.tables.everyItem().cells.everyItem().words.everyItem().getElements())

    if (everyStory.footnotes.everyItem().words.length)
        words = words.concat(everyStory.footnotes.everyItem().words.everyItem().getElements());

    for (var i = words.length - 1, len; i >= 0; i--) {

        len = words[i].textStyleRanges.length;

        if (len <= 1)
            continue;

        // consolidate the contents in the last tsr
        words[i].textStyleRanges[len - 1].contents = words[i].contents;

        // delete the other tsrs
        while (--len)
            words[i].textStyleRanges[0].remove();

    }

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Normalize Word Styles');

 

Adobe Expert
April 15, 2024

Cool - might use that one myself too. 

Adobe Expert
April 15, 2024

No - because it has to find a pattern - and you can't find 2 styles at the same time 

You're looking for 

Word that begins with Regular typeface
Followed by a word with a Bold typeface

In the find dialog you can only find 1 type of style

There's no GREP for bold you can't find
\l<bold> as there's no GREP for bold or underline for that matter (be cool if there was)

The only option you have is to find 1 type of style
So if you search for Regular typeface you can't also search for a lookahead for Bold or Underline

There's only 1 entry for this 


But I devised this mini script that you can run 
save it to a text file and change the extension to .jsx and drop into the Scripts Folder

// Function to check if a text run has inconsistent styles
function hasInconsistentStyles(text) {
    var firstStyle = text.characters[0].appliedCharacterStyle;
    for (var i = 1; i < text.characters.length; i++) {
        if (text.characters[i].appliedCharacterStyle != firstStyle) {
            return true; // Inconsistent styles found
        }
    }
    return false; // All characters have the same style
}

// Main function to iterate through text and find inconsistencies
function findInconsistentStyles() {
    var doc = app.activeDocument;
    var textFrames = doc.textFrames;
    var found = false; // Flag to indicate if inconsistent text is found
    
    for (var i = 0; i < textFrames.length; i++) {
        var text = textFrames[i].texts[0]; // Access the first text range in the text frame
        
        for (var j = 0; j < text.words.length; j++) {
            var word = text.words[j];
            
            if (hasInconsistentStyles(word)) {
                // Select the inconsistent text
                word.select();
                found = true;
                
                // Prompt user to continue
                if (!confirm("Inconsistent text found. Continue to the next instance?")) {
                    return; // User chose to stop
                }
            }
        }
    }
    
    // If no inconsistent text is found
    if (!found) {
        alert("No inconsistent text found in the document.");
    }
}

// Call the main function
findInconsistentStyles();




Adobe Expert
April 15, 2024

You do need to have character styles applied for it to work

You can find Preptext script online - and run this first, this will auto apply character styles throughout your document.
Then you can run this script and it will identify each inconsistent style (hopefully)

Inspiring
April 15, 2024

Sorry, I don't think this is possible. 

Inspiring
April 15, 2024

I think this could be done with a script: search for words with no style on first character then check if the rest of the word has a character style applied.

Adobe Expert
April 15, 2024

Already done it 😛