Skip to main content
Participating Frequently
October 19, 2022
Answered

Multiple Para Styles

  • October 19, 2022
  • 2 replies
  • 409 views

Hello. I have the following script that verifies and corrects uppercase acronyms on headings—but is limited to a single paragraph style. It would be a significant improvement to make it work on multiple paragraph styles. Also, this function requires the word acronym for every entry; perhaps there is a way to clean up the list. Thank you guys.

 

app.findChangeTextOptions.caseSensitive = false;

app.findChangeTextOptions.wholeWord = true;

 

acronym ('ABC');

acronym ('BBC');

acronym ('CBS');

acronym ('FCC');

 

function acronym (myFind) {

   app.findTextPreferences.findWhat = myFind;

   app.findTextPreferences.appliedParagraphStyle = "Heading1";

   app.changeTextPreferences.capitalization = Capitalization.ALL_CAPS;

   app.activeDocument.changeText();

This topic has been closed for replies.
Correct answer m1b

Hi @joaquinm13464128, see how you go with this:

var acronyms = [
    'ABC',
    'BBC',
    'CBS',
    'FCC'
];

var styleNames = [
    'Heading1',
    'Heading2',
    'Heading3',
    'Heading4'
];


function main() {

// string all the acronyms together for the grep
var findWhat = '(?i)\\b(' + acronyms.join('|') + ')\\b';

// gather all the paragraphs in these styles
var found = findTextInParagraphStyles(app.activeDocument, findWhat, styleNames);

// change them all to CAPS
for (var i = 0; i < found.length; i++)
    found[i].contents = found[i].contents.toUpperCase();


};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Capitalize Acronyms');


/**
 * Find text set in all multiple
 * paragraph styles.
 * @author m1b
 * @version 2022-10-19
 * @param {Document|Page|Story} findWhere - any Indesign Object with a findGrep method.
 * @param {String} findWhat - the grep string.
 * @param {Array<String>} styleNames - the paragraph style names to find.
 * @returns {Array<Paragraph>}
 */
function findTextInParagraphStyles(findWhere, findWhat, styleNames) {

    if (styleNames.constructor.name == 'String')
        styleNames = [styleNames];

    if (!findWhere.hasOwnProperty('findGrep'))
        throw Error('findTextInParagraphStyles cannot find in "' + findWhere + '".');

    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    var found = [];

    // find criterion
    app.findGrepPreferences.findWhat = findWhat;

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

        try {

            // find criterion
            app.findGrepPreferences.appliedParagraphStyle = styleNames[i];

            // do the find
            found = found.concat(findWhere.findGrep());

        } catch (error) {
            /* paragraph style not found */
        }

    }

    return found;

};

- Mark

2 replies

Participating Frequently
October 19, 2022

Hi Mark,

It works very well! This is a true piece of art now. Now it's going to be much easier to populate the list and work with multiple para styles. Thanks again for the quick response and your effort.

Best,

Joaquin

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 19, 2022

Hi @joaquinm13464128, see how you go with this:

var acronyms = [
    'ABC',
    'BBC',
    'CBS',
    'FCC'
];

var styleNames = [
    'Heading1',
    'Heading2',
    'Heading3',
    'Heading4'
];


function main() {

// string all the acronyms together for the grep
var findWhat = '(?i)\\b(' + acronyms.join('|') + ')\\b';

// gather all the paragraphs in these styles
var found = findTextInParagraphStyles(app.activeDocument, findWhat, styleNames);

// change them all to CAPS
for (var i = 0; i < found.length; i++)
    found[i].contents = found[i].contents.toUpperCase();


};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Capitalize Acronyms');


/**
 * Find text set in all multiple
 * paragraph styles.
 * @author m1b
 * @version 2022-10-19
 * @param {Document|Page|Story} findWhere - any Indesign Object with a findGrep method.
 * @param {String} findWhat - the grep string.
 * @param {Array<String>} styleNames - the paragraph style names to find.
 * @returns {Array<Paragraph>}
 */
function findTextInParagraphStyles(findWhere, findWhat, styleNames) {

    if (styleNames.constructor.name == 'String')
        styleNames = [styleNames];

    if (!findWhere.hasOwnProperty('findGrep'))
        throw Error('findTextInParagraphStyles cannot find in "' + findWhere + '".');

    // reset grep prefs
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;

    var found = [];

    // find criterion
    app.findGrepPreferences.findWhat = findWhat;

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

        try {

            // find criterion
            app.findGrepPreferences.appliedParagraphStyle = styleNames[i];

            // do the find
            found = found.concat(findWhere.findGrep());

        } catch (error) {
            /* paragraph style not found */
        }

    }

    return found;

};

- Mark

Peter Kahrel
Community Expert
Community Expert
October 19, 2022

If the font used in the paragraph styles has AllCaps, then it's easier to change the styles. Often that's a better solution because the underlying text is still upper/lower case, which may still be required in index markers, running headers, etc.

m1b
Community Expert
Community Expert
October 19, 2022

Thanks Peter. It's funny, because I deliberately changed it to a hard change because it bothered me that any future resetting or changing of the style would revert it to the wrong case. Definitely would come down to specifics of the job I guess.