Automating Adobe InDesign 2019 - AppleScript


XML: Another Approach

Tuesday, September 24, 2019

Another Approach to XML

I often tell students that with power comes difficulty. True, the more power a program offers, the more difficult it becomes--the more there is to learn. So it is with using XML in InDesign. The previous blogs open the door; but to really cover the subject would require a fairly sizable book. Before we leave the subject of XML, however, there's one more approach that needs some exposure: using auto tag. Our sample script creates a document with paragraph styles and XML tags which are then mapped. Dummy text is placed in a default text frame and styled. Because paragraph styles are mapped to XML tags, the document is ready for importing an XML text file that uses the same XML tagging.

To condense the script as much as possible, a default document is created and repeat loops are used to create styles and tags. The script assumes that the default document is a single page with the same margins for left and right. Also, no code has been added to check for user error. We will leave this up to the reader. You might want to spend some time figuring out how style pointers (in stylePtr list) are used to define the style to apply to each paragraph of dummy text.

Make and Map Document for XML

(*Uses repeat structures to create paragraph and XML tags, style text and then apply style to tag mapping.*)
--lists in list define style name, size, space before, and first line indent
set styleList to {{"Heading1", 24, 0, 0}, {"Heading2", 14, 12, 0}, {"Para1", 12, 9, 0}, {"Body", 12, 0, 24}}
set styleRef to {}
set myText to "Headline 1" & return & "Paragraph 1 of my story" & return & ¬
"Paragraph 2 of my story" & return & "This is a subhead" & return & "This is text following subhead" ¬
& return & "This is the second paragraph following the subhead"
set stylePtr to {1, 2, 4, 3, 4, 4}
tell application "Adobe InDesign CC 2019"
   set measurement unit of script preferences to points
   --set x to applied paragraph style of text defaults of docRef
   set fontRef to font "Myriad Pro 	 Regular"
   set basicStyle to paragraph style 2
   set applied font of basicStyle to fontRef
   --creates a default document
   set docRef to make document
   tell docRef
	set rootElement to XML element 1
	set basicStyle to make paragraph style with properties {name:"Basic", font:fontRef, size:10, leading:12}
	repeat with i from 1 to length of styleList
	   copy item i of styleList to {myName, mySize, spBefore, myIndent}
	   set end of styleRef to make paragraph style with properties ¬
{based on:basicStyle, name:myName, point size:mySize, space before:spBefore, first line indent:myIndent}
	end repeat
   end tell
   set myPage to page 1 of docRef
   --create text frame and add styled dummy text
   tell myPage
	tell margin preferences to copy {top, left, bottom, right} to marginPrefs
	set myBounds to my getmyBounds(bounds of myPage, marginPrefs)
	set myFrame to make text frame with properties {geometric bounds:myBounds}
	set myStory to parent story of myFrame
	set contents of myStory to myText
	set theCount to count of paragraphs of myStory
	repeat with i from 1 to theCount
	    set ptr to item i of stylePtr
	    set theStyle to item ptr of styleRef
	    set applied paragraph style of paragraph i of myStory to theStyle
	end repeat
   end tell
   --create tags to match paragraph styles
   tell docRef
	repeat with eachItem in styleRef
	   set theName to name of eachItem
	   set tagRef to make XML tag with properties {name:theName}
	   make XML import map with properties {mapped style:eachItem, markup tag:tagRef}
   end repeat
   end tell
   --apply tag to style mapping to theStory
   tell myStory to auto tag
end tell

(*Calculates bounds for live page text frame based on page bounds and margins*)
on getmyBounds(pageBounds, marginPrefs)
   copy pageBounds to {y0, x0, y1, x1}
   copy marginPrefs to {t, l, b, r}
   set myBounds to {y0 + t, x0 + l, y1 - b, x1 - r}
   return myBounds
end getmyBounds

With the document created when running the previous script (Make and Map Document for XML), you can now import an XML file and replace the current story (dummy text) with XML. Of course, the tags in the XML file will need to match the tags in the document.

Import and Place XML

set fileRef to choose file with prompt "Select XML file for import"

tell application "Adobe InDesign CC 2019"
   set docRef to document 1
   my xmlImportPrefs(docRef, false)
   tell docRef
	set frameRef to text frame 1 of page 1
	set rootItem to XML element 1
   end tell
   tell rootItem 
        import XML from fileRef
	place XML using frameRef with autoflowing
   end tell
end tell

(*Sets up xml import preferences for merge import.*)
on xmlImportPrefs(docRef, doRepeat)
   tell application "Adobe InDesign CC 2019"
	tell XML import preferences
	   set create link to XML to false
	   set allow transform to false
	   set import style to merge import
	   set repeat text elements to doRepeat
	   set import to selected to false
	   set ignore whitespace to false
	   set ignore unmatched incoming to false
	   set import CALS tables to false
	   set import text into tables to false
	   set remove unmatched existing to false
	end tell
   end tell
end xmlImportPrefs

Place XML

The Place XML command can also be written using a page item with the XML element.

tell docRef
   set frameRef to text frame 1 of page 1
   set rootItem to XML element 1
   import XML from fileRef
   tell frameRef to place XML using rootItem with autoflowing
end tell

In addition to Place XML there are a number of place into methods. These are worth exploring.

Tag and Style Naming

Notice that we are careful to use XML naming for names of styles and tags (no spaces or special characters). If you don't have that kind of control, you will need to remove spaces or special characters from paragraph style names before attempting to create a tag with the same style.

XML Structure

The XML structure for the above example is slightly different than used previously:

<root>
<story>
<heading1>Heading Here</heading1>
<heading2>Subhead Here</heading2>
<body>Body Text</body>
<para1>First line of section</para1>
<body>Body Text</body>
<body>Body Text</body>
</story>
</root>

Upward and Onward

Think of all the ways that the Make and Map script could be modified and used to automate the creation of templates for documents such as a bulletin or newsletter where the document structure remains the same.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18424&PostID=1529691&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

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

Archive