InDesign JavaScript: Copying GREP Styles

Recently one of our clients asked us to bring some InDesign templates up to their corporate specs. Part of this was inserting a set of GREP styles into the styles already contained in the documents. GREP styles are great ways to automatically apply formatting to particular text whenever it is entered, removing the obligation from the user to remember what phrases are non-breaking or italicized. Unfortunately, if you have to add GREPs to many existing style, the process is repetitive, slow and error-prone. The solution is InDesign JavaScript.

InDesign JavaScript to the Rescue!

The solution is a script. InDesign has good support for JavaScript, which works for both Windows and OSX versions of InDesign. The down-side to InDesign scripting is that there is not as much knowledge out there on the web, at least when you compare it to JavaScript for HTML. So, in the spirit of increasing human-kind’s knowledge base, here is a script for copying GREP styles. The original code came from a script found on the Adobe Community forums. Then we added the ability to work with styles that are in style groups, up to 2 levels deep. Enjoy!

//This utility will copy GREP styles into styles that are nested in up to two levels of groups i.e. Main Style Group>Headings Group>Heading 1
//Copy the text below to a text editor like NotePad or TextEdit, then save as a file with a “.js” ending.
//Copy it into the Scripts Panel folder inside the Scripts folder inside the InDesign application folder. 
//Create a style at the lowest level of the Paragraph Styles i.e. NOT in a style group.
//Put all the GREP styles you wich to copy into this style.
//Type the style name between quotes in the line below. Then run the script and select the styles to which you want to copy the GREP styles.
var source = 'GREPSourceStyle';
var theDoc = app.activeDocument;  
var pStyles = theDoc.allParagraphStyles;
var pStyleStringList = []; 
var getpStyleIndexinpStyles = selectpStyle(pStyleStringList);
var selectedpStylesByName = getSelectedpStyleNames(getpStyleIndexinpStyles);
l = selectedpStylesByName.length;

function fillpStyleStringList(){  
  for(i = 0 ; i < pStyles.length; i++){  
    if(pStyles[i].parent.parent.toString() === '[object ParagraphStyleGroup]'){
      pStyleStringList.push('Group: ' + pStyles[i] + ', Subgroup: ' + pStyles[i] + ', Name: ' + pStyles[i].name);
    }else if(pStyles[i].parent.toString() === '[object ParagraphStyleGroup]'){
      pStyleStringList.push('Subgroup: ' + pStyles[i] + ', Name: ' + pStyles[i].name);
      pStyleStringList.push('Name: ' + pStyles[i].name);

function selectpStyle (array){
  var myWindow = new Window ("dialog", "Please select your target paragraph styles.");
  var myInputGroup = myWindow.add ("group");
  var select = myInputGroup.add ("listbox", [0, 0, 300, 300], array, {scrolling: true, multiselect: true});
  var myButtonGroup = myWindow.add ("group");
  myButtonGroup.add ("button", undefined, "OK");
  myButtonGroup.add ("button", undefined, "Cancel");
  if ( == 1){
    var mySelection = select.selection;
    var tmpList = [];
    for(g = 0; g < mySelection.length; g++){
    return tmpList;

function getSelectedpStyleNames(getpStylesIndexinpStyles){
  var currentTargetpStyleName;
  var SelectedNameArray = new Array();
  for(j = 0; j < getpStylesIndexinpStyles.length; j++){
    var tempArray = new Array(2);
    currentTargetpStyleName = pStyles[getpStyleIndexinpStyles[j]].name;
    currentTargetpStyleSubgroup = pStyles[getpStyleIndexinpStyles[j]];
    currentTargetpStyleGroup = pStyles[getpStyleIndexinpStyles[j]];
    tempArray[0] = currentTargetpStyleGroup;
    tempArray[1] = currentTargetpStyleSubgroup;
    tempArray[2] = currentTargetpStyleName;
    SelectedNameArray[j] = tempArray;
  return SelectedNameArray;

function setGrepStyle(targetGroup, targetSubgroup, targetName){
  var target
  error = "";
  basepStyle = theDoc.paragraphStyles.item(source);
  if (!basepStyle.isValid) error = 'Source style does not exist';
  if(targetGroup != "" && targetGroup != && targetGroup !={
    var temptarget = theDoc.paragraphStyleGroups.itemByName(targetGroup.toString());
    target = temptarget.paragraphStyleGroups.itemByName(targetSubgroup.toString()).paragraphStyles.itemByName(targetName.toString());
  }else if(targetSubgroup != "" && targetSubgroup != && targetSubgroup !={
    target = theDoc.paragraphStyleGroups.itemByName(targetSubgroup.toString()).paragraphStyles.itemByName(targetName.toString());
    target = theDoc.paragraphStyles.itemByName(targetName.toString());
  if (!target.isValid) error += 'rTarget style does not exist';
  if (error != ""){alert (error); exit()}
    gs = basepStyle.nestedGrepStyles;
  for (i = 0; i < gs.length; i++){
    target.nestedGrepStyles.add (gs[i].properties);

Modular Documents

Corporate users in marketing and sales departments frequently need to produce complex documents. Proposals and RFQ/RFPs fit into this category. Modular documents make it easier to produce these files, but Microsoft Word is is not modular by nature.

Users need to add and remove sections of the document to increase the relevance to the potential buyer. If you’ve used Microsoft Word for more than a few minutes, you’ve noticed that its documents are not modular in the least. Each section in a document is dependent on information from other sections. Even the trained Word users find it difficult to remove a Word section or set of pages without trashing the whole file.

The wrong path: Trying to use PowerPoint for Modular Documents

For some clients, this leads them down the wrong road. They notice that PowerPoint has great page modularity. You can remove slides, move them around and add new ones without having any effect on the existing ones. So they request that their proposal template be formatted in PowerPoint. Genius brainwave? NOT!

PowerPoint is missing so many long-document functions that this is a terrible solution. What you gain in modularity, you more than lose in productivity. PowerPoint is missing typestyles, automatic tables of contents, page margins, end notes, table styles, cross-referencing, bookmarks and much more. Users can get around all of these by spending more time manually formatting, but isn’t that what the client was trying to avoid in the first place?

There are 2 alternatives for modular documents. Both are better than the PowerPoint “solution”. One is Microsoft Publisher, and the other is good old Word, used in a slightly different way.

Microsoft Publisher for Modular Documents

First, I’ll cover Publisher, because it’s less well known and deserves to be used more. Microsoft Publisher is included with many versions of Office, so it’s likely your client already has it installed. It follows many Microsoft conventions, so the learning curve is not too steep. Publisher is a full-fledged desktop publisher program with an interface similar to Adobe PageMaker, if you’ve been around that long.

Unlike Word, Publisher uses CMYK and Pantone colors, does color separations, has measurements to 1/1000″ and, most germane to our discussion, has Master Pages! This latter feature makes it easy to create a modular document. All pages are completely independent of one another. No section or page breaks to foul up formatting. No pictures anchored to text that move around. The main catch with Publisher is that people don’t know it’s there, so they have to do some learning.

OK, so maybe learning is a roadblock. That’s OK, there is another way that’s still better than PowerPoint for proposals. Word!

Using Word for Modular Documents

Word is useable for modular documents as long as you know about Master Documents. A Master Document is like a super file that links a number of Word documents. Simply put, you create separate Word files for each section of your long document. Then you use a Master Document to link them for printing. The sub-documents can be edited, moved and deleted with ease. Your client gets flexibility in document structure and only has to learn one new thing!

Word has had this feature for a long time, but it got a bad reputation. This was because users would assemble Master Documents and then try to maintain the large assembly as one entity. Almost inevitably, corruption of the Master Document would ensue and users would have to start over. So what’s changed?

Over the years, Word has gotten more stable. The Master Document feature doesn’t get corrupted as easily. But the deepest secret is that you create a Master Document to print the files, then you throw it away! Trying to preserve it by editing it is what creates the problems, so don’t try to preserve it. A Master Document is a temporary device used only at output time. Remember that and you will never have a problem.

We can help your client with Master Documents. We write tutorials that cover every step of creation, assembly, printing and follow-up. We also write macros that create instant Master Documents from a folder full of sequenced Word files. This is much easier than toiling away with manual formatting in PowerPoint. Give us a call to solve your modular document challenges.

Set Up Word for Professional Editing

Word’s default settings hide crucial formatting information. Professional editing of Word docs requires you to change the program’s display parameters.

In effect, paragraph formatting is stored in the paragraph mark and section formatting is stored in the section break after the relevant content. But Microsoft decided users wanted a pretty interface rather than a functional one, so they chose to hide both entities by default. Every hour of every day, thousands of documents are getting mangled because of this design decision.

Likewise, every graphic in a Word document is anchored to a paragraph. Delete the paragraph, poof, graphic gone. Move the text to a new page and the graphic moves with it. But the anchor is invisible! Let’s fix this!

Professional editing not possible - invisible characters hidden

The normal Word view: pretty, but not informative.

Here we can see that the graphic is anchored to the first paragraph and that anchor is locked. The first paragraph is separated from the second by a continuous section break, which means the page formatting could be totally different for the two paragraphs. The word “text” has been bookmarked. We can take care to format this without ruining any of these characters, now that we can see them.

Invisible characters shown for professional editing

Word’s Useful View

How to Set Up Word for Windows for Professional Editing

The first step to professional editing in Word is to display all the hidden formatting marks. Here’s how to do that in each version:

Word 2003

  • Choose Tools>Customize>Options.
  • Uncheck Show full menus after a short delay and
  • Check Always show full menus. Click on Close.
  • Choose Tools>Options>View.
  • In the Show section, check Bookmarks.
  • In Formatting Marks, check All.
  • Under Print and Web Layout options, check Object anchors, Text boundaries, White space between pages and Vertical ruler.

Word 2007

  • Click on the Office button and then on the Word Options button.
  • Select the Display pane.
  • Check Show all formatting marks.
  • By default,Show white space between pages in Print Layout view should already be checked. If not, check it.
  • Now choose the Advanced pane.
  • Under the Show document content section, check Show bookmarks and Show text boundaries.
  • By default, under Display, Show vertical ruler in Print Layout view should already be checked. If not, check it.

Word 2010

  • Click on the File tab and then the Options button.
  • Select the Display pane.
  • Check Show all formatting marks.
  • By default,Show white space between pages in Print Layout view should already be checked. If not, check it.
  • Now choose the Advanced pane.
  • Under the Show document content section, check Show bookmarks and Show text boundaries.
  • By default, under Display, Show vertical ruler in Print Layout view should already be checked. If not, check it.

Word 2013 and 2016

  • Click on the FILE tab and then the Options button.
  • Choose the Display pane.
  • Check Show all formatting marks.
  • By default,Show white space between pages in Print Layout view should already be checked. If not, check it.
  • Now select the Advanced pane.
  • Under the Show document content section, check Show bookmarks.
  • By default, under Display, Show vertical ruler in Print Layout view should already be checked. If not, check it.

How to Set Up Word for Mac for Professional Editing

Word 2004, Word 2008 and Word 2011

  • Choose Word>Preferences>View.
  • Under Show, check Object anchors, Bookmarks and Text boundaries.
  • Under Nonprinting characters, check All.
  • Under Window, Vertical ruler should already be checked. If not, check it.

Word 2016

  • Choose Word>Preferences>View.
  • Under Show in Document, check Object anchors and Bookmarks.
  • Under Show Non-Printing Characters, check All.
  • Under Show Window Elements, Vertical ruler and White space between pages in Print Layout View should already be checked. If not, check them.

Some Useless Options

Unchecking these options can save your time and sanity:

  • When selecting, automatically select entire word, to enable per-character formatting and correcting,
  • Keep track of formatting, so Word will not create a new style for every little formatting change, and
  • Scale content for A4 or 8.5×11″ paper sizes, so all branding and placed graphics will print undistorted.

Just doing these steps will give you a leg up on 99% of the rest of the world in terms of professional formatting in Word. The great majority of formatting screwups are the result of inadvertently deleting the hidden formatting characters. Next post, we’ll get into more details about how these formatting marks actually work.

Microsoft Publisher: Better than Word?

Is Microsoft Publisher better than Word? For design-intensive documents like newsletters, the answer is YES!

We recently had a request to transcribe a full-scale 16-page magazine layout from InDesign to Word. We recommended using Microsoft Publisher. In addition to the usual Word, PowerPoint and Excel, many versions of Office include Microsoft Publisher, a lightweight but capable desktop publishing program.

MS Publisher is comparable to PageMaker, for those of you that have been in the industry long enough. The interface is similar enough to other Office products that the learning curve is fairly low. But the best part is that it’s free and already installed in most offices. Your client doesn’t have to buy or install anything to get good quality design documents.

Users will find useful features like:

  • master pages,
  • Pantone and CMYK color models,
  • the ability to make color seps and print to a real press,
  • measurements to 1/1000 of an inch and
  • many others.

Word doesn’t have any of these.

Microsoft Publisher for Modular Design

But perhaps the greatest advantage lies in a fundamental design choice in Word. Publisher does not link everything to the text stream. Just like InDesign and Quark, pages and graphics exist as independent entities that stay where you put them. In Word, all page breaks, placed graphics, column breaks, etc. are anchored to the text string. When the text is edited, all elements move in relation to it. This isn’t a defect, just the fundamental difference between a word processor and a page layout program.

Designers rarely recommend Publisher simply because they’re unfamiliar with it. It’s a Windows-only product, so they don’t see it in their copy of Mac Office. Microsoft doesn’t advertise it as a feature product. But Publisher is ideal for newsletters, brochures and magazines. Consider it for any document where you need a flexible layout with lots of graphics, photos and articles.

These types of files can be done in Word, but they are always less reliable and more limited. Word is still a better choice for documents that must be editable by anyone at the client’s office. Publisher is not universally installed and does require a little familiarity. For design-intensive Word files, we suggest additional tech support time to help handle the inevitable “I deleted a paragraph and my photo disappeared as well!” questions.

Micirosoft Publisher Case Study

New York University needed a flexible ask brochure to help raise construction funds from donors. They required an electronic document that could have pages added and removed at will (not a strong point of Word!). They also needed to customize the brochure with each donor’s name and information before printing it out on a color copier. Publisher met their needs for handling large linked graphics and color consistency using Pantone specs. Here’s what the cover looked like:

Microsoft Publisher ask brochure cover

Cover of NYU ask brochure in Microsoft Publisher

Do you have a client newsletter project coming up? Give us a call at +1 201 664 6007 to discuss whether Microsoft Publisher might suit your client better.