4 Replies Latest reply on Jan 14, 2010 5:08 PM by PaulR72

    [CS3 JavaScript] Value, or reference to an object

    PaulR72 Level 1

      Hello all,


      My programming background was non object-oriented C from when I was a youngster, where I vaguely remember it always seemed easy to know whether a function or expression was going to return a value or a reference to an object.


      Here's an example. When I take in corrections to InDesign files, I often want to do a very quick search, down from where the insertion point is to the end of the story, but just with one keyboard depression -- not having to click and click and type into InDesign's own find/change dialogue box. So I wrote a quick script to do that, where one can enter a quick search string.


      But trying to isolate the range of text to search is difficult.


           mySelection = app.selection[0];

           textrangeToSearchWithin = mySelection.parentStory.characters.itemByRange(mySelection.index, mySelection.parentStory.characters.length-1);


      The above itemByRange() method seems to return a string, not a reference to the object within InDesign itself. I've tested this by finding the constructor name of its parent and parent's parent -- both don't seem right. But having a reference to the object within InDesign itself is required if I am to search the range of text using InDesign's findText() method, and then determine what the index within the story of the first found instance is.


      Can I force itemByRange() to return a reference? And a more general question, how predictable are returns from InDesign's methods?


      My questions could be because of my not being very well versed with JavaScript, rather than anything to do with InDesign, but I couldn't find any answers on the 'net.


      Bye bye



        • 1. Re: [CS3 JavaScript] Value, or reference to an object
          Harbs. Level 6

          itemByRange can take some getting used to. The way you used it, it 

          returns a Character-Array object (it's kind of hard to explain).


          You can change that into a normal array of Characters with 



          A better way to get a text range is:


          mySelection = app.selection[0];

          textrangeToSearchWithin = 






          • 2. There are keystrokes...
            John Hawkinson Level 5

            You know about these, right?:


            Keys for finding and changing text




            Mac OS

            Insert selected text into Find What box



            Insert selected text into Find What box and finds next



            Find next occurrence of Find What text

            Shift+F2 or Alt+Ctrl+F

            Shift+F2 or Option+Command+F

            Insert selected text into Change To box



            Replace selection with Change To text



            • 3. Re: [CS3 JavaScript] Value, or reference to an object
              PaulR72 Level 1


              Hiya Harbs,


              Thanks for your info about using texts.itemByRange. Just tested it, and the item returned was of type "Text", which is just what I wanted.


              It seems odd to me that a texts.itemByRange() would accept a character and not an index integer as arguments. It also seems odd that it returns a single Text object, and not an array of Texts. But it works -- which is the main thing!


              Many thanks





              • 4. Re: There are keystrokes...
                PaulR72 Level 1

                Hi John,


                Thanks for your post. I know about those keyboard shortcuts, but they won't be much use for me. When I'm taking in corrections I'll tie the script to a keyboard shortcut and use it to quickly jump to a string of characters some lines further down. This is to save using the mouse too much (RSI!) and for general speediness. May not save much time, but it all mounts up -- my job can be quite repetitive!


                I could do a control-F and type in a string into the InDesign find dialogue box then tap enter, but there will often be, say, a paragraph style in the "find format" box already, or the last find I did may have been case sensitive, and so it can take a few mouse clicks to reset the find dialogue box.