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


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