23 Replies Latest reply on Sep 21, 2016 9:23 AM by ZedGeneve

    How to get the Column number

    ZedGeneve

      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

        • 1. Re: How to get the Column number
          Trevorׅ Adobe Community Professional

          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

          • 2. Re: How to get the Column number
            ZedGeneve Level 1

            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!

            • 3. Re: How to get the Column number
              Trevorׅ Adobe Community Professional

              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?

              • 4. Re: How to get the Column number
                ZedGeneve Level 1

                 

                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

                • 5. Re: How to get the Column number
                  Trevorׅ Adobe Community Professional

                  Can you embed the screenshot using the forums insert image button.

                  Sorry for the inconvenience

                  • 6. Re: How to get the Column number
                    Peter Kahrel Adobe Community Professional & MVP

                    Your screenshot doesn't show whether you have a single text frame with 2 columns, or two separate text frames next to each other. Which is it?

                    • 7. Re: How to get the Column number
                      Trevorׅ Adobe Community Professional

                      My bet it's the latter and hence the white men

                      • 8. Re: How to get the Column number
                        ZedGeneve Level 1

                        SODu

                         

                        SODy

                         

                        You gave me a cold sweat, but I'm pretty sure each page is a 2 columns page, as i mentioned, i'm able to go to TextColumn#

                         

                        Are these pics better?

                         

                        Thanks a lot for the time you're taking to help a poor noob like me.

                        • 10. Re: How to get the Column number
                          Laubender Adobe Community Professional & MVP

                          ZedGeneve wrote:

                           

                          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!!!

                          Hi Ed,
                          I think you did not copy/paste all the code from Trevor's snippet, but made some mistakes by rewriting it; or by rewriting parts of it.

                           

                          The method itemByRange() needs an opening and a closing bracket. The closing one is missing!

                          It's not textColumns[0].length - 1, but simply textColumns.length - 1

                           

                          Further: Trevor's snippet is working fine.

                          But it will not get you very far, because you are working with single column text frames, that are threaded together in a column layout.
                          For this kind of layout selected text is always in column one. How could it be different? You are inspecting the text columns of a text frame and not the layout of different text frames on the page.

                           

                          And: The Adobe Forum User Interface is providing formatting means for code representation.

                          If code is formatted as code it will be much more easier for participants to read and understand code.

                          Select text you are intending for code formatting, go to >> (Insert) Synatx Highlighting and choose javascript

                           

                          Your cited line of text intended as code would be formatted like that:

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

                           

                          Hi Trevor,

                          perhaps you should explain why your snippet is working.

                          Ed is a beginner.

                           

                          Regards,
                          Uwe

                          • 11. Re: How to get the Column number
                            ZedGeneve Level 1

                            That's what i did, feeding a TinyGrab link; here it is again with the pics this time.

                             

                            Capture d’écran 2016-09-18 à 17.51.33.png

                             

                            Capture d’écran 2016-09-18 à 18.01.59.png

                            • 12. Re: How to get the Column number
                              Laubender Adobe Community Professional & MVP

                              The code formatting in the forums is only available when using "Use advanced editor".
                              And "Use advanced editor" is only available, if you do not reply in your Inbox.

                               

                              Admittedly that's a little "Catch 22" situation.
                              And I hate the Forum developers for that.

                               

                              Regards,
                              Uwe

                              • 13. Re: How to get the Column number
                                ZedGeneve Level 1

                                Well as a beginner, when i got the error message, i tried that very stupidly, but yes i got an error message with the original text of course:

                                Capture d’écran 2016-09-18 à 18.12.42.png

                                 

                                I'm really puzzled as I created pages, then asked 2 columns, put my text in them.

                                 

                                In my previous version, i'm perfectly able to select columns, one after the other:

                                main();

                                function main(){

                                    if(app.documents.length==0){return}

                                              var myDoc = app.documents[0];

                                              var mySelection = app.selection[0];

                                              var myStory = app.activeDocument.stories[0];

                                              var myTextFrame = app.selection[0];

                                              var myPath=["", "", "", "", "", "", "", ""];

                                              var leParStyle= "";

                                              var leCarStyle ="";

                                              var laCouleur= "";

                                              var chemin ="";

                                              var niveau = 0;

                                              var oldLine="";

                                              var i=0;

                                            

                                             

                                              for(colonne=0; colonne<myStory.textColumns.count() <11;colonne++){

                                  

                                                //regardons sur quoi on tombe au début de la colonne, en analysant le premier mot

                                               myStory.textColumns[colonne].words[0].select();

                                              mySelection = app.selection[0];

                                             

                                              leParStyle = mySelection.appliedParagraphStyle.name;// heading 1 à 8

                                              niveau = leParStyle.slice(leParStyle.length-1);// récupère le dernier caractère du style heading n

                                              leCarStyle=mySelection.appliedCharacterStyle.name;

                                              laCouleur= mySelection.fillColor.name;

                                                //si la couleur est != Black, on est devant une césure de longue rubrique

                                               

                                                //on commence par regarder sir nous avons déjà un chemin,

                                                if (myPath[1] != ""){

                                                    chemin="";

                                                    for (i=1;i<niveau;i++){

                                                        chemin= chemin+ myPath[i].contents+ "|";

                                                        }

                                                    if (niveau=1 & leCarStyle = "MotCle"){

                                                        //si la colonne commne,nce par un chapitre on ne fait rien

                                                        }else if (myPath[1].length>0){ //ne rien faire pour la première colonne

                                                           

                                                            switch(laCouleur){

                                                                    //               la colonne commence par un soustitre                    

                                                                    case "Black":

                                                                        oldLine = myStory.textColumns[colonne].lines[0].contents

                                                                        myStory.textColumns[colonne].lines[0].contents= chemin+"\r"+oldLine;

                                                                        myStory.textColumns[colonne].paragraphs[0].appliedParagraphStyle="rappel";

                                                                        myStory.textColumns[colonne].paragraphs[0].words[0].appliedCharacterStyle="MotCle";

                                                                        myStory.textColumns[colonne].paragraphs[0].keepWithNext=1;

                                 

                                                                        //app.selection[0].appllyParagraphStyle()="rappel";

                                                                        break;

                                                                   

                                                                    // la colonne comence par une césure de rubrique

                                                                    default:

                                                                    break;

                                                               }

                                                        

                                                        

                                                         }//else

                                                   

                                                }

                                             

                                              //maintenant on met à jour les derniers chemins pour les niveaux

                                              for(i=1; i<=8;i++){

                                                app.findGrepPreferences = null;

                                                app.changeGrepPreferences = null;

                                                if (i==1){

                                                    app.findGrepPreferences.findWhat = "^\\u+";

                                                 }else{

                                                     app.findGrepPreferences.findWhat = "^[\\l-]+";  

                                                }

                                                app.findGrepPreferences.appliedParagraphStyle = "heading "+i;

                                                var go= myStory.textColumns[colonne].findGrep();

                                                if(go.length>0){

                                                 //alert("Dernier Niv "+i+" | " +go[go.length-1].contents); //attention l'indice 0 c'est la dernière trouvée, tandis que l'indice length-1 est le premier trouvé si on a la recherche en sens invers vers l'arrière

                                                 myPath[i]= go[go.length-1].paragraphs[0].words[0];

                                                

                                                }else{break;}

                                 

                                              }

                                • 14. Re: How to get the Column number
                                  ZedGeneve Level 1

                                  For sure it's a text frame with 2 columns!

                                  • 15. Re: How to get the Column number
                                    Laubender Adobe Community Professional & MVP

                                    Hi Ed, your screenshot is showing two different text frames selected.

                                     

                                    The in and out ports for flowing text through several text frames are clearly visible.
                                    And they contain small "triangles" ( or "arrows" ) indicating that the frames are indeed threaded.


                                    If you would turn on "View Threaded Text Frames" you should also see connecting guides:

                                     

                                    ThreadedTextFrames-2-frames-selected.png

                                     

                                    Alternatively:

                                     

                                    OneTextFrame-4-columns.png

                                    Here we can clearly see column guides of the text frame.

                                     

                                    Regards,
                                    Uwe

                                    • 16. Re: How to get the Column number
                                      ZedGeneve Level 1

                                      Ok so clearly it's the case 1

                                      I did the following:

                                      1) Created a new page

                                      2) in the menu Page -> Margins and columns, set the col numbers to 2.

                                       

                                      Why in this case if i count myStory.textColumns.count()  i got the right number of cols?

                                       

                                      Sorry again but i hope other beginners like me will stumble here too.

                                       

                                      Thanks

                                      • 17. Re: How to get the Column number
                                        ZedGeneve Level 1

                                        OK i understand now, there's the option from the Page menu i used, that seems to create 2 Frames on the page

                                        and the Object menu, in which you set the TextBlock options and then the number of columns...

                                         

                                        Could you be kind enough to explain me when to use one or the other?

                                         

                                        Sorry folks for all the pain.

                                        • 18. Re: How to get the Column number
                                          Laubender Adobe Community Professional & MVP

                                          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:

                                           

                                          GetIndexOf-textColumn-TREVOR.png

                                           

                                          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

                                          • 19. Re: How to get the Column number
                                            Trevorׅ Adobe Community Professional

                                            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

                                            • 20. Re: How to get the Column number
                                              Trevorׅ Adobe Community Professional

                                              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

                                              • 21. Re: How to get the Column number
                                                ZedGeneve Level 1

                                                Great! Rustic but efficient.

                                                Why don't they give us a sample function to get this info?

                                                Thanks a lot for your kindness.

                                                Best

                                                Ed

                                                • 22. Re: How to get the Column number
                                                  Trevorׅ Adobe Community Professional

                                                  Hi Ed,

                                                   

                                                  Can you please mark the question as answered.

                                                   

                                                  Thanks

                                                  • 23. Re: How to get the Column number
                                                    ZedGeneve Level 1

                                                    Setting my shame aside, i created a new document, this time setting a text block on 2 columns.

                                                     

                                                    The thing is : now if i type something like app.activeDocument.stories[0].textColumns.count(), i got 0....

                                                     

                                                    In short, i d'ont understand how to access the famous colums now.

                                                     

                                                    Thanks