Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Distribute tabbed table equally

Engaged ,
Jan 21, 2010 Jan 21, 2010

Hi All,

I want a script that can distribute tabbed table equally.

Please see my below code:

if (app.selection[0].constructor.name != "Text") exit ();

var myParagraphs = app.selection[0].paragraphs;

var longest = 0;

var para = []

for(b=0;b<myParagraphs.length; b++)

{

para = ((myParagraphs.contents).replace(/\r/,"")).split(/\t/)

}

var longest = new Array();

longest[0] = 0;

longest[1] = 0;

longest[2] = 0;

longest[3] = 0;

longest[4] = 0;

for (a=0; a<5; a++)

{

for(b=0;b<myParagraphs.length;b++)

{

longest = Math.max(parseInt(longest),para.length)

}

}

var textframebound=app.selection[0].parentTextFrames[0].geometricBounds;

var myHeight = textframebound[2] - textframebound[0];

var myWidth = textframebound[3] - textframebound[1];

var totalcolWidth = 0

for (a=0; a<longest.length; a++)

{

totalcolWidth += longest

}

var distribut = myWidth - totalcolWidth

In above code i got the longest value in each column and now i am confused how can i get point size width and set stop tab on each column.
Thanks
Shonky

TOPICS
Scripting
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2010 Jan 22, 2010

Don't use this:

longest = Math.max(parseInt(longest),para.length)

as it returns the length in characters. To get the real width, subtract the Horizontal Offset of the first character from the one of the last character:

alert (app.selection[0].paragraphs[0].insertionPoints[-2].horizontalOffset - app.selection[0].paragraphs[0].insertionPoints[0].horizontalOffset);

That will give you the exact width of the text inbetween.

Note that you converted your text to a plain string before (reading 'contents'), so you can't plug this line in right away. Measuring works on the Words array of a paragraph (and on any Character range as well), so all you have to do is getting a handle to the first and last character of each number and measure the distance inbetween.

---

The second part, handling tabs, is done through the paragraph's TabStops property. Removing all tabs:

tabStops.everyItem().remove();

Adding a tab:

tabStops.add ({position:SomePosition});

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 22, 2010 Jan 22, 2010

Hi Jongware,

Thank you for your response,

longest = Math.max(parseInt(longest),para.length)

with above code i am trying to getting maximum characters length in each column and then i'll multiply with point size width.
But with your code:

app.selection[0].paragraphs[0].insertionPoints[-2].horizontalOffset - app.selection[0].paragraphs[0].insertionPoints[0].horizontalOffset

it give me whole paragraph width not each column.

How can i get each column width with your code please suggest.

coding for tab setting now clear, thanks again.

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 26, 2010 Jan 26, 2010

Hi All,

help me to complete this script.

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 26, 2011 Jan 26, 2011
LATEST

Hi.

I have also been working on this feature, and have come up with the following.

It works fine in CS5, but I have not tested elsewhere. I have not commented yet, as I have just finished.  Maybe you have sorted this already?

The following script has no extensive error catching and reporting, and will work on a text frame, that has multiple lines, the same amount of tabs per line, and the text frame large enough to hold the text when the new tabs are applied.

Anyway, here is the script:

try{ myStart = true; myDoc = app.documents[0]; myFrame = myDoc.selection[0]; myStory = myFrame.paragraphs[0].parentStory; myLineCount = myStory.lines.length; myControlArray = new Array(); for(i=0;i<myLineCount;i++){     myLine = myStory.paragraphs;     myReset();     app.findTextPreferences.findWhat = "^t";     myTabFind = myLine.findText();     myReset();     myTabLength = myTabFind.length;     if(myStart == true){         for(n=0;n<myTabLength;n++){             myControlArray = 0;             }         }     for(n=0;n<myTabLength;n++){         myStart = false;         // Finding the distance of each tab         myIndex = myTabFind.index;         if(n==0){             if((myStory.characters[myIndex].horizontalOffset)-myStory.characters[0].horizontalOffset > myControlArray ){                 myControlArray = ((myStory.characters[myIndex].horizontalOffset) - myStory.characters[0].horizontalOffset) + 4;                 }             }          else{              myEndPoint = myStory.characters[myTabFind.index].horizontalOffset;              myStartPoint = myStory.characters[(myTabFind[n-1].index)+1].insertionPoints[0].horizontalOffset;              if(myEndPoint - myStartPoint > myControlArray){                  myControlArray = (myEndPoint - myStartPoint) + 4;                  }              }         }     } myTabStops = new Array(); for(i=0;i<myTabLength;i++){     if(i == 0){         myTabStops = myControlArray;         }     else{         myTabStops = myControlArray + myTabStops[i-1];         }     } myStory.tabStops.everyItem().remove(); for(i=0;i<myTabLength;i++){       myStory.tabStops.add({position:myTabStops});      } } catch(e){     } function myReset()             {             app.findTextPreferences = NothingEnum.nothing;             app.changeTextPreferences = NothingEnum.nothing;             }

Let me know how it goes for you.

It works on a text frame or insertion point selection.

Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines