Automating Adobe InDesign 2019 - AppleScript


Splitting Data Merge Document

Tuesday, October 08, 2019

Splitting Merged Data PDFs

While on the subject of using Excel as a data source for InDesign, there is an interesting automation that involves Acrobat. This has to do with Acrobat's unique feature that allows a single PDF to be split into individual files based on document parameters. One, parameter in particular is to use bookmarks. This is where InDesign comes in: creating bookmarks to identify split locations.

Think of a document where you may have many similar sections created using Data Merge and you wish to create a separate PDF for each. Assigning a bookmark to the first data element for each section (data set) can be used by Acrobat to split the PDF.

Creating Bookmarks in InDesign

A popular method for creating Bookmarks in InDesign manually is to use Table of Contents. The key to the process is to style the placeholder for each data element using a paragraph style. In the data merge process each placeholder is mapped to an Excel data item. It is the paragraph style that identifies the page element to be bookmarked. For our example, the Name style was used.

Table of Contents

One advantage of using Table of Contents is that the resulting list of bookmarks can be veriied prior to exporting the document to PDF. Identify the paragraph style for the top level data item and make sure Create PDF Bookmarks is checked when the table of contents is created.

Creating bookmarks using Table of Contents...Creating bookmarks in Table of Contents

The resulting Table of Contents can be placed on the pasteboard.

Table of Contents placed on pasteboard...Verify Table of Contents for bookmarks

The merged document with its Table of Contents thus created is exported to PDF, again making sure that Bookmarks is checked.

Export to PDF settings...Settings for Export to PDF

From there, the Split function in Acrobat is used to split the document into separate files based on the bookmarks.

Steps in Acrobat to split PDF using bookmarks...Steps to Split PDF

Scripted Solutions

For those who write scripts to automate Adobe InDesign, there are other options. The desired solution may use Acrobat to split the document with a script to create the bookmarks for the merged data document. Let's explore this option.

Bookmark Script

The user will need to identify the paragraph style for bookmarking. With this the script can use Find Text to identify the top level data item. First, verify a document is open in InDesign. The script will use a handler, getDocRef, to identify the document and make sure there are no dialogs open.

set docRef to getDocRef
(*Returns reference to document; errors if no document open or modal state is true*)
on getDocRef()
   tell application "Adobe InDesign CC 2019"
      if modal state then
	  error "Please close dialogs before running script"
      end if
      if (count of documents) = 0 then
	  error "Requires an open document."
      end if
      set docRef to document 1
   end tell
   return docRef
end getDocRef

Next the script will get a list of paragraph styles in the document and have the user select the one to use for the bookmarks. This will require two handlers: getParagraphStyle that calls getChoice.

set promptStr to "Select paragraph style for bookmarks"
set docRef to getDocRef()
set styleRef to getParagraphStyle (docRef, promptStr)
(*Gets list of paragraph styles and calls getChoice to have user choose from the list*)
on getParagraphStyle(docRef, promptStr)
   tell application "Adobe InDesign CC 2019"
      tell docRef
	 set styleNames to name of paragraph styles
	 set styleName to my getChoice(styleNames, promptStr)
	 set styleRef to paragraph style styleName
      end tell
      return styleRef
   end tell
end getParagraphStyle

(*Returns user choice from list; errors if no choice made*)
on getChoice(parastyles, promptStr)
   set styleChoice to choose from list parastyles with prompt promptStr
   if styleChoice is false then
      error "Requires a selection"
   end if
   set styleName to item 1 of styleChoice
   return styleName
end getChoice 

The real workhorse for the script is a handler we have named createBookmarks. It requires the reference to the document and the reference to the paragraph style selected.

set testList to createBookmarks(docRef, styleRef)
(*Creates bookmarks by setting hyperlink page destinations using find to get list of paragraphs for bookmarks*)
on createBookmarks(docRef, styleRef)
   set testList to {}
   tell application "Adobe InDesign CC 2019"
      set find text preferences to nothing
      set change text preferences to nothing
      set applied paragraph style of find text preferences to styleRef
      tell docRef
	 delete bookmarks
	 set foundSet to find text
	 if foundSet is {} then
	     error "No text items found"
	 end if
	 repeat with eachItem in foundSet --length of itemList
	    set frameRef to item 1 of parent text frames of eachItem
	    set pageRef to parent page of frameRef
	    set destContent to contents of contents of eachItem
	    if not (exists hyperlink page destination destContent) then
		set markDest to make hyperlink page destination with properties {destination page:pageRef, name:destContent}
	    else
		set markDest to hyperlink page destination destContent
	    end if
	    if not (exists bookmark destContent) then
		make bookmark with properties {name:destContent, destination:markDest}
	    end if
	    set end of testList to destContent
	 end repeat
      end tell
      set find text preferences to nothing
      set change text preferences to nothing
   end tell
   return testList
end createBookmarks

We have the handler return a list of the paragraph contents just for verification. You could just alert the user with a count of the list items. With this, the top of the script can read as follows:

set promptStr to "Select paragraph style for bookmarks"
try
   set docRef to getDocRef()
   set styleRef to getParagraphStyle(docRef, promptStr)
   set testList to createBookmarks(docRef, styleRef)
   activate
      display alert ("Bookmarks created: " & length of testList) giving up after 5
on error errStr
   activate
   display alert "Error: " & errStr
end try

You could have the script go on to export the file to PDF. To make the above script complete you may also want to add a handler to set the Find Text options just to make sure these are set as needed for your document.

Script to Create Individual PDFs

Using bookmarks may be the way to go especially if each data merge data set requires more than one page. However, if you do have a simple document where each page contains the entire data merge set you could just Export to PDF and use Create Separate PDF Files. Should you want the files to be named the same as the contents for the top data item (as was done manually above), a script similar to the following can be used. Our sample script requires that the text frame holding the data items be named "data".

set promptstr to "Select paragraph style for data file name reference"
set testList to {}
tell application "Adobe InDesign CC 2019"
   set docRef to document 1
   set nameList to name of paragraph styles of docRef
   set styleChoice to my getChoice(nameList, promptstr)
   tell docRef
      set docPath to file path as string
      set styleRef to paragraph style styleChoice
   end tell
   repeat with i from 1 to number of pages of docRef
      set pageRef to page i of docRef
      set pageName to name of pageRef
      set frameRef to text frame "data" of pageRef
      set paraList to (paragraphs of frameRef where applied paragraph style is styleRef)
      set textContent to (text 1 thru -2 of item 1 of paraList)
      set pathStr to docPath & textContent & ".pdf"
      set fileName to textContent & ".pdf"
      set acrobat compatibility of PDF export preferences to acrobat 5
      set page range of PDF export preferences to pageName
      set include structure of PDF export preferences to false
      set pdf display title of PDF export preferences to display file name
	 export docRef format PDF type to pathStr
   end repeat
end tell

(*Returns user choice from list; errors if no choice made*)
on getChoice(parastyles, promptstr)
   set styleChoice to choose from list parastyles with prompt promptstr
   if styleChoice is false then
	error "Requires a selection"
   end if
   set styleName to item 1 of styleChoice
   return styleName
end getChoice

Hint: For naming the resulting files, you might consider using a non-printing data item in the merged document to identify the name of the PDfs created. Just place this data item on the pasteboard with part of its text frame touching the page.

Onward and Upward

Make one of the scripts above your own by adding Find Text options and/or other functionality as needed. Plan your data merge documents to include file naming (see Hint above).

Disclaimer:
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
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18424&PostID=1529756&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags

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

Archive