6 Replies Latest reply on Feb 2, 2012 8:28 AM by BigGunN

    targeting basics

    BigGunN Level 1

      First let me appologize for what will seem a very basic question, but I am new to InDesign and Scripting and just had this project laid on me, and I can't seem to get my head around it.


      Lets say I have 4 pages in a document.

      Page 1 has 1 textframe

      Page 2 has 2 textframes

      Page 3 has 3 text frames

      Page 4 has 4 text frames


      Does using myInDesign.Documents.Item(1) target a page, a document, or a textframe?

      Does using myInDesign.Pages.Item(1) target a page, or a textframe?

      How do I target Page 2?

      How do I target Textframe 3 on page 4?



      Next, how do I target XML Nodes.  For example using the example XML from Adobe I have something like this:




                          <name>Pulse/Triangle Voltage Controlled Oscillator</name>












                          <description>A wide range, temperature-compensated voltage controlled ocsillator featuring 1 volt per octave exponential response. Pulse and triangle waveform output.</description>



                          <name>MultiWave Voltage Controlled Oscillator</name>
















                          <description>A wide range, temperature-compensated voltage controlled ocsillator featuring 1 volt per octave exponential response. Pulse, triangle, and sine waveform output.</description>





      If I target myDocument.XMLElements.Item(1) I all the data in all the nodes, but if I try this: myDocument.XMLElements.Item(2) I get an error saying it doesn't exist.


      So my question is, what is the best way to get the value of a node in the tree, for instance "<part_number>"?


      thanks in advance

        • 1. Re: targeting basics
          [Jongware] Most Valuable Participant

          If you taget 'an' item in a document, it depends on the order of creation. No need to do that, though, as you can *specifically* first target a page and then any item on it. But in the case of multiple items per page, you still have to have *something* to go on. Position, for example, or color, or label, or contents. You need to interrogate all likely objects for the attribute of choice before you can know for sure you got the one you wanted.


          As for you XML question: is this Visual Basic? If it is (next time you'd want to mention that), it's very easy. VB starts counting its objects at index 1, and so XML item #1 is the item called <code>. Since this is the root object, there are no other elements in the same hierarchy; all of the others are descendants ('child nodes') of that first node.

          • 2. Re: targeting basics
            BigGunN Level 1

            Thank you so much for your reply, although I am apparently still missing something. And yes I am using VB.


            So in the case of specifically first targeting a page, then an item, How do I interrogate the likely objects on the page?  If there is a heirarchy, then I would think that I would be able to get to the first textframe created on a page by using its index value like so: myInDesign.Pages.Item(1), and the second frame create with this: myInDesign.Pages.Item(2).  Or when I create a textframe in my script, do I need to specifically give it a tag in order to target it?


            On the XML side, so if index 1 is <code>, what is the index of <part_number>, or am I completely wrong in how I am thinking about that.  I know I should be able to use XPath and the glue code somehow, but I cannot find any examples of how to do it or what it is supposed to look like.  Is there a way to use an xpath with a something like myTextFrame.PlaceXML?


            Update:  So jongware, I just came across your Object Model page.  I think it will be extremely useful after I get some understanding, but right now it is just confusing me more.  So using that as guide, If I wanted to grab one "device" node and put it into a text frame, could I use something like this:     myDocument.XMLElements.Item(1) item("device")    This obviously isn't correct since I keep getting errors.  Can you give me an example of how this should look?

            • 3. Re: targeting basics
              [Jongware] Most Valuable Participant

              The first XMLElement -- the root -- contains all other elements as its children, like this:








              and each of them may contain items of its own:




              and so on and so forth. I don't think VB allows this lookup-by-name you tried. Then again, even in my native language (Javascript), moving around this tree structure is a bit tricky. Hence the suggestion of XPath (which is, of course, not necesseraly *easier* if you don't know that either). I have no idea if the glue code exists for VB.

              • 4. Re: targeting basics
                BigGunN Level 1

                Jongware - that was extremly helpful and exactly what I needed for the XML.  I was able to write out nodes in newly created pages and textframes, so thank you very much.


                Would it be possible to also give me a few examples for my first question, regarding pages and text frames.  If it is similar to the XML I am assuming it would be something like this...


                myInDesign.Pages.Item(1).TextFrame.Item(1) - target first frame created on first page

                myInDesign.Pages.Item(1).TextFrame.Item(2) - target second frame created on first page

                myInDesign.Pages.Item(2).TextFrame.Item(1) - target first frame created on second page


                If the above is not correct, then I guess i am still not clear of what "Item" is refering to. So to go back to my original post...

                Does "Item" in myInDesign.Documents.Item(1) refer to a page in the document, or the document itself?

                Does "Item" myInDesign.Pages.Item(1)  refer to a page, or a textframe on the page?

                Thanks again



                Update: ok I think I answered this after playing around with things today.  So I think the short answer is that ITEM refers to whatever preceeds it, so myInDesign.Pages.Item(1) refers to pages, and TextFrame.Item(1) refers to textframes.


                I still have to figure out how it orders things though.  I tried creating two frames on a page then replacing the content in the second one I created with TextFrames.Item(2), but it replace the content in the first one I created instead.

                • 5. Re: targeting basics
                  [Jongware] Most Valuable Participant

                  You are correct in both your update remarks. "Item" is used on a collection of (uh) items to get a handle to a single one. (Your textframes example is probably reading "myInDesign.Pages.Item(1).TextFrames.Item(1)" in your code, plural, because otherwise it will not work.)


                  In Javascript this notation is slightly easier: myInDesign.pages[0].textFrames[0] ('0' since JS starts counting at that instead of 1).


                  You cannot reliably use indexing items "directly"! You cannot expect, in general, that if you have 2 text frames on a page, Item(1) is the top one and Item(2) the bottom one; or, Item(1) is the first one created and Item(2) the second one. (Usually it is, but InDesign may move the indices around at will.)


                  If you have two text frames and you want to change the contents in one of them, you need to justify your choice. That is, if one of the text frames has a solid border of 1 pt wide, and the other one has a border of 2 pts wide, then you can decide "I want to change the one that has a 1 pt border". To do so you would examine the text frames on your page (bogus VB follows, since I don't "do" VB):


                  If myInDesign.Pages.Item(1).StrokeWeight = 1 Then

                    myInDesign.Pages.Item(1).Contents = "Hello World"

                  End If


                  (There are more elegant ways to do this; it depends on what you attempt to do, how many text frames there may be, etc. etc.)

                  1 person found this helpful
                  • 6. Re: targeting basics
                    BigGunN Level 1

                    Thank you again, I really appreciate the time you have given me.


                    That definitely gives me a direction to go in and things to try.