7 Replies Latest reply on Oct 3, 2012 10:20 AM by Kasyan Servetsky

    How to get contents string from itemByRange?

    Kasyan Servetsky Level 5

      Dear all,


      Here is what I am trying to achieve:

      I selected a word and want to get the contents of the 1st word in the string (the name of a person).


      In fact, the selected text is the source* of a hyperlink and my goal is to get the person’s name it points to.

      *selection[0] is the same as hyperlink.source.sourceText


      Since I can’t get the paragraph or line where the selected word is (or simply don't know how to do this), I am trying to get it from the story: starting from the 1st character of the selected word and moving backwards character by character comparing its contents. When it hits “\r”, I know that’s the beginning of the line. This way I can get the part of the line I am interested in with itemByRange() method which returns “Array of Character”.

      function Main() {
          var doc = app.activeDocument;
          var sel = app.selection[0];
      function GetName(txt) {
          var lastCharacter = txt.characters[0];
          var character = txt.characters[0];
          var story = txt.parentStory;
          var index = lastCharacter.index;
          while (character.contents != "\r") {
              character = story.characters[index];
          // Array of Character itemByRange (from: varies, to: varies)
          // Returns the Characters within the specified range. 
          var arr = story.characters.itemByRange(character, lastCharacter);
          var str1 = arr.contents[0];
          var str2 = str1.match(/^\S+/);
          return str2;

      My problem is that I can’t get the contents -- str1 -- by script. However, I can see in Data Browser that it’s there. If I click str1, it shows up:


      If I type str1 in Console, I get it too:


      I also tried to use:





      but it didn’t work either.


      I must be missing something very obvious, but I am stuck. Can anybody help me, please?