7 Replies Latest reply on Mar 14, 2017 2:36 AM by [Jongware]

    Get physical width of character

    SimonLinden Level 1

      In my current projects many paragraphs end with a right flushed cross-reference. I'm using the shift+tab to separate the text from the cross reference and to keep it to the right. As the basic formatting I added two shift+tabs because otherwise the main paragraph would sometimes be justified even on the last line. So now I'm making a script to loop through it all and remove that extra tab where it is not needed. In the screenshot the selected two tabs should be replaced with one, but the reference to the top left needs two tabs (because of the justified text alignment).

      Is there a way to get the width of a character? If there is I could easily determine where two tabs are needed and where I should remove one. Right now I have a script that makes an estimation based on the number of characters in each line. Its decent but requires a lot of manual confirmation. Any suggestions?

       

      tab-width.jpg

        • 1. Re: Get physical width of character
          Peter Kahrel Adobe Community Professional & MVP

          You can measure the difference between the beginning and the end of the found items. Assuming that you've been looking for those double tabs, do this:

           

          // Assumes a myFound array of found sequences of two tabs
          start = myFound[i].insertionPoints[0].horizontalOffset;
          end = myFound[i].insertionPoints[-1].horizontalOffset;
          distance = end - start;
          if (distance > [some value]) {
            // replace the two tabs with one
          }
          

           

          Peter

          • 2. Re: Get physical width of character
            Obi-wan Kenobi Adobe Community Professional

            Hi Peter,

             

            Clever! 

             

            Editors like to make corrections on text! Layout changes could trigger a real mess if no precaution! …

             

            app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "Double-Tab To One! …");
            
            
            function main()     
                {
                    var myDoc = app.activeDocument;
                    app.findGrepPreferences = app.changeGrepPreferences = null;
                    app.findGrepPreferences.findWhat = "~y~y";
                    myFound = myDoc.findGrep();
                    var F = myFound.length;
                    for ( f = F-1; f >= 0; f-- ) {    
                        start = myFound[f].insertionPoints[0].horizontalOffset;  
                        end = myFound[f].insertionPoints[-1].horizontalOffset; 
                        distance = end - start;  
                        distance > 10 && myFound[f].contents = 1397909876;
                        }
                    app.findGrepPreferences = app.changeGrepPreferences = null;        
                    app.findGrepPreferences.findWhat = "(?<!~y)~y(?!~y)";
                    myFound = myDoc.findGrep();
                    var F = myFound.length;
                    for ( f = F-1; f >= 0; f-- ) {    
                        start = myFound[f].insertionPoints[0].horizontalOffset;  
                        end = myFound[f].insertionPoints[-1].horizontalOffset;
                        distance = end - start;
                        distance < 0 && myFound[f].insertionPoints[-1].contents = 1397909876; 
                        }
                    app.findGrepPreferences = app.changeGrepPreferences = null;
                }
            

             

            (^/) 

            • 3. Re: Get physical width of character
              Kasyan Servetsky Level 5

              See also this thread.

              — Kas

              1 person found this helpful
              • 4. Re: Get physical width of character
                SimonLinden Level 1

                Sorry for my late response! This is exactly what I need!

                • 5. Re: Get physical width of character
                  SimonLinden Level 1

                  Thanks - I actually searched for this issue but didn't find that thread. I noticed that in my case I must use insertionPoints instead of characters because the tab I measure is just one character - but has two insertionPoints.

                  • 6. Re: Get physical width of character
                    Peter Kahrel Adobe Community Professional & MVP

                    > the tab I measure is just one character - but has two insertionPoints.

                     

                    All characters have two insertion points.

                    • 7. Re: Get physical width of character
                      [Jongware] Most Valuable Participant

                      Peter Kahrel  wrote

                       

                      > the tab I measure is just one character - but has two insertionPoints.

                       

                      All characters have two insertion points.

                       

                      To expand on that: the first insertionpoint is with the cursor to the left, the second with the cursor to the right. That way, you can use 'aCharacter.insertionPoints[0].contents = "(";' and aCharacter.insertionPoints[1].contents = ")";' to add something either to the left or the right of any character, which would otherwise not be possible.