In our previous blog post we introduced the idea of using an XML file to define the elements for an InDesign document. If you followed the steps outlined in the post you should have ended up with a script that will do the following:

  • create a document from a template chosen by the user
  • save the document
  • import XML from an XML file chosen by the user

If you did not create the script satisfactorily, we have provided the script and resources for you on the AppleScript page of this web site.

The script requires an InDesign template (.indt) saved in InDesign’s Templates folder. Make sure that InDesign is running in the background before launching the script. When prompted, designate a folder and filename for the document in a handy location. Next you will be prompted to choose an XML file for import.

When the script finishes, save the document created. Open InDesign’s structure pane (View > Structure) if not open. The keyboard shortcut for this is Command+Option+1. InDesign’s Structure panel should look similar to the following:

Structure panel

If you do not see XML elements after the “page” elements as in the above (child elements), disclose the twisty triangle to the left of “page” elements.

If you do not see text snippets to the right of each “text” element, open the contextual menu for the Structure panel. (The icon at the top, far right of the Structure menu opens the contextual menu.)

In the contextual menu for the Structure panel you will see an entry for Show Text Snippets. Click to enable text snippets. (This changes to Hide Text Snippets when snippets are enabled.)

If you are using your own XML file, verify that the XML tags for the text elements match the names for paragraph styles in your document. If not, you will need to map tags to paragraph styles. (See About XML Tags below.)


To place the XML, InDesign provides a number of methods. We will start with Place XML. This method uses an XML element to place the XML using one of the following InDesign objects: story, page item, graphic, movie, or sound. Parameters can include an x-y coordinate (place point) and a true or false to indicate if autoflow will be used.

With our document created and the XML imported using the script, the following will place the XML into the text frames for the document:


   tell application "Adobe InDesign CC 2015"
        set docRef to document 1
	tell docRef
		set rootElement to XML element 1
                set storyRef to story -1
	end tell
	tell rootElement to place XML using storyRef with autoflowing
   end tell

This script assumes that the template has been set up correctly (see Setting Up the Template below).

When run with the active document being one created from last week’s blog post script, the text is placed into the document.

If you try this, you will see there is a problem in that all of the text flows into the text frame on the first page, spilling over to the next page.

This is not exactly what was wanted. We need to break the story flow up between text frames on subsequent pages.

Notice in the XML structure that the first XML element for each page element is tagged “text”. To break the text at the first paragraph for each page, we can take advantage of the Keep Options property of a paragraph style. The paragraph style named “text” in our sample template is set to break at the next page (Keep Options: Start Paragraph on Next Page).

If we could associate our paragraph styles with the XML tags, that would solve the problem. And, that is just what map XML tags to styles is all about. With paragraph styles defined and named the same as the imported XML tags, all we need to do is map the XML tags to our paragraph styles by name. If the names of your XML tags do not match the names of your paragraph styles, you will need to map the tags to styles manually (see section About XML Tags below).

You can set up automated tagging as a handler for your script. The following example assumes that paragraph styles have been named to conform to names for the imported XML tags.

   (*Uses XML tag to style to map styles*)
   on mapTags(docRef)
      tell application "Adobe InDesign CC 2015"
	tell docRef
	   set tagList to XML tags
	   repeat with i from 1 to length of tagList
	      set tagName to name of item i of tagList
	      if (exists paragraph style tagName) then
		    set styleRef to paragraph style tagName
		    make XML import map with properties {mapped style:styleRef, markup tag:item i of tagList}
	      end if
	   end repeat
	   map XML tags to styles
	end tell
      end tell
   end mapTags

The call for the handler can be placed after the line that ends the tell block for the document (docRef)

   --after end tell
   my mapTags (docRef) 


Revert the InDesign document to its last saved version (File > Revert). Add the mapTags handler with its call to your PlaceXML script above. Run the script with the document active.

When you run the script, the text frames may change color, pages will be added, and the text will be styled. The text frame on page 2 should look like the following:

Styled XML Text

If your text frames do not change color, choose Structure > Show Tagged Frames from InDesign’s View menu. Because the script associated text in the text frames with an XML tag, the text frames are colored using the tags’ default color. InDesign sets up the colors for XML elements by default unless you specify in your script or otherwise.


When you import an XML file into an InDesign document, the tags assigned in the file display in the structure pane. These tags will also be listed in the document’s Tags panel (Window > Utilities > Tags).

InDesign Tags panel

Page items associated with an XML element, assume the color and properties assigned to the XML element’s markup tag. The mapTags handler in the example script above assigns the tags to matching paragraph styles.

If you open Map Tags to Styles from InDesign’s Tags panel’s contextual menu, you will see the result of running the handler.

Map Tags to Styles Dialog

Notice that there is a Map by Name button in the lower corner of this panel. If you were to need to map XML tags to text styles manually, this may be the option to use if names match.



The code that does the magic in the script is

	tell rootElement to place XML using storyRef with autoflowing

For a document populated with XML to take advantage of the autoflowing option, it must be set up using Primary Text Frame. Without this, autoflow just will not work. Here’s the procedure:

  1. Create a single page document with Primary Text Frame checked (this was referred to as the master text frame in earlier versions of InDesign)
  1. New Document with Primary Text Frame checked
  2. In the Pages panel, switch to the document’s master spread “A-Master”
  3. Grab the handles for the master text frame and drag to describe the geometry for the text frame that will receive the XML text.
  4. Switch back to the first page of your document.
  5. Set up paragraph and character styles as needed.
  6. Save your document as a template in a folder named “Templates” in InDesign’s application folder.


Try creating your own template for an XML autoflow document. Use our XML file or create one of your own. If you use our XML file, be sure to name the paragraph styles in your template to correspond. (Remember, XML is case specific, so don’t capitalize unless needed.) Add the code from today’s post to that from last week to create a script that goes from document creation to styled text.