Automating InDesign CS6


Got Fractions?

Tuesday, May 21, 2013

Got Fractions?

If you are painstakingly formatting fractions—-numerator(s) over virgule followed by denominator(s)—-listen up. You have several options for taking the work out of this workflow, especially if you are using Open Type fonts.

Using Glyphs

Many fonts such as Minion Pro and Caslon Pro include common fractions such as 1/4, 1/2, 1/3, 5/8, and 7/8 as part of their Glyph set. Open the Glyph palette (Type > Glyphs). Make sure your font is selected (at the bottom of the palette). Find your fractions and double-click on each of the ones you want to access. This puts the glyph in the Recently Used list at the top. When you need a fraction, place your cursor in the text location, and double-click on the fraction in the Glyph palette.

If you need to do this more than a few times within a document, you may want to set up your paragraph styles to support the Open Type Fraction feature. (See discussion following.)

Using the OTF Fraction Feature

This option works with Open Type Fonts only.

  • Select the paragraph style for the document text that contains fractions.
  • Double-click on the paragraph style listing in the Paragraph Styles panel. (This opens the Paragraph Style Options panel for the style.)
  • Click on GREP Style in the options listing. Inside the GREP Style category window, click on the New GREP Style button at the bottom.
  • Change the To Text field to read as follows:
  •      (?<\d)(?<!\d/)\d+/\d+(?!/\d)(?!\d)
  • Click on the word [None] in the Apply Style field to reveal its drop down arrow. Click on the arrow to open the list. From the list, select New Character Style.
  • In the New Character Style window, give the style a name such as Fractions (Style Name field).
  • Click on OpenType Features in the options listing. (This opens the OpenType Features panel for the style.) Click to place a checkmark in front of the Fractions option.
  • Dismiss the two dialog boxes by clicking on their OK buttons.

Now, with the modified paragraph style selected, begin typing. As soon as you enter a word ending character following a fraction [number(s), slash, number(s)] the fraction will format as if by magic. This fraction feature even works with imported text. Apply the paragraph style to the imported text and the work is done for you.

There is only one problem with this solution, if your fractions occur in a number of paragraph styles, you need to repeat the grep style procedure for each style.

Using a Script

With the help of InDesign's find/change grep preferences, a script can style all of your fractions in one fell swoop without considering paragraph styles. The styles just need to use Open Type fonts that support the Fraction feature. For our example, we will have the user select the target text and then run our script. For this we will need the following functionality:

  • identify a GREP pattern for finding text
  • identify the text formatting for the found text (Fraction feature)
  • get text selection
  • set options for including/or excluding specific text options within the selection
  • use find/change GREP to find all text instances matching the regular expression pattern and apply the text formatting for the found text (Fraction feature)

Let's see how this can be done:

AppleScript

--establish GREP pattern
set grepPattern to "(?<![0-9])(?<![0-9]/)+[0-9]+(?!/[0-9])(?![0-9])"
--establish change attributes for found text 
set changeattrib to {«class otfr»:true} --otf fractions feature 
try --get reference to text selection 
   set textselected to getstoryselected() --if text is selected, use change grep 
   if textselected is not missing value then 
      set foundset to findchangegrep(textselected, greppattern, changeattrib) 
   end if 
on error errstr 
   display alert ("error: " & errstr)
end try 
--==========
 --handlers 
--==========
--returns reference to text selected; otherwise returns missing value 
on getstoryselected() 
   set textselected to missing value 
   tell application "Adobe InDesign CS6" 
      set thesel to selection 
      --make sure selection is not empty 
      if length of thesel > 0 then 
         --make sure selection is text 
         if class of item of thesel is text then 
            set textselected to parent story of selection 
         end if 
      end if 
   end tell 
   return textselected 
end getstoryselected 
(*changes attributes for text matching greppattern*) 
on findchangegrep(textref, greppattern, changeattrib) 
   tell application "Adobe InDesign CS6" 
      tell find change grep options 
         set properties to {include footnotes:true, include hidden layers:true, include locked layers for find:true, include locked stories for find:true, include master pages:true} 
      end tell 
      --clear find change grep preferences
      set find grep preferences to nothing 
      set change grep preferences to nothing
      --set find change grep preferences
      set find what of find grep preferences to greppattern 
      set properties of change grep preferences to changeattrib 
      --do the find change
      tell textref 
         set textlist to change grep 
      end tell 
      --reset find change grep preferences
      set find grep preferences to nothing 
      set change grep preferences to nothing 
   end tell 
   return textlist 
end findchangegrep

ExtendScript

main();
function main() {
    try {
        var textRef = textOrFrameSelected();
     } catch (e) {
         alert (e)
        return;
     }
    //define find change options
    var findChangeOptProps = {includeFootnotes:false, includeHiddenLayers:false, includeLockedLayersForFind:false, includeLockedStoriesForFind:false, includeMasterPages:false};
    //establish GREP pattern
    var grepPattern = "(?<![0-9])(?<![0-9]/)[0-9]+/[0-9]+(?!/[0-9])(?![0-9])"; 
    //establish change attributes for found text
    var changeAttrib = {otfFraction:true};
    var changeText = undefined; 
    var findAttrib = undefined;
    var foundSet = findChangeGrep_Attribute (textRef, grepPattern, changeText, findAttrib, changeAttrib, findChangeOptProps);
    alert ("" + foundSet.length + " instances changed");
}
/*==============
HELPER FUNCTIONS
================*/
////////////////////////////////
//returns text frame reference if frame or text item selected
function textOrFrameSelected() {
	if (app.selection.length == 0) {
		throw ("Requires text or  text frame selection");
	}
	var selItem;
	var selClass = (app.selection[0].constructor.name);
	switch (selClass){
		case "TextFrame":
			selItem = app.selection[0].parentStory;
			break; 				
		case "Story":	
		case "Text":
		case "TextStyleRange":
		case "InsertionPoint":
		case "TextColumn":
		case "Paragraph":
		case "Line":
		case "Word":
		case "Character":
			selItem = app.selection[0].parentStory;
			break;
		default:
			throw ("Selected object is not text or text frame");
	}	
	return selItem;
}
//////////////////
function findChangeGrep_Attribute (textRef, grepPattern, changeText, findAttrib, changeAttrib, findChangeOptProps) {
	initGrepPrefs();
	if (grepPattern != undefined) {
		app.findGrepPreferences.findWhat = grepPattern;
	}
	if (changeText != undefined) {
		app.changeGrepPreferences.changeTo = changeText;
	}
	if (findAttrib != undefined) {
		app.findGrepPreferences.properties = findAttrib;
	}
	if (changeAttrib != undefined) {
		app.changeGrepPreferences.properties = changeAttrib;
	}
	app.findChangeGrepOptions.properties = findChangeOptProps;
	var foundSet = textRef.changeGrep();
	initGrepPrefs();
	return foundSet;
	//initialize Grep preferences
	function initGrepPrefs () {
		app.findGrepPreferences = NothingEnum.NOTHING;
		app.changeGrepPreferences = NothingEnum.NOTHING;
	}
}

If you are observant you will notice that the ExtendScript version differs slightly from the AppleScript version in that the user can either select a text frame or text. Other than that, the scripts are pretty much the same.

If you decide to use a script, you will want to place the script in the Script panel for your version of InDesign (5.0 or higher). You can automate the process further by assigning a keyboard shortcut to the script.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=15239&PostID=1014811&A=Trackback
Trackbacks
Post has no trackbacks.