Automating Adobe InDesign Libraries

Using templates and style sheets are two ways styling and information can be repurposed to automate document creation in Adobe InDesign. A third resource that can enter into this time-saving equation is libraries. Libraries can be used to store often-used page elements such as graphics, logos, and layout geometry. In this blog we will explore how access to libraries can be automated using a script. The scripts will be written in AppleScript, but ExtendScript users are invited to follow along.

Keep in mind, when adding a page item to a library, InDesign saves all page, text, and image attributes while maintaining the following relationships:

  • Grouping is maintained
  • Formatting for text is retained
  • If named the same as those in the destination document, styles in a library asset are converted to the existing styles. Otherwise, they are added.
  • Original layers are preserved if the property paste remembers layers for the clipboard is set to true.
    tell application "Adobe InDesign CC 2014"
        tell clipboard preferences to set paste remembers layers to true
    end tell

Working With Libraries

For this demonstration, you will want to have a document open with a company logo or masthead selected. Remember that selection returns a list of the items selected. If only one item is selected, the result is a single item within a list.

    tell application "Adobe InDesign CC 2014"
        set selList to selection 
    end tell

To add the selected item to a library, you first need to have an open library. A list of the libraries that are currently available from the library panel is returned using the following:

    tell application "Adobe InDesign CC 2014"
	set libList to name of libraries
    end tell 

To discover the name of the currently open library in the panel, you can use the following:

    tell application "Adobe InDesign CC 2014"
	set libList to name of libraries
	if length of libList > 1 then
            set openLibrary to name of every library panel where visible is true
	else if length of libList = 1 then
            set openLibrary to name of library panel 1
        else
	    activate
	    display alert "No libraries are open"
	end if
    end tell

A library is just a file that is stored some place on your computer. When you create a library manually, you select the folder where you want the library file to reside. In previous versions of InDesign, a library folder existed by default. Currently, InDesign leaves the folder creation up to you. The Button library and the Swatch library folders are in the Presets folder for the application. This may be where you will also want to store your library files. If this is the location you want, you can get a list of the library files contained in that folder using the following:

    set libList to missing value
     tell application "Adobe InDesign CC 2014"
	set appPath to file path as string
	set libPath to appPath & "Presets:Libraries:"
    end tell
    tell application "Finder"
	set libList to name of every file of folder libPath
    end tell

Once you have a list of library file names, your script can open the library. Add the following to the code above:

    set libChoice to choose from list libList with prompt "Select library to open"
    if class of libChoice is list then
	set libToOpen to item 1 of libChoice
	tell application "Adobe InDesign CC 2014"
            set theLib to open (libPath & libToOpen)
	end tell
    end if

With the library file open, you can add a selected page item (or items) to the library.

    if selection is not {} then
        set assetRef to selection
        tell libRef
            set assetRef to store using assetRef with properties {name:"TestItem", description:"Just a test", asset type:geometry type}
        end tell
    end if

What If…

But wait, if the library is already in the Library Panel, you might want to have the user select the library to use from the list of items in the panel. If the library is not available in the library panel, you could have the user select from the list of library files in your designated folder. Lastly, you may not want the script to run at all if nothing is selected. Suddenly, the script starts to require some serious status checking:

  • If libraries are already available in the Library Panel, user can select from list of available libraries.
  • If libraries are not available, user will need to open a library from list of library files
  • If a selection exists, determine if user wants to add it as an asset to library. If so, fields for entering name of asset, description and asset type will need to be provided.

Modular Script

With all of the ifs and options needing to be added to the script, it is time to break the code into modules (handlers in AppleScript, functions in ExtendScript). By breaking the code into modules you can write and test each module separately.

Check Libraries

For starters you will need a handler that gets a list of all of the libraries in the library panel plus all of the library files that are not in the panel. You can put these lists into global variables since the script will not be modifying the lists.

    global openLibraries, closedLibraries
    (*call to the handler passes name of library folder*)
    set libPath to checkLibraries ("Libraries")
    (*the handler*)
    on checkLibraries(libraryFolderName)
	set closedLibraries to {} --default value for variable
	tell application "Adobe InDesign CC 2014"
		--establish path to library folder
                set appPath to file path as string
		set libPath to appPath & "Presets:" & libraryFolderName
		--get list of libraries available in Library Panel
                set openLibraries to name of libraries
	end tell
	tell application "Finder"
		set libList to name of every file of folder libPath
	end tell
        --remove names of files from list if already available in Library Panel
	repeat with i from 1 to length of libList
		set checkName to item i of libList
                --remove extension from name of file
		set dotOffset to offset of "." in (checkName)
		if dotOffset > 0 then
			set checkName to (text 1 thru (dotOffset - 1)) of checkName
		end if
		if checkName is not in openLibraries then
			set end of closedLibraries to checkName
		end if
	end repeat
	return libPath
    end checkLibraries

Custom Dialog

To present the library options to the user, a custom dialog using enabling groups can be the answer.

Enabling groups allow the user to choose from one or more sets of input fields. The code for creating an enabling group can be written similar to the following:

    (*as part of code that creates a custom dialog*)
    set enable1 to make enabling group with properties {static label:"New Library:", checkedState:false}
    tell enable1
        tell (make dialog column)
            tell (make dialog row)
                make static text with properties {static label:"File Name:  "}
                set nameField to make text editbox with properties {min width:240}
            end tell
	end tell
    end tell 

For the script, the first enabling group option might be for creating a new library. Other enabling groups would only be created as dictated by the result of the checkLibraries handler.

Dialog when no library files exist

If libraries are available (openLibraries list), an enabling group would provide a dropdown showing the list.

    (*as part of code that creates a custom dialog*)
    if length of openLibraries > 0 then
        make dialog row --adds space above enabling group
        set enable2 to make enabling group with properties {static label:"Existing Library", checkedState:false}
        tell enable2
            tell (make dialog column)
                set selectLibrary to make dropdown with properties {string list:openLibraries, selected index:0, min width:316}
            end tell
        end tell
    end if 

Dialog when all library files have been made available in Library Panel

The third enabling group would only be created if there are library files which have not been made available in the Library Panel.

    if length of closedLibraries > 0 then
        (*code is siimilar to that above*)
    end if

Dialog with three enabling groups allow user to choose from all library options

A fourth enabling group could provide for asset information should there be a selection in an active document.

As you can see, the code for the custom dialog for this script will be quite long and more than is within the scope of this blog. The complete code will be provided as a download when the discussion of libraries and assets is completed.

Until then, you may want to try your hand at writing your own custom dialog. If you downloaded the DocFromTemplate_Dialog script (from our AppleScript page), you can use the customDialog handler as a guide.