Skip to main content
Known Participant
September 18, 2016
Answered

How to get the Column number

  • September 18, 2016
  • 2 replies
  • 1442 views

Hi to all,

I'm sorry to ask this question as i found 2 different answers, tried them without any result. So before becoming mad i prefer to ask !

So given mySelection = app.selection[0], how is it possible to get the column in which is the selection?

I tried app.selection[0].parentTextFrames[0].characters.itemByRange(0, x)

     with x being app.selection[0].index.

     but then i'm stuck as if i add ".textColumns" i get an error

I explored the second way using app.selection[0].textColumns.item(1).insertionPoints.item(1), but again i'm stuck.

As a beginner i don't understand why there is no simple built-in function to call in order to get such a basic result

Any help would be MUCH appreciated

Thanks

Ed

This topic has been closed for replies.
Correct answer Trevor:

Hi Uwe,

The snippet is only going to work properly in cases where the frames are textColums and not just linked textFrames.

I think that's quite predictable.

I'm going out now but if it's not answered when I come back I'll try answer Ed's question

Trevor


O.k. here goes,

For and only for linked text frames.

// For and only for linked text frames.

// Warning

// The Good: This will PROBABLY work for you

// The Bad :THIS IS A REALLY PRIMITIVE METHOD

// The Ugly: It could fail in COUNTLESS cases

// Here goes......

var mySelection, textFrame, parentPage, c;

mySelection = app.selection[0];

textFrame = mySelection.parentTextFrames[0];

parentPage = textFrame.parentPage;

c = 0;

while(textFrame && textFrame.parentPage === parentPage){

    textFrame = textFrame.previousTextFrame;

    c++;

}

alert('Column number: ' + c);

Now regarding the question of linked text frame columns versus single text frames with multiple text columns or span columns. It all depends on what you are trying to achieve and your workflow.

The are scripts around written by me and others that convert multiple text columns to linked textframes and vise versa.

If you want to know what's more suitable for you to use with your workflow, then I suggest the regular ID forum.

HTH

Trevor

2 replies

ZedGeneveAuthor
Known Participant
September 18, 2016

Too late i see the little men in white coming to pick me!

myColumnIndex = mySelection.parentTextFrames[0].characters.itemByRange (0, mySelection.index).textColumns[0].length - 1;

gives me an Object invalid error!!!

I tried for fun myColumnIndex= app.selection[0].textColumns[0].index; which ***seems** to produce a valid result, then

var myColumn = mySelection.parentTextFrames[0].textColumns[myColumnIndex]; gives me a column, but not it's number

and then var myColumnsFirstWord = myColumn.words[0].contents; gets an Object is invalid too.

Arrrrgh but thanks already for your help!

Trevor:
Legend
September 18, 2016

Perhaps you are confusing textCoulmns with table columns and really need the little white men.

Or maybe your talking about span columns.

Or maybe your selection is on a textFrame with only one column?

ZedGeneveAuthor
Known Participant
September 18, 2016

Here's a pic of a page. I got a page setup with 2 columns each in it. For example

myStory.textColumns.count()

gives me the correct overall number of textcolumns, then with myStory.textColumns[colNumber].words[0].select(); i'm able to acces each.

SOCW

Trevor:
Legend
September 18, 2016

Hi Ed

Before you go mad

var mySelection = app.selection[0];

var myColumnIndex = mySelection.parentTextFrames[0].characters.itemByRange (0, mySelection.index).textColumns.length - 1;

var myColumn = mySelection.parentTextFrames[0].textColumns[myColumnIndex];

var myColumnsFirstWord = myColumn.words[0].contents;

alert('myColumnIndex: ' + myColumnIndex + '\nmyColumn: ' + myColumn + '\nmyColumnsFirstWord: ' + myColumnsFirstWord);

HTH

Trevor

Community Expert
September 19, 2016

Hi Trevor,

I tested a bit more. And indeed there are some cases where your snippet from answer #1 is throwing errors.

Still I cannot tell why exactly. Just presenting some results here:

The IDML of my test document can be downloaded from my Dropbox account:

https://www.dropbox.com/s/dyn95xt3n4hqhg1/GetIndexOf-textColumn-TREVOR.idml?dl=0

Regards,
Uwe

Trevor:
Legend
September 19, 2016

Hi Uwe,

The snippet is only going to work properly in cases where the frames are textColums and not just linked textFrames.

I think that's quite predictable.

I'm going out now but if it's not answered when I come back I'll try answer Ed's question

Trevor