Skip to main content
Participating Frequently
July 6, 2023
Answered

Grep for looking for a specific character in previous line.

  • July 6, 2023
  • 1 reply
  • 194 views

I am trying to make a grep code to apply to a style to look for the previous line (ends in paragraph return) where if the line contains parentheses (). Not even sure if its possible but everything I have tried hasnt worked. Any ideas would be more than appreciated.

This topic has been closed for replies.
Correct answer m1b

Okay, here's a script that implements the above logic.

- Mark

 

/**
 * Applies a paragraph style to any paragraph
 * following a paragraph containing parentheses.
 * @7111211 m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/grep-for-looking-for-a-specific-character-in-previous-line/m-p/13918906
 */

function main() {

    var doc = app.activeDocument,

        // the grep to find the paragraph after
        // a paragraph containing parentheses
        myFindWhat = '\\([^\\)\\r]+\\).*\\r\\K.*',

        // put your paragraph style name here
        changeToStyleName = "MyAfterParenthesesStyle",

        // get the paragraph style
        changeToStyle = doc.paragraphStyles.itemByName(changeToStyleName);

    if (!changeToStyle.isValid) {
        alert('There is no paragraph style named "' + changeToStyleName + '".');
        return;
    }

    // the find/change grep
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = myFindWhat;
    app.changeGrepPreferences.appliedParagraphStyle = changeToStyle;
    doc.stories.everyItem().paragraphs.everyItem().changeGrep();

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Format Paragraphs");

1 reply

m1b
Community Expert
Community Expert
July 6, 2023

Hi @BryceRobertsGD, I don't think you'll be able to use a GREP style for this case, because the scrope of a paragraph style is just that one paragraph.

 

Here is an example of a grep to match the paragraph following a paragraph containing parentheses:

\([^\)\r]+\).*\r\K.*

But it won't work as a grep style in a paragraph style.

 

However, you tagged "Scripting". Are you looking for a scripting solution? This would perform the grep search and set the paragraph style of the found paragraphs. Would that help?

- Mark

Participating Frequently
July 6, 2023

A script would be nice there are a lot of variables in this so I figgured paragraph styles would be the simplest solution. But escentially the project has stories structured like this:
NAME

Date

Story (sometimes the story is in a nested text box)


The problem Im facing is that if the name has parentheses (and depending on the font some other characters) they go below the baseline causing the spacing between the name and date to be too close. So I have to manually adjust those but if there is no decenters present the date needs to remain at the same height. 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 7, 2023

Okay, here's a script that implements the above logic.

- Mark

 

/**
 * Applies a paragraph style to any paragraph
 * following a paragraph containing parentheses.
 * @7111211 m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/grep-for-looking-for-a-specific-character-in-previous-line/m-p/13918906
 */

function main() {

    var doc = app.activeDocument,

        // the grep to find the paragraph after
        // a paragraph containing parentheses
        myFindWhat = '\\([^\\)\\r]+\\).*\\r\\K.*',

        // put your paragraph style name here
        changeToStyleName = "MyAfterParenthesesStyle",

        // get the paragraph style
        changeToStyle = doc.paragraphStyles.itemByName(changeToStyleName);

    if (!changeToStyle.isValid) {
        alert('There is no paragraph style named "' + changeToStyleName + '".');
        return;
    }

    // the find/change grep
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = myFindWhat;
    app.changeGrepPreferences.appliedParagraphStyle = changeToStyle;
    doc.stories.everyItem().paragraphs.everyItem().changeGrep();

}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Format Paragraphs");