A journey into the world of working with third party applications can open up many opportunities for exploration. This is where I have found myself lately looking for ways to automate Google Docs for import into Adobe InDesign.

THIRD PARTY APPLICATIONS

The biggest problem with using documents prepared in third party applications is that many of these users have had little training. This often results in documents with a number of problems when it comes to importing into Adobe InDesign. One problem I discovered recently is that images placed in Google Docs exported as HTML may work well for web but are less than optimal for print. If possible, you will need to get the original images from the user and resize and crop as needed for print.

Another problem is that the user may create a book intended for chapters as one long document. To convert the document into chapters you have a little work ahead of you:

  1. Make sure paragraph styles have been used for styling text
  2. Make sure the first paragraph of each chapter starts with a similar paragraph (“Introduction”, “Chapter 1”, etc.) that is styled with a unique style, possibly “Title” or “Subtitle”.
  3. Create a “flag” in the document where each of the images is placed (possibly just before the image’s caption). This flag will need a unique text format to set it apart from the other text. (I used the image name inside of square brackets ([image_01}, etc.).

Once the document is prepared, export the document as Microsoft Word. Then import into InDesign. (See our previous blog).

Once the Microsoft Word document is imported, save the InDesign document and maybe even a copy just for backup.

Next, you will need a template that sets up the documents for the final chapters. Make sure the template has paragraph styles set up to match (or can be mapped) to those used in the Microsoft Word document. The template also needs to have Primary Text Frame set to true.

With this in place, you can use a script to cut the document into individual chapters:

With your InDesign document open (the one with the imported Microsoft Word document) the following script will create the chapter documents in the same folder.

The files created will be named the same as the first paragraph of the chapter, so hopefully the first paragraph will be the name of the chapter (Chapter 1, and so on).

The method used by this script incorporates cut and paste which presents a small problem of its own: autoflow does not work with pasted text. For this reason, you will need to make sure the template used for the individual chapters provides ample pages. You can always remove empty pages once images are added and final edits are completed.

As with scripts that work with text in InDesign, the script works with the document from back to front. This eliminates the problem that is caused when text indexing gets changed due to adding and/or moving text.

 

SCRIPT DEPENDENCIES

The script requires several pieces of information:

  • The name of the paragraph style used to style the first paragraph of each chapter
  • The minimum length of text in a text flow needed to qualify as a story

Because you are running the script, the dependencies are no problem, you can just hard code the values to the script. Should you give the script to another person to run, you will need to provide a custom dialog to allow the user to provide that information.

For the purpose of demonstration, the values for the dependencies are hard-coded at the top of the script.

About Minimum Story Length

When a Microsoft Word document is imported into InDesign, the document can have any number of stories, only one of which is the actual text flow for the document. For this reason, we give the script a minimum character length to filter out the empty stories that may have a few characters for whatever reason. With tests, a minimum length of 100 characters seemed reasonable and worked well for the script.

Script Features

A feature of this script is that it presents the user a list of the chapter document titles found. This is to allow verification prior to actually processing the document. You don’t want to process the entire document only to find that one or two of the chapters were not prepared correctly, or for some other reason were not recognized by the script. Being able to stop the script if the list is missing a chapter or two can save some otherwise unnecessary work.

The script takes care of empty spaces and/or line returns styled as part of the chapter’s first paragraph (clipText handler).

Chapter documents will be saved to the same folder as the InDesign master document.

The path to the template is returned from a choose file command.

AppleScript

(*Values for paraStyleName and minLength should be returned from custom dialog rather than being hardcoded.*)
property templatePath : missing value
set paraStyleName to "Subtitle"
set minLength to 100 --arbitrary length to consider for valid story
set query to "Do you want to continue?"
set storyQuery to "There is more than one story longer then " & minLength & " characters." & return & query
try
	set templatePath to choose file with prompt "Select template for chapters"
	--Get reference to document and path to document: getDocRef handler 
	set {docRef, chapterPath} to getDocRef()
	--get list of chapter headings: getChapterList handler
	set {chapterString, storyRef, foundList} to getChapterList(docRef, paraStyleName, minLength, storyQuery)
	set foundSet to item 1 of foundList
        with timeout of 500 seconds
	   set doContinue to button returned of (display dialog (chapterString & return & query) buttons {"Yes", "No"} default button 1)
	end timeout
        if doContinue is "No" then
		error "Existing script with faulty chapter list"
	end if
	set testList to makeChapters(docRef, storyRef, foundSet, chapterPath, templatePath)
on error errStr
	activate
	display alert "Error " & errStr
end try
--====Handlers=====
(*Returns list of chapter titles, 
a reference to the story, 
and the found set of chapter title references*)
on getChapterList(docRef, paraStyleChoice, minLength, storyQuery)
	set chapterString to ""
	set foundList to {}
	tell application "Adobe InDesign CC 2015"
		set find text preferences to nothing
		set change text preferences to nothing
		set styleRef to paragraph style paraStyleChoice of docRef
		set applied paragraph style of find text preferences to styleRef
		tell docRef
			set storyList to every story where length of it is greater than minLength
			my storyListTest(storyList, storyQuery)
		end tell
		set storyRef to item 1 of storyList
		tell storyRef
			set foundText to find text with reverse order
			if foundText is not {} then
				set end of foundList to foundText
				repeat with j from 1 to length of foundText
					set chapterString to chapterString & item j of foundText
				end repeat
			end if
		end tell
		set find text preferences to nothing
		set change text preferences to nothing
	end tell
	return {chapterString, storyRef, foundList}
end getChapterList
(*Returns reference to document and file path if active document; otherwise throws error*)
on getDocRef()
  tell application "Adobe InDesign CC 2015"
    if modal state is true then
      error "modal window is open"
    end if
    if exists active document then
      set docRef to active document
    else
      error "Requires active document"
    end if
    tell docRef
      set docPath to file path as string
    end tell
  end tell
  return {docRef, docPath}
end getDocRef
(*In event more than one story is found having minimum required length*)
on storyListTest(storyList, storyQuery)
	if length of storyList > 1 then
		set userResponse to display dialog storyQuery buttons {"No", "Yes"} default button "No"
		if button returned of userResponse is "No" then
			error "User Cancelled"
		end if
	end if
end storyListTest
on makeChapters (docRef, storyRef, foundSet, chapterPath, templatePath)
--to come
end makeChapters

TEST SCRIPT

Of course you have been testing the script as you go along. At this point, however, it is critical that you make sure the script works as anticipated. Change the value for the minLength variable to 1 and test. Once you have determined that the script is working as intended, fill in the code for the makeChapters handler. Notice that this handler calls another handler trimText() to remove unwanted characters at the beginning and end of the chapter names.

on makeChapters(docRef, storyRef, foundSet, chapterPath, templatePath)
	set testList to {}
	tell application "Adobe InDesign CC 2015"
		set endNumber to -1
		repeat with i from 1 to length of foundSet
			set textRef to item i of foundSet
			set thisText to contents of textRef
			set chapterName to my trimText(thisText)
			set end of testList to chapterName
			set savePath to chapterPath & (chapterName & ".indd")
			set insPt to index of character 1 of textRef
			activate
			set theDup to select (text from character insPt to character endNumber of storyRef)
			copy
			set newDoc to open templatePath
			set pastePoint to insertion point 1 of text frame 1 of page 1 of newDoc
			select pastePoint
			paste
			save newDoc to savePath without force save
			close newDoc saving in savePath
			set endNumber to insPt - 1
		end repeat
	end tell
	return testList
end makeChapters
(*Removes characters listed in badChar list at beginning and end of string passed*)
on trimText(thisString)
	set badChar to {" ", "\r", "\t", "\n"} --space, return, tab, forced return 
	repeat while character 1 of thisString is in badChar
		set thisString to text 2 thru -1 of thisString
	end repeat
	repeat while character -1 of thisString is in badChar
		set thisString to text 1 thru -2 of thisString
	end repeat
	return thisString
end trimText 

This pretty much is the meat and potatoes for the script. Depending on the capability of your computer ad the length of the original story, the script will take a few seconds to run. You may also get a warning about missing fonts. Your experience may dictate adding some refinements to the script.

There are other methods that could be used instead of cut and paste, but we will leave this up to a later discussion.

Now it is up to you to decide how to get the path to the template and the name of the paragraph style used to mark the beginning of each chapter (maybe a choose file and choose from list, or a custom dialog). Add this with your own refinements to the script.

One refinement you might want to add is to remove any “bad characters” at the beginning of the chapter text after pasting. If not, you may end up having to manually do so after creating the chapters. With my actual runs, I may end up removing a paragraph return at the beginning of the chapter and adding paragraph returns in the text where images are to be placed.