Skip to main content
Known Participant
May 22, 2025
Question

Highlighting non-contiguous words/phrases so a paragraph style can be assigned in one click

  • May 22, 2025
  • 2 replies
  • 760 views

I read an old question (2019) that said you cannot highlight non-contiguous words/phrases. I'm wondering if this has changed now and if there is a way to do this. I want to highlight subheadingins in chapter and assign a paragraph style and change them with one click!

 

2 replies

Barb Binder
Community Expert
Community Expert
May 22, 2025
BobLevine
Community Expert
Community Expert
May 22, 2025
Not to throw cold water on it, but 18 votes in five years? Not gonna get much traction.
BobLevine
Community Expert
Community Expert
May 22, 2025

Nothing's changed. Could likely be scripted.

If you're bringing styled text in, you could map styles from Word or do a find/change, but that requires some forethought.

Known Participant
May 22, 2025

It's been a bit of a nightmare. The Word document had been copied from a PDF (far from ideal) then amended so of course the formatting was all over the place. So I had to import into Indesgn and then assign character styles to the text (as there were italics and bold interspersed amongst the text that I wanted to keep and two styles of typeface) so I could then unformat the text but keep the italics and bold in place. I have then had to create paragraph rules to use as well and remember to change character styles to 'none' so the paragraph style would work. As it is a laborious job that is now having to be formatted by hand, being able to highlight non-contiguous text would have saved me so much time.  

BobLevine
Community Expert
Community Expert
May 22, 2025

Again, could be scripted. In fact, my guess is that even ChatGPT or any other AI could write it for you. Here's a quick one from Co-Pilot. I strongly suggest saving the document first and running it on a copy.

 

I'd be curious to know how you make out with it.

 

// InDesign Script: Convert Manual Overrides to Styles
if (app.documents.length > 0) {
    var doc = app.activeDocument;

    // Function to apply character styles
    function applyCharacterStyles() {
        var allTextFrames = doc.textFrames;
        for (var i = 0; i < allTextFrames.length; i++) {
            var textFrame = allTextFrames[i];
            var text = textFrame.texts[0];

            // Loop through each character
            for (var j = 0; j < text.characters.length; j++) {
                var char = text.characters[j];
                if (char.appliedCharacterStyle == doc.characterStyles[0]) { // Default style
                    var newStyle = findOrCreateCharacterStyle(char);
                    char.appliedCharacterStyle = newStyle;
                }
            }
        }
    }

    // Function to apply paragraph styles
    function applyParagraphStyles() {
        var allParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements();
        for (var i = 0; i < allParagraphs.length; i++) {
            var para = allParagraphs[i];
            if (para.appliedParagraphStyle == doc.paragraphStyles[0]) { // Default style
                var newStyle = findOrCreateParagraphStyle(para);
                para.appliedParagraphStyle = newStyle;
            }
        }
    }

    // Helper: Create or find a character style based on formatting
    function findOrCreateCharacterStyle(char) {
        var styleName = "CharStyle_" + char.fontStyle + "_" + char.pointSize;
        var existingStyle = doc.characterStyles.itemByName(styleName);
        if (!existingStyle.isValid) {
            var newStyle = doc.characterStyles.add({ name: styleName });
            newStyle.fontStyle = char.fontStyle;
            newStyle.pointSize = char.pointSize;
            return newStyle;
        }
        return existingStyle;
    }

    // Helper: Create or find a paragraph style based on formatting
    function findOrCreateParagraphStyle(para) {
        var styleName = "ParaStyle_" + para.justification + "_" + para.pointSize;
        var existingStyle = doc.paragraphStyles.itemByName(styleName);
        if (!existingStyle.isValid) {
            var newStyle = doc.paragraphStyles.add({ name: styleName });
            newStyle.justification = para.justification;
            newStyle.pointSize = para.pointSize;
            return newStyle;
        }
        return existingStyle;
    }

    // Run the functions
    applyCharacterStyles();
    applyParagraphStyles();

    alert("Manual overrides have been converted to styles!");
} else {
    alert("No document is open. Please open a document and try again.");
}