5 Replies Latest reply on Mar 20, 2013 4:50 AM by Jump_Over

    {AS} How to find line/text breaks in a text?

    DBLjan Level 3



      i checkt various sources but somehow I cant get it done, yet it should be simple:


      I just want to count the characters to the first enter, linefeed or paragraph break (dont know the name in english, the "enter" on the right of my keyboard) of my textselection. I dont know how to interpret the values InD sends back…

      Please help:



      repeat with x from 1 to (count of characters in selection)


            if character x of selection is in {linefeed, return} then

                -- do stuff

           end if


      end repeat


      This doesnt get the text breaks :-(

        • 1. Re: {AS} How to find line/text breaks in a text?
          [Jongware] Most Valuable Participant

          This is based on Javascript, not Applescript, but the principle should be the same:


          A 'character' in InDesign is a base unit of text *including* its formatting, and so it's a composite object. You cannot compare it to a literal character value such as 'return'. To access the *literal* characters, you can use 'contents':


          alert (app.selection[0].contents.match(/^[^\r\n]+/)[0].length);


          (Translate into AS as you see fit )


          This does a number of things in one line:

          1. app.selection[0] is your selected text

          2. its "contents" is a regular string of Unicode characters

          3. the match command does a regular expression, that

            3a. matches anything not 'return' or 'linefeed' from the start of the string

            3b. 'match' returns an *array* of found substrings, which may be 'null' (i.e. it didn't match anything)

            3c. ... however, I *think* that using this regex will always return a single string result -- numbered '0' in JS

          4. .... and the length of this string is the number of regular characters from the start of  your selected text.



          • 2. Re: {AS} How to find line/text breaks in a text?
            DBLjan Level 3

            Actally i cant really translate this to AS because im not so skilled with it, but thank you for your help.
            I somewhere found some AS-stuff using "contents" and didnt know what to make of it, but ill look deeper into that, this should help.

            • 3. Re: {AS} How to find line/text breaks in a text?
              [Jongware] Most Valuable Participant

              Perhaps we can convince you to move over to Javascript?


              Some pros and cons -- other readers are invited to chime in here!




              • you can use it in any supported application in OS X
              • you can use one single script to target different applications (f.i., copying text from one app to another)


              • it's only for OS X
              • personally, I *hate* the 'human readable' syntax. For simple one line commands it works, but with anything involving more than a single "x of y in z" I get lost in my train of thoughts




              • you can use it in any supported application (granted, there aren't too many ... Adobe's software, in general, but also in all Web browsers!)
              • it's platform independent. The same script runs on both Windows and OS X.
              • it has a logical syntax -- my app.selection example above can be read left to right, and all objects and commands right of a period are immediately applicable to the object to its left


              • the syntax is logical, but only if you are a trained programmer It's not as intuitively as AppleScript
              • 4. Re: {AS} How to find line/text breaks in a text?
                DBLjan Level 3

                I see the Pro's for JS, but i have no choice here: As a designer I dont get much time windows to work on scripting. So if i have a project done, i can code some stuff. As im not a trained programmer i find it hard to get into a unknown syntax at all. Somehow i discovered AS and changed this and that in sample scrippts and got a bit better, just enough to enhance my workflow as a designer.


                It is very kind of you to show me the Cons of AS, but i'm stuck with it right now and cant let you guys code my whole scripts in JS, nor i cant change them after that :-)

                But thanks anyway.


                While we're at it, i got stuck with my script, regarding of programming language. Can you help me out or should I open a new thread?

                I have a textselection in a textbox. And I need to know the x-choordinate to the right of the selection, but only in the first line of the selection (if it spans over two or gets broken into another paragraph.) I managed it by counting the characters till a shift in baseline of the char happens. But with superscripted characters or rotated textboxes this isnt the optimal way. So, i asked the question above to figure out of my textseletion contains a breaking character, but this wont work if the text just flows into the next line. Do you have any idea with wich character property to work to get this right? I'll provide a picture here:

                Bildschirmfoto 2013-03-20 um 12.15.40.png

                I only want to get the xchoordinate of the selections first line, no matter what break follows. (To provide the user accidentally selection "over" the lines end).

                • 5. Re: {AS} How to find line/text breaks in a text?
                  Jump_Over Level 5



                  also JS but if we talk about the rules:


                  this your X coordinate of right of first line of selection