Skip to main content
Participating Frequently
October 31, 2012
Answered

How do I set tabs in textFrames with tabStops?

  • October 31, 2012
  • 3 replies
  • 3068 views

Could anyone show me the javascript syntax, or point me to a script, that sets tabStops and positions them.

I can't get it working.

Please ask if any further clarification or explanation would be usefull.

This topic has been closed for replies.
Correct answer Jongware

This works for me. It took some extra time because, uh, well, I accidentally was looking at the InDesign reference ... took me a while to find out why none of the commands worked ...

But my hunch payed off: I was thinking, maybe you need to construct a new tab stop. That's way different than how InDesign works.

/* set up for testing */

doc = app.activeDocument;
ele = {position:[100,0], size:[300,100], tab:[60,90,120,200]};

var rectRef = doc.pathItems.rectangle(ele.position[0]/1, ele.position[1]/1, ele.size[0]/1, ele.size[1]/1);
eleText = doc.textFrames.areaText(rectRef);       
var dataArray= new Array ();
eleText.contents="hello\tworld\thow\tare\tyou\ttoday";

/* Use the following to set tab stops */

t = new Array(ele.tab.length);

for (tab=0; tab<ele.tab.length; tab++)
{
t[tab] = new TabStopInfo;
t[tab].position = ele.tab[tab];
}
eleText.paragraphs[0].tabStops = t;

Other than with InDesign , you cannot add or modify a single tab stop. Instead, the entire array is "read/write". Creating a new array of "TabStopInfo" items and feeding that into your paragraph is a roundabout way of doing stuff, but hey, at least it works.

3 replies

HugoTaitAuthor
Participating Frequently
November 1, 2012

I see that overnight the JavaScript heavyweights have waded in. Thanks Guys. It seems I can't edit my original post now, so I'm having to reply to it.

From what I gather you are saying, I can't create a tabs with a script. But maybe if my template document had a text box containing tabs I could copy the paragraph attributes to a new paragraph and them manipulate the TabStop.position value.  Would that be the way to proceed?

Carlos, you asked to see what I'd done so far so here it is!  As you will see I'm an enthusiastic novice with a lot to learn when it comes to javascript, so any comments or suggested improvements anyone cares to make would be most welcome.

I am creating ai files containing text and graphic elements created from data in a XML file, mostly these are tabular data. Below is the function that handles the text elements.

function layOutText(ele){

    var eleText;

    if (ele.@type=="area") {

        var rectRef = doc.pathItems.rectangle(ele.position[0]/1, ele.position[1]/1, ele.size[0]/1, ele.size[1]/1);

        eleText = doc.textFrames.areaText(rectRef);       

        if (ele.@class=="dataTabbed"){

            var dataArray= new Array ();

            eleText.contents=splitDataString(ele.value.toString());//this is a call to a function that splits and puts in the command characters for the tabs and

            eleText.paragraphs[0].paragraphAttributes.tabStops.length=ele.tab.length();

            for (tab=0; tab<ele.tab.length(); tab++){

             eleText.paragraphs[0].paragraphAttributes.tabStops[tab].tabStopsInfo.position=ele.tab[tab];// this doesn't work

                }

            }

        else {

            eleText.contents= ele.value.toString ();

            }

        if (eleText.paragraphs)eleText.paragraphs[0].hyphenation=false;

        }

    else {

        eleText = doc.textFrames.add();

        eleText.position=[ele.position[0]/1, ele.position[1]/1];

        eleText.contents= ele.value.toString ();

        }

    eleText.textRange.characterAttributes.textFont=app.textFonts.getByName(ele.fontStyle.font.toString());

    eleText.textRange.characterAttributes.fillColor=giveColor(ele.fontStyle.colour.toString());

    if (ele.justification.@type=="center") eleText.paragraphs[0].justification = Justification.CENTER;

    }

I am passing it a XML objects that prints out something like

<textElement type="area" class="dataTabbed">

  <value>Benefit t £9,600 t 40% t 23% r Clarins t £7,000 t 10% t -13% r Dior Beaute t £4,500 t 5% t 26% r No7 t £21,500 t -10% t -16% r YSL t £3,200 t -20% t 37%</value>

  <tab type="left">400</tab>

  <tab type="left">600</tab>

  <tab type="left">800</tab>

  <tab type="left">1000</tab>

  <tab type="left">1500</tab>

  <position type="top">275</position>

  <position type="left">10</position>

  <size type="width">350</size>

  <size type="height">140</size>

  <justification type="left"/>

  <fontStyle class="data">

    <font>Tahoma</font>

    <size>11</size>

    <colour>black</colour>

  </fontStyle>

</textElement>

Thank you.

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
November 1, 2012

This works for me. It took some extra time because, uh, well, I accidentally was looking at the InDesign reference ... took me a while to find out why none of the commands worked ...

But my hunch payed off: I was thinking, maybe you need to construct a new tab stop. That's way different than how InDesign works.

/* set up for testing */

doc = app.activeDocument;
ele = {position:[100,0], size:[300,100], tab:[60,90,120,200]};

var rectRef = doc.pathItems.rectangle(ele.position[0]/1, ele.position[1]/1, ele.size[0]/1, ele.size[1]/1);
eleText = doc.textFrames.areaText(rectRef);       
var dataArray= new Array ();
eleText.contents="hello\tworld\thow\tare\tyou\ttoday";

/* Use the following to set tab stops */

t = new Array(ele.tab.length);

for (tab=0; tab<ele.tab.length; tab++)
{
t[tab] = new TabStopInfo;
t[tab].position = ele.tab[tab];
}
eleText.paragraphs[0].tabStops = t;

Other than with InDesign , you cannot add or modify a single tab stop. Instead, the entire array is "read/write". Creating a new array of "TabStopInfo" items and feeding that into your paragraph is a roundabout way of doing stuff, but hey, at least it works.

Larry G. Schneider
Community Expert
Community Expert
November 1, 2012

Great job, Jongware. Sometimes it's difficult to see the forest for the trees.

CarlosCanto
Community Expert
Community Expert
October 31, 2012

I didn't get it to work, but I haven't covered all possibilities yet, what seems to work is to apply a paragraph style with your tab info in it....now, I'm having a hard time creating the paragraph style with the tab info.

post what you have so far

Jongware
Community Expert
Community Expert
October 31, 2012

"tabStops" is a property of ParagraphAttributes. It's an r/w array, so shouldn't it be possible to add items to it?

CarlosCanto
Community Expert
Community Expert
October 31, 2012

JW, don't you remember we're talking about Illustrator where half the things are not accessible and within the half that is, half of it is broken?

I was able to read the actual tab stop position, set a new tab stop position, read it back to make sure was applied, but nothing seemed to happen to the actual text

Larry G. Schneider
Community Expert
Community Expert
October 31, 2012

You can get info about existing Tab Stops from the Paragraph Attributes but I see no way of setting them by script.