Working with Tabs

I often need to set tabs in certain types of list. The simplest case is a list of abbreviations, in which we want an em-space between the second column and the longest abbreviation in the first column. Here is an example:

CS

Creative Suite

ESTK

ExtendScript Toolkit

ID

InDesign

JS

JavaScript

OMV

Object Model Viewer

VB

Visual Basic

What we need to do is to measure the length of all abbreviations, determine which is the longest, add the width of an em to it, and set a tab stop in each paragraph in the selection. Here is the script (we assume that there are already tabs between the abbreviation and the full form):

// make sure we have a proper selection if (app.selection[0].constructor.name != "Text") exit (); // declare a variable, set it to 0 var longest = 0; // store the selected paragraphs in myParagraphs var myParagraphs = app.selection[0].paragraphs; // find the longest abbreviation for (var i = 0; i < myParagraphs.length; i++) longest = Math.max (longest, myParagraphs[i].words[0].insertionPoints[-1].horizontalOffset); // determine the width of an em var em = myParagraphs[0].characters[0].pointSize; // tab position: position last letter - position first letter plus em var tab = (longest - myParagraphs[0].insertionPoints[0].horizontalOffset) + em; // set tab in all selected paragraphs app.selection[0].tabList = [{position: tab, alignment: TabStopAlignment.leftAlign}]; // set left indent and first-line indent app.selection[0].leftIndent = longest; app.selection[0].firstLineIndent ...

Get Scripting InDesign CS3/4 with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.