Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Changing the paragraph style of the NEXT paragraph using Find or GREP

Return to Member Forum

  • Author
    Posts
    • #60751
      Glenn Kramer
      Member

      Hi InDesign users, is there a way that I can automatically change the paragraph style of the next paragraph using Find or GREP? For example: I want to search for all the H2s, then have the paragraph style “No Indent Body” be applied to the paragraph after that. There should be an easy way to do that, but I haven't figured it out. Thank you!

    • #60752

      Can't be done, it needs a script.

    • #60753

      .. whacked this out in 30 seconds:

      app.findTextPreferences = null;
      app.findTextPreferences.appliedParagraphStyle = “H2”;
      var myResults = app.activeDocument.findText();
      for (i=0; i<myResults.length; i++)
      myResults[i].parentStory.paragraphs.nextItem(myResults[i].paragraphs[0]).appliedParagraphStyle = “No Indent Body”;

      • #116293
        Robert Kern
        Member

        Hi Theunis,

        I tried your script but it kicked out the following error:

        Error Number 8:
        Error String: Syntax error

        Line 5:
        Source: app.findTestPreferences.appliedParagraphStyl,e = “H2″;
        Offending Text: ”

        I’m a newbie to scripting, users not a writer, but it seemed simple enough to modify our script to the tags we use (H1, H2, H3, and Body1).
        Any thoughts on how to adjust the script, below, to fix the problem is greatly appreciated!

        app.findTextPreferences = null;
        app.findTextPreferences.appliedParagraphStyle = “H2”;
        var myResults = app.activeDocument.findText();
        for (i=0; i<myResults.length; i++)
        myResults[i].parentStory.paragraphs.nextItem(myResults[i].paragraphs[0]).appliedParagraphStyle = “Body1”;

      • #116306

        The prettifying software of this forum automatically changed straight single and double quotes to “correct” curly opening and closing. And JavaScript cannot handle that! There is a minimal hint to this if you copied the code into Adobe’s own ExtendScript Toolkit Editor, or another code editor that does syntax-coloring: you can see text strings get a different color when you use the ‘correct’ type of quotes.

        Trying again below; if it doesn’t work, delete the quotes one by one and replace them with *correct* correct straight ones.

        app.findTextPreferences = null;
        app.findTextPreferences.appliedParagraphStyle = "H2";
        var myResults = app.activeDocument.findText();
        for (i=0; i<myResults.length; i++)
        	myResults[i].parentStory.paragraphs.nextItem(myResults[i].paragraphs[0]).appliedParagraphStyle = "No Indent Body"; 
        
    • #60758
      Eugene Tyson
      Member

      It can be done in a 3 step Find/Change though (I know the brilliant Jongware has already made a script)

      1.

      Find GREP

      Find What:

      Leave Blank

      Change to:

      $0~

      (the ~ can be any character you're sure is not used in the document)

      Find Format:

      Heading 2

      Change Format:

      Leave blank

      That will add an “~” (or any character you choose) to the end of all Heading 2 headers

      2.

      Find GREP

      (?<=~r).+

      Change format:

      Choose the indent or formatting you require

      3.

      Find GREP

      Find What:

      ~

      Change to:

      Leave blank

      (clear change and find formats)

    • #60760

      Ah, how ingenious mr. Bunt.

      Actually, I may have used your method before I knew how to script and thus had to use my brains!

    • #60761
      Eugene Tyson
      Member

      I'm sure your way is a lot better there Jongware.

      The problem with my method is that you'd have to do this for new paragraphs being inserted or large amounts of paragraphs swapping position or some other sort of weird editing.

      A one step solution is better than 3.

      So if there was a large amount of editing, would your script change the other paragraphs too?

    • #60762

      I presume you mean you might move “1st paragraph after a heading” and “any next paragraphs” around, and there should always be a “don't indent” style after any sort of heading (*) and “indent” after any “don't indent” OR “indent” style.

      I bet Framemaker does this out-of-the-box :) but in InDesign you'd have to run a script after every major edit.

      (*) Typically, one would never indent a plain text paragraph after a heading or some white space, such as at the end of a list.

    • #60763

      … Here is a script that does just that. Warning: it may change more than you want, and it's hard to see what it changed! Uncomment the fill-with-color lines if you need to know where the changes occurred (and run on a copy).

      indentStyle = “Body-Indent”;
      noIndentStyle = “Body-No Indent”;

      if (app.documents.length > 0 && app.selection.length == 1 && app.selection[0].hasOwnProperty(“baseline”))
      {
      story = app.selection[0].parentStory.paragraphs;
      lastStyle = null;
      for (i=0; i<story.length; i++)
      {
      // Do we need to change?
      if (story[i].appliedParagraphStyle.name == indentStyle ||
      story[i].appliedParagraphStyle.name == noIndentStyle)
      {
      // Do we want an indent?
      if ((lastStyle == indentStyle || lastStyle == noIndentStyle))
      {
      lastStyle = indentStyle;
      if (story[i].appliedParagraphStyle.name == noIndentStyle)
      {
      story[i].appliedParagraphStyle = indentStyle;
      // story[i].fillColor = “Red”;
      }
      } else if (!(lastStyle == indentStyle || lastStyle == noIndentStyle))
      {
      lastStyle = noIndentStyle;
      if (story[i].appliedParagraphStyle.name == indentStyle)
      {
      lastStyle = story[i].appliedParagraphStyle = noIndentStyle;
      // story[i].fillColor = “Green”;
      }
      }
      } else
      {
      lastStyle = null;
      }
      }
      }

    • #60764
      Eugene Tyson
      Member

      Cool

    • #60768
      Glenn Kramer
      Member

      Thank you Jongware and Eugene! You are both brilliant. I couldn't figure out how to do this. I'll apply these techniques on the very next publication after I import the Word document text.

    • #60771
      Amy Gilbert
      Participant

      This is great. Thank you!

Viewing 10 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads