STYLING TEXT FOR GLOSSARY

Our Feature Script for the month of July involves creating page destination hyperlinks for terms on Glossary pages. Part of the process for setting up the Glossary pages however requires a little work. There needs to be some way to identify the glossary terms from their descriptions. For the most part, the glossary term is the first word of each paragraph in the Glossary text flow. But in some instances the term can be comprised of two or more words. To get a list of the terms for the purpose of creating text hyperlinks automatically, a script can be used if the terms are assigned a unique character style. Of course, you could always manually select the text and apply the style. That is OK if the glossary list is short, but if not, there are better solutions:

    1. Use a script to read in the glossary term and its definition as part of a text file, spread sheet, or XML file. Depending on how the source file is created, the styling can be part of the process of placing the text.

Define the Glossary term as part of text entry.

    When entering text manually, the user can apply a special character at the point in the text where the glossary term ends. You can set up a keyboard shortcut to place the special character if needed.

For simplicity, this demonstration will use the second option and have the user enter a special character between the glossary term and its definition. The special character used will be different depending on the method used for applying the style.

APPLYING STYLE USING A SCRIPT

In the first option above, the text is styled as part of placing the text. For the second option, you can use a script to apply the unique character style to the text up to the point where the special character is entered. This requires having the ability to get the offset of the character within the each paragraph. The problem with this method is that special characters such as nonbreaking spaces cannot be used. (The offset of a nonbreaking space in a paragraph returns 0.) To solve this problem, use any character that returns a valid offset value and substitute it for the special character you wish to use as part of the script.

The following demonstration script assumes the special character used is an underscore (_), the paragraph style to use is “Glossary” and the character style to apply to the glossary terms is “GlossaryBold.” As part of the process, the script will change the underscore to a nonbreakingspace.

To test the script, copy the code to AppleScript Editor. Select the text in the document to be styled and run the script from the editor.

Style Glossary Text

set parastyleName to "Glossary"
set charstyleName to "GlossaryBold"
set findChar to "_"
try
    tell application "Adobe InDesign CC 2015"
        set replaceChar to nonbreaking space
	set docRef to document 1
	set parastyleRef to paragraph style parastyleName of docRef
	set charstyleRef to character style charstyleName of docRef
	set paraCount to count of paragraphs of selection
	set applied paragraph style of every paragraph of selection to parastyleRef
	repeat with i from 1 to paraCount
	    set thisPara to paragraph i of selection
	    set paraRef to (a reference to paragraph i of selection)
	    set oset to offset of findChar in thisPara
	    if oset > 0 then
		set thisText to object reference of (text from character 1 to  character (oset - 1)) of paraRef
		tell thisText to apply character style using charstyleRef
	    end if
            set character oset of paraRef to replaceChar
	end repeat
    end tell
    on error errStr
	activate
	display alert "errStr " & errStr
    end try 

To make this a more usable script, you will want to add a custom dialog from which the user can select the styles for styling the text and determine the characters for the find and replace (findChar, replaceChar).

APPLYING STYLE USING NESTED STYLE

A better solution might be to simply create a nested paragraph style and apply the style to all of the Glossary text. For this purpose, the special character could be a fixed white space character (Type > Insert White Space) or the End Nested Style Character (Type > Insert Special Character > Other). The special character used for this example was the nonbreaking space character simply because it has an easy-to-remember keyboard shortcut (command + option + x). The nested style dialog for our sample nested paragraph style is shown in the screen capture below.

Settings for nested style

TAKING ADVANTAGE OF CHARACTER STYLING IN HYPERLINK SCRIPT

Assuming that the Glossary text is in a series of linked text frames, a list of the glossary terms to use for creating text hyperlinks can be created easily using a script. For this, a simple find text can be used. Because our sample HyperGlossary script creates page destination hyperlinks, its getGlossaryTerms handler (shown below) requires a reference to the text frame, the find string, the character style used for styling the terms, and a boolean to indicate if the list found should be reversed.

(*Returns list of glossary terms found on Glossary pages using character style identified by glossaryStyleRef*)
on getGlossaryTerms(frameRef, findStr, glossaryStyleRef, doReverse)
    tell application "Adobe InDesign CC 2015"
	set find text preferences to nothing
	set change text preferences to nothing
	tell find text preferences
	    set find what to findStr
	    set applied character style to glossaryStyleRef
	end tell
	tell frameRef
	    set foundSet to find text reverse order doReverse
	end tell
	set change text preferences to nothing
	tell find text preferences to nothing
	    set glossaryTerms to {}
	    repeat with i from 1 to length of foundSet
		set end of glossaryTerms to contents of item i of foundSet
	    end repeat
	    return glossaryTerms
	end tell
    end getGlossaryTerms

The handler is called from within a repeat loop that iterates through the list of terms. The entire script HyperGlossaryWDlg can be downloaded from our Feature page for this month.

ON YOUR OWN

Take advantage of the download (there is a version for AppleScript as well as Extenscript). Go through one of the scripts to understand how it was put together. The structure of the script is almost identical for both versions of the script. You may discover some handy routines in the process, and better, refine the script to make it your own.