Automating Adobe InDesign CC 2015

Plain Text Files for Automation

Tuesday, November 15, 2016

Read Text File as List

There are a number of ways information in a plain text file can be used as part of an automation workflow. The popular FindChangeByList script that is part of the Sample scripts provided by Adobe is one example. This script reads the support text file in a designated folder) to get the information needed for a find operations in InDesign.

FindChangeByList (partial code)

The code in this example uses the return character to define each find operation (delimiter). The code that handles this functionality returns a list of the find operations.

--The variable myFileRef is a string reference to the file's path.
set myFileRef to open for access (myFile as alias)
set myEOF to get eof myFileRef
if myEOF > 0 then
set myText to read myFileRef using delimiter {return}
close access myFileRef
end if

Notice in the above that the code opens the file for access and then closes the file reference after reading. This is often the accepted procedure, however, opening and closing the file is not necessary if the script is just going to read the file.

The above example reads the file using a specified character (return) as a delimiter. A problem could be raised here if a line return is not used to delimit the file's content.

List Paragraphs from File

Below is an alternate method for reading in a text file as a list. Here the handler that reads the text file uses paragraph references to return the list of entries.

(*Reads text file and returns list of paragraphs*)
on readTextFile(fileRef)
set fileSize to (get eof fileRef)
if fileSize > 0 then
set textList to paragraphs of (read fileRef)
end if
return textList
end readTextFile

This could be used for a number of automation workflows that require a list of files to be processed. The text file for this would provide the names of the files in processing order.

One example is a script for creating a book (.indb file). This requires the InDesign documents (.indd) for the book to be in the same folder as the text file that lists the files.

Yes, you could just have your script get the list of files from the folder, but chances are that alphabetical order may not match the order of the files needed for the book. Of course you could always rearrange the order of the files in the book inside InDesign, but that would be one more step required of the user.

Make Book

(*Gets list of file names from the getFileList handler.
Uses folder name to create a book  (.indb file) and then gets list
of file names from text file to add the files to the book.*)
set promptStr to "Choose folder for processing files"
set textFileName to "BookChapters.txt"
  tell application "Adobe InDesign CC 2015"
    --make sure no documents or books are open
    set bookCount to count of books
    set docCount to count of documents
    if bookCount + docCount > 0 then
      error "Please close documents and books before running this script"
    end if
    --have user choose the folder to process
    set {folderStr, folderName} to my getFolderWithName(promptStr)
    --read text file in the folder to get list of files to process
    set fileList to my getFileList(textFileName, folderStr)
    --make the book using list of files in the folder chosen
    set bookRef to my makeBook(folderStr, folderName, fileList)
  end tell
on error errStr
  activate (display alert "Error: " & errStr)
end try
display alert "Script is done " giving up after 2
--=========== HANDLERS =============
-returns folder path of folder chosen as well as the folder's name
on getFolderWithName(promptStr)
  set folderRef to choose folder with prompt promptStr without multiple selections allowed
  set folderStr to folderRef as string
  set oldDelim to AppleScript's text item delimiters
  set AppleScript's text item delimiters to ":"
  set pathList to text items of folderStr
  set AppleScript's text item delimiters to oldDelim
  set folderName to item -2 of pathList
  return {folderStr, folderName}
end getFolderWithName
--returns list of files from reading text file designated
on getFileList(textFileName, folderStr)
  set fileRef to folderStr & textFileName
  set fileAlias to fileRef as alias
  set fileSize to (get eof fileAlias)
  if fileSize > 0 then
    set chapterList to paragraphs of (read fileAlias)
  end if
  return chapterList
end getFileList
(*creates book from files found in folder designated using list of files.
Uses folderName for name of the book file*)
on makeBook(folderStr, folderName, fileList)
  set bookFullName to (folderStr & folderName & ".indb")
  tell application "Adobe InDesign CC 2015"
    set bookRef to make book with properties {full name:bookFullName}
    with timeout of 300 seconds
      tell bookRef
        repeat with i from 1 to length of fileList
          set chapterName to item i of fileList & ".indd"
            make book content with data {full name:(folderStr & chapterName)}
          end try
        end repeat
      end tell
    end timeout
  end tell
  return bookRef
end makeBook

Try It

  • Create a plain text file that lists the files to process. List each file to process as a separate paragraph (return delimited).
  • Name the file "BookChapters.txt". Make sure the content of the file looks similar to the following.
    Plain text file for list
  • Place the file inside a folder named the same as your sample book.
  • Add some InDesign files to the folder named the same as listed in the sample file above.
  • Copy the script into a new AppleScript Script Editor document.
  • Compile the script using the Compile button at the top of the Script Editor window.
  • Run the script using the Run button the left of the Compile button
  • You will be asked to select the folder that contains your InDesign files and the plain text file.
  • If all goes well, the script will begin processing.
  • When completed you will be presented a dialog indicating that the script has run.

Onward and Upward

To make the script versatile, you could add a custom dialog box to have the user enter the name of the text file to be read. You could even modify the script to start a new book by having the script create the InDesign files in the folder using a template designated by the user. Now that you are confident in creating a script that reads a plain text file for a list of files to process, perhaps you can think of more ways to take advantage of functionality provided by this script.


Example scripts are designed to be used as sample code to give users ideas for creating scripts of their own. No representation is made as to the scripts' completeness, and user is advised to use at their own risk.

Trackback Link
Post has no trackbacks.

Recent Posts


Fixed Format ePub AppleScript Templates lists script templates Library Folder GPS Read text file as list Editbox coordinates Repeat With Custom Dialog records Create Document AppleScript selection AppleScript dialogs dropdowns pasteboard transform Document Presets Find Change Create Text Frame Character Styles Radiobutton Group Interactive Publications text editbox metacharacters fit images Export to PDF Templates reframe text concatenation Metadata text item delimiters if/then cut and paste Publish Online Border Panel Checkbox Control functions measurements XML structure PDF export preferences active spread tutorial do script doScript transform again Video user interaction level Find Text Preferences resolve Change Text ePub for iPad enable redraw save template Custom CSS Style Sheets Index of blogs and scripts for year 2016 Choose file file paths directory say PDF security Excel Tables automating InDesign InDesign dialogs with timeout Adding Captions import XML handlers CSS InDesign CC object styles BrdgeTalk ePub for Kindle automation try/on error Placing images repeat loops System Events stylesheets External Object Scripts InDesign template Text Cleanup Hyperlinks Find by Font Combobox script folder Sound PDF Export HTML scripting table styles Document stylesheets XML tags import HTML entire path Bullets GPS metadata Image Placement Document from Template Nested Styles HTML Export image metadata watermark Find by Attribute Apple apps import text Caption Variable Choose from list automated workflow arrays folder new document integer editboxes Enabling Groups massage text find change options transformation matrix Media Custom Buttons page items Numbering styles path Embed Document Script Preferences parent Autoflow XML Automating animations Glossary Adobe InDesign CC paragraph styles text placement parent page Mail trimText Table cells Styling Text Find by Color resize measurement editboxes placeable Fixed Layout ePub wild cards CALS tables