Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Convert Text to Lowercase with a GREP Utility

As I mentioned back in this earlier post, InDesign’s GREP feature lacks the ability to convert found text from upper to lower case (or vice versa). In the comments to that post, jezabellabingbong wrote:

I have 300 pages that I want to change the first character of a bullet point to lowercase. It would also be nice to remove the full stop at the end of the bullet point. As some bullet points contain several sentences, it would need to omit the full stops and capitalised sentences within the paragraphs.

So today the answer remains: No, you can’t do this in InDesign. But InDesign just uses one flavor of GREP. There are others, including some that can convert case. So think outside the box!

Here’s my original text, formatted with automatic bullets (using a paragraph style called List):

Note that some paragraphs end with a period and others don’t. We want to make all the characters at the beginning of the list items lower case and remove the period when present.

The trick is to export your text in a file format that can be manipulated by GREP (text only) while also being able to re-import it without losing all your formatting. The answer, of course, is InDesign Tagged Text. So choose File > Export, then choose InDesign Tagged Text from the Format pop-up menu. Now open that text file in a text editor that has a GREP feature. On the Mac that should probably be BBEdit or its free (but very powerful little sister) TextWrangler:

All the paragraphs are tagged with the paragraph styles, so it’s easy to search for paragraphs that start with our “list” style. Now we open the Find/Change dialog box, turn on the GREP checkbox, and type in some code. Warning: There are likely more elegant and optimized ways to write the GREP codes; feel free to note your favorite ways of doing this in the comments below.

In this case, I’ve written GREP code

(<ParaStyle:list>)([A-Z])(.+)([a-z])\.?$

which searches for any string of text that starts with the “list” paragraph style tag, followed by a single uppercase character, followed by any string of text, ending with any lowercase character that may or may not be followed by a period at the end of the paragraph.

I replace it with the original paragraph style codes (\1), then use the \l code that converts the following character to lowercase, then insert the rest of the line of text (\2\3\4), leaving out the period at the end if one had been there. (Note that TextWrangler is using backslash-number instead of the dollarsign-number code that InDesign uses. This is another example of how different flavors of GREP work slightly differently. Be sure to read the Help files for whatever program you’re using!)

When I click Change All, it does it’s magic. The only problem is that I’ve removed the periods at the end of the bullet items that needed them (the ones that contain a second sentence in the line. So I’ll run a second GREP query to add those periods back in:

Seems like there must be a way to do both these steps with one GREP query (basically, make the first one leave in the punctuation for those paragraphs that need it), but my Monday-morning brain isn’t finding one.

Anyway, the important thing is that after I’m done, I just need to save the text file, go back to InDesign, delete the text that was there, and use File > Place to import the modified tagged text back in. The result looks exactly the same as the original, except for the changes we made:

David Blatner

David Blatner

David Blatner is the co-founder of the Creative Publishing Network, InDesign Magazine, and the author or co-author of 15 books, including Real World InDesign. His InDesign videos at Lynda.com are among the most watched InDesign training in the world. You can find more about David at 63p.com.
David Blatner

Latest posts by David Blatner (see all)

  • - November 30, -0001
Tags
Related Articles
Comments

22 Comments on “Convert Text to Lowercase with a GREP Utility

  1. A simple script does the trick:

    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.appliedParagraphStyle = "List";

    // First letter in bullet list to lower case:
    app.findGrepPreferences.findWhat = "^.";
    found = app.activeDocument.findGrep();
    for (i = 0; i < found.length; i++)
      found[i].characters[0].changecase (ChangecaseMode.lowercase);

    // Delete final period
    app.findGrepPreferences.findWhat = ".$";
    app.changeGrepPreferences.changeTo = "";
    app.activeDocument.changeGrep();

    Removing periods at the end of paragraphs can be done in the interface: the script here simply automates that: Find .$ and replace with nothing (there should be a backslash before the period, two of them in the script text, but they disappear all the time, and time is running out).

    Peter

  2. Wow… Thanks, Peter! I hate it when a simple script makes an entire blog post moot. ;)

    If anyone tries to use that script, make sure you set the curly quotes to straight quotes and change “List” to the name of your paragraph style.

    This code won’t put the quotes back on for the list items that have full sentences, but I bet that wouldn’t be hard.

  3. “This code won?t put the quotes back on for the list items that have full sentences, but I bet that wouldn?t be hard.”

    Probably not — but you didn’t mention them ;)

    P.

  4. Like all Peter’s scripts, this is an object-lesson in elegance and sparse beauty. It can also be adjusted in reasonably obvious ways to suit individual needs.

    Peter’s introduction to scripting ( ‘Scripting InDesign with JavaScript’) is brilliant, and I recommend it to everyone who wants to take InDesign to new levels.

  5. Are adobe listening though. Why no record actions button?

    Seriously – the scripts posted in the last few weeks have been nothing short of miraculous.

    Well done guys.

  6. Why no record actions button?

    Well, just thinking aloud?

    The difference between an action and a script is a bit like the difference between a dance instructor and a puppeteer.

    With an action, you make the “moves” you want the program to make, then you tell it to do the same thing on its own. It has to recognize which “moves” are relevant. With a script, you “pull the strings” of the program in such a way that it has no choice over what to do, so it doesn’t have to “recognize” what’s relevant and what isn’t.

    If you change a capital ‘A’ to a lower case ‘a’, and tell the program to “do that again, on your own this time”, it has to “recognize” the relevant thing you did, so it can imitate it. But was the relevant thing you did the replacement of a capital letter with the same letter in lower case, or was it the replacement of any old character with an ‘a’?

    My tentative guess is that recording actions in InDesign would call for so much “recognition” on the part of the program that it would be much more difficult to arrange than in Photoshop (say). So it probably isn’t an oversight so much as a practical impossibility.

  7. @Jeremy: I see what you mean, but I don’t think it’s that hard. I mean, just give us a combination of the “do again” features in Illustrator and MS Word. Replacing an “A” with an “a” is literally just a “type the letter a” command, so doing it over would type another letter “a”.

    My understanding is that doing Actions would be a huge, huge engineering effort on Adobe’s part. However, perhaps doing a “Do it Again” feature might be more doable.

  8. @ David:
    “I hate it when a simple script makes an entire blog post moot.”
    You should not. Peters Script is excellent, sure. But how many people are able to program scripts? To learn a bit more about GREP (by the help of posts like yours) could be a “workaround”. Kind of low level scripting.

  9. But of course you have to consider another thing if you change capitalcase to lowercase: there might be names at the beginning of a paragraph. How would you exclude them from changing? Especially when you run into acronyms.

  10. @Jochen: I was just joking with that comment. I think it is important to know how to do this with different methods — a script, GREP, and so on.

    @Uwe: You bring up a good point! Wow, that complicates things a lot. Unless you have a list of all the possible acronyms and names, you would probably just have to go back and proof the document for those kinds of errors.

  11. At least there are only 26 letters in English ABC. So far, the following routine is coming to mind:
    Find what (GREP): ^A
    ParaGraph style: List
    Change To: ^a
    Save the query. Give it a descriptive simple name, like “A-01”.
    Do the same for other 25 letters.

    Next time simply run 26 automatic queries saved in the memory.

    Or perhaps use the Creative Suite inherent FindChangeBy List script.

    This seems to be an alternative to scripts or BBedit grep search.

  12. If you don’t mind using All Caps, I’ve made this simple routine work on test text.

    First, protect any acronyms and proper nouns with All Caps (possibly using a character style that can be replaced later). Acronyms are easy to find/change using GREP, but if there were many proper nouns this method could be more trouble than it’s worth.

    Select the list and change it all to lower case manually. Anything already formatted with All Caps isn’t affected by the change to lower case.

    Then GREP search for single character following end-of-sentence punctuation + space.

    Change the found character format to All Caps.

    If it were a long and complex list you might have to factor in quotation marks, parentheses and so on, but that’d be possible to achieve fairly simply, I’d think, using unicode class properties.

    Of course, I’m a GREP dunderhead and have probably missed something obvious. However, for the simple task set by the OP (and ignoring for the moment the preference for full stop handling), this appears to work.

  13. I have been looking for the solution to the exact problem that Jezabellabingbong was.

    Thanks David, I have been searching my Grep books for the solution – so glad to know the reason I didn’t find the answer was because it doesn’t exist in InDesign GREP

    I used your Text Wrangler method to test out if this worked. Few things I noticed – not sure if is the same for everyone.

    ? Will not affect Paragraph styles that have any overrides (including manual kerning)
    ? I cant get the text to change for any Paragraph style that is in a Style Group (any folder in Styles Panel)
    ? Does not work with a character style applied to the text. But this can work to an advantage. Can use this to make sure important names/words never change. I tried adding the character style to the code – but it causes an error.

    I am not sure if for the Character Style and Style Group if i am entering the code incorrectly or if it is just too complex.

    Thanks

  14. Hi David, just a quick note to say thank you! Had forgotten I posted that query and only found it after searching for another solution to something else. Will try to implement your elegant solutions over the next month and report back.

    Here?s another conumdrum fro you?

    As indesigns footnotes features are primitive to say the least, we are forced to convert all footnotes to text to run them over two columns at the bottom of the page (I use convert to endnotes script to help). We try and do that as late as possible, but Sod?s law the client will come back and insert a few more, thereby messing up the numbering. I would like to run a script that searches from a chosen point to the end of the document and changes the footnote reference number both in the copy and in the footnotes themselves by a specified amount. Having 150 footnotes to manually adjust is ….. hmmm, best not print that!

  15. Wow, that was a fast reply! Will give it a try. Thank you again. Despite a love of all things automated my brain turns to mush whenever I try and remember how to compose a GREP.

  16. I don’t know whether it’s just me or if everyone else encountering problems with
    your website. It appears as if some of the written text on your posts are running off the
    screen. Can someone else please provide feedback and let me know if
    this is happening to them too? This may be a issue with my browser because I’ve had this happen before.
    Kudos

    • Hi travel,

      Thinking it might be on your end. I’m using Chrome on my desktop and the text is responsive. Checked it in Safari on my phone and it is okay there too. What browser and device are you using and experiencing the problem?

Leave a Reply

Your email address will not be published. Required fields are marked *