Automating Adobe InDesign 2019 - AppleScript

XML Multiple Items on Multiple Pages

Wednesday, September 04, 2019

XML from 1 to Many Pages

The key to working with XML for a multiple page project is to start with a document having a Primary Text Frame. Instead of building page elements for XML on the first page of the document, the master page is used. Let's see how this works. We will first walk through the process manually. For the example we will use a document that does not support facing pages. To make things simple, we will import text styles and XML tags from a standing template. Create a document using the following setting:

Document preset settings for our business card example...Document settings for new document

Master Page A

With your document open, select A-Master from InDesign's Pages panel. In the master page you can use Create Guides to begin setting up guides. Drag guides from the left ruler to creater guides at 11p0 and 34p0.

When you click on the area of the page inside the margins, the In Port for the page displays near the upper top-left corner indicating that this is a Primary Text Frame.

Resize the Primary Text Frame to fit within the right hand portion of the first business card, sized to 10p0 high (2p0 space at top of card).

Click in the outport of the frame you just resized and drag out a similar frame in the card to the right. This threads the two frames together. With View > Extras > View Text Frames selected, a line appears when you activate the page to show the thread. Continue in this manner to thread all of the similar text frames. When all frames are threaded, the page should look like the following.

showing page with threaded text frames...Page with threaded text frames

Return to page one of the document. Use command plus shift to click on the first threaded frame on the page to override it from the Master page.

Load Resources

From InDesign's Paragraph Styles menu select Load All Text Styles and select the template having the styles needed.

From the Tags Panel select Load Tags and select the same template.

Dummy Text

The strategy we are using for the project is to have paragraph styles, XML maps, and dummy text using the same string values (Name, Title, ePhone, and eMail).

In the override text frame, add dummy text using the expected string values.

Text frame with dummy text for XML import...Dummy Text added to text frame

Style the dummy text using the paragraph styles of the same name.

If you don't see ending paragraph marks at the end of each line, select Show Hidden Characters from under InDesign's Text menu. Being careful to select the text without the paragraph returns, select their tags from the Tags panel.

Map Tags to Styles

In the Tags panel menu select Map Tags to Styles. In the dialog that opens map the Name, Title, Ephone, and Email tags to paragraph styles of the same name. Open the Structure pane (Command+Option+1). Disclose the triangles to reveal the structure. Notice that the XML element is Story, not Employee as anticipated. Also there is no Employees element.

XML Elements

Select the Story Element and select New Parent Element from the Structure panel menu. Select Employees from the Tags dropdown in the dialog presented. Click OK.

Click on the Story Element and select Employee from the Tags panel. The parent text frame for the dummy text changes color to match the color for the XML tag. Save the document.

Time to Import

From InDesign's File menu, select Import XML. Select the Employees.xml file with Show XML Import Options and Merge Content checked. Click Open.

Import file options in open file dialog...Import file options for XML

From the XML Import Options, check Clone repeating text elements. All other options are not checked. Click OK.

XML options dialog showing clone repeating checked...XML import options

Open the Structure panel (command+Option+1). Our sample XML document has 12 entries for Employee. Drag the Root element to the first text frame of the document (highlighted). It takes a second or two for the document to update. Navigate to the second page of the document. There you should see the remaining 12 business card text frames are also populated.

Finish the document as needed adding the remaining business card elements to the frames on the master page.

That's a lot of steps required to automate 12 business cards, but imagine having 100 or more cards to create.

Now Let's Automate

Should you have a number of clients that order business cards in bulk and are willing to send you the information as an XML file, you may want to create a script to do the hard lifting for you. Our sample script is more than you will want to walk through at this point, so we will just show the major sections here. Most of the handlers are fairly standard and have found their way into a number of blog posts in the past. We will look at most of them in next week's blog post where we can spent a little more effort explaining the code.

The Sample Script (partial listing)

The top of the script defines the variables used and calls handlers from within a try/on error trap:

set dummyText to "Name" & return & "Title" & return & "Ephone" & return & "Email" & return
set presetName to "BizCard_6up"
set gridList to {3, 2, 11, 4, 12, 10} --rows, columns, gutter, gap, width, height}
set marList to {2, 2, 2, 2}
(*Parameters for propList: page width, page height, marginList,  facing pages, master frame, number pages, columnCount*)
set propList to {48, 44, {2, 2, 2, 2}, false, true, 1, 1}
set vList to {11, 23, 25, 34} --position list of vertical guides
set hList to {14, 16, 28, 30, 42} --position list for horizontal guides
   set docRef to docFromPresetMake(presetName, propList)
   set pageRef to masterGuides(docRef, vList, hList)
   set destPage to masterFrames(docRef, pageRef, gridList)
   createDummyText(docRef, destPage, dummyText)
on error errStr
   display alert ("Error: " & errStr)
end try 

docFromPresetMake (not listed) - This handler checks to see if a document preset exists, if not a preset is created. Either way, the preset will have the same properties.

masterGuides (not listed) - creates vertical and horizontal page guides on the master page. Returns a reference to the master page.

masterFrames (not listed) - This handler creates a grid of text frames designed to hold the content from the imported XML. The frames are threaded. To calculate the geometric bounds for the text frames, a handler getBounds is called. It returns a reference to the first page of the document.

loadResources (not listed) - imports the text styles and XML tags from a chosen template. (The template is in the Templates folder for the application.) It also maps the XML tags to paragraph styles having the same name values.

doXML - the workhorse for the script. This handler places and styles the dummy text for the first text frame of the document. It uses a reference to the root XML element to create the two major XML elements Employees and Employee. It associates the Employee element to the first text frame of the document. It then repeats through the dummy text paragraphs and creates XML elements whch are then associated to the paragraph text. The paragraph style and the XML tag are then mapped. The user is then prompted to select the XML file to be placed which is then imported and placed.

(*creates tagged dummy text and associated XML elements. 
Has the user select the XML file which it then imports and places. *)
on doXML(docRef, destPage, dummyText)
	tell application "Adobe InDesign CC 2019"
		set docRef to document 1
		tell docRef
			set destPage to page 1
			set frameRef to text frame 1 of destPage
			set rootElement to XML element 1
			set tagRef to XML tag "Employees"
			set childTagRef to XML tag "Employee"
			tell rootElement
				set masterElement to make XML element with properties {markup tag:tagRef}
			end tell
			tell masterElement
				set parentElement to make XML element with properties {markup tag:childTagRef}
			end tell
		end tell
		tell parentElement to markup using frameRef
		tell frameRef
			set contents to dummyText
			repeat with i from 1 to count of paragraphs
				set paraText to paragraph i
				set paraRef to object reference of paragraph i
				set styleName to text 1 thru -2 of paraText
				set styleRef to paragraph style styleName of docRef
				set tagRef to XML tag styleName of docRef
				set applied paragraph style of paragraph i to styleRef
				tell parentElement
					set elementRef to make XML element with properties {markup tag:tagRef}
				end tell
				set applied paragraph style of paraRef to styleRef
				set textRef to object reference of text from character 1 to character -2 of paraRef
				tell elementRef
					markup using textRef
				end tell
				tell docRef
					make XML import map with properties {mapped style:styleRef, markup tag:tagRef}
				end tell
			end repeat
		end tell
		set fileRef to my getXMLFile()
		my xmlImportPrefs(docRef, true)
		tell rootElement
			import XML from fileRef
			place XML using frameRef with autoflowing
		end tell
		set applied paragraph style of paragraph 1 of frameRef to styleRef
	end tell
end doXML

The final page is shown below. Even the trim tick marks were created using a separate script (TickMarks).

Page one of our final document...Our first page with tick marks etc. added

Onward and Upward

To make the script user friendly, you will want to create a custom dialog for the user to enter in the parameters for the document preset and grid settings. Our demonstration script leaves a lot of work to be completed. Hopefully you will tune in again next week where we will list the missing handlers and work through their code.

Scripts provided are for demonstration and educational purposes. No representation is made as to their accuracy or completeness. Readers are advised to use the code at their own risk.

Trackback Link
Post has no trackbacks.

Recent Posts


time reference to Buttons image metadata grid stacking order Checkbox Control Image Events Chaining Paragraph Styles dropdowns Next Style Styling Text text item delimiters script folder choose file name place point repeat loops Autoflow master spread Multi-state objects Radiobutton Group Create Text Frame import styles next text frame Templates Check for Styles import XML move file random number Character Styles Custom Dialog lists of lists live bounds XML tags rest integer editboxes selection Duplicate text editbox Enabling Groups Previous text frame giving up after tab list do script StandardAdditions file paths Border Panel Library Folder Adding Captions object styles script templates Keyboard Shortcuts table styles entire path dialog Document Presets Document stylesheets date objects Choose from list static alignment Placing images measurement editboxes Combobox sort list Change Text try/on error Write to File draw from center Choose file Image Placement info for measurements tabs Script panel Document from Template lists Clearing Overrides handlers automated workflow Place text auto tag Nested Repeat Loops rest of list active spread file info Editbox convert to text XML structure Script Preferences coordinates import text Text Style Mapping paragraph styles Export to PDF user interaction level find change options fit Apply Object Style intent Repeat With Create Document Excel Find by Attribute primary text frame threaded text frames bookmarks CALS tables Read text file as list GREP map tags to styles start paragraph Adjust Layout XML Find Text radiobutton control page items Script Library text import preferences InDesign template