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

GREP find & change question

New Here ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

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

TOPICS
How to , Scripting , Type

Views

410

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
Participant ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

Sorry, I don't think this is possible. 

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
Participant ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

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.

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

Already done it 😛 

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

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();




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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

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)

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

Thank you Eugene, this script wordks fine.

I like it that it shows you where the inconsistent styles are, so i can still decide if i want to change it.

It would be handy if there would also be a 'change' button in the pop-up window. Just like in a find&change window.

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 ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

LATEST

Yeh, I'm a beginner scripter - and where I can find it - I can't get it to zoom to the change - and adding more controls is confusing me. But I'll take another look - usually a break from it can be clarifying.

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

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.

Screenshot 2024-04-15 at 18.31.43.png

- 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');

 

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

Cool - might use that one myself too. 

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
Participant ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

Wonderful! That textStyleRanges function looks really useful. Quick and easy way for finding words with multiple styles applied. Only issue I found is where there is punctuation at the end of the word that should remain unstyled. Just a bit more logic to check for that condition …

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

> Only issue I found is where there is punctuation at the end of the word that should remain unstyled.

 

@Tim Sheasby yep, that's how Indesign decides what is a "word".

- Mark

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

@m1b 

 

But this will waste time on re-styling EVERY word in the document? Even if there are only 10 results to fix out of 10k...

 

Even in your screenshot - it's 11 out of 29.

 

It would be much more optimal to get all TextStyleRanges, then check first and last words and fix only those.

 

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

That's a really great idea @Robert at ID-Tasker!

 

Here is a version that I hope follows your suggestion and should be faster on a large document:

 

/**
 * 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 and idea to use first and last word of all textStyleRanges by RobertTkaczyk
 * @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 textStyleRanges = everyStory.textStyleRanges.everyItem().getElements();

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

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

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

        len = textStyleRanges[i].words[0].isValid && textStyleRanges[i].words[0].textStyleRanges.length || 0;

        if (len > 1)
            normalizeStyle(textStyleRanges[i].words[0]);

        len = textStyleRanges[i].words[-1].isValid && textStyleRanges[i].words[-1].textStyleRanges.length || 0

        if (len > 1)
            normalizeStyle(textStyleRanges[i].words[-1]);

    }


    /**
     * Given a text, removes all textStyleRanges
     * except the last, keeping the contents in-tact.
     * @param {Text} text - the text object to normalize.
     */
    function normalizeStyle(text) {

        len = text.textStyleRanges.length;

        // consolidate the contents into the last text style range
        text.textStyleRanges[len - 1].contents = text.contents;

        // delete the other text style ranges
        while (--len)
            text.textStyleRanges[0].remove();

    };

};

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

 

 

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

@m1b 

 

Almost - there is no point in getting TSRs of every separate Paragraph - just TSRs of a Story/Cell/Footnote.

 

This way - you'll save even more "time" when there are big blocks of text - multiple Paragraphs - with the same formatting.

 

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 ,
Apr 15, 2024 Apr 15, 2024

Copy link to clipboard

Copied

Oops! That was left over from the script I took it from that needed paragraphs. Good Call!

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

Ok guys, you were so fast 😉

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

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