Skip to main content
Inspiring
January 31, 2012
Answered

targeting basics

  • January 31, 2012
  • 1 reply
  • 980 views

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:

<code>

<devices>

          <device>

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

                    <type>VCO</type>

                    <part_number>DS001</part_number>

                    <supply_voltage>

                              <minimum>3</minimum>

                              <maximum>18</maximum>

                    </supply_voltage>

                    <package>

                              <type>DIP</type>

                              <pins>16</pins>

                    </package>

                    <price>3.25</price>

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

          </device>

          <device>

                    <name>MultiWave Voltage Controlled Oscillator</name>

                    <type>VCO</type>

                    <part_number>DS002</part_number>

                    <supply_voltage>

                              <minimum>3</minimum>

                              <maximum>15</maximum>

                    </supply_voltage>

                    <package>

                              <type>SOIC</type>

                              <pins>18</pins>

                    </package>

                    <package>

                              <type>DIP</type>

                              <pins>18</pins>

                    </package>

                    <price>3.79</price>

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

          </device>

</devices>

</code>

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

This topic has been closed for replies.
Correct answer Jongware

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

myDocument.XMLElements.item(1).XMLElements.item(1)

|

myDocument.XMLElements.item(1).XMLElements.item(2)

:

myDocument.XMLElements.item(1).XMLElements.item(n)

and each of them may contain items of its own:

myDocument.XMLElements.item(1).XMLElements.item(3).XMLElements.item(1)

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.

1 reply

Jongware
Community Expert
Community Expert
January 31, 2012

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.

BigGunNAuthor
Inspiring
January 31, 2012

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?

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
January 31, 2012

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

myDocument.XMLElements.item(1).XMLElements.item(1)

|

myDocument.XMLElements.item(1).XMLElements.item(2)

:

myDocument.XMLElements.item(1).XMLElements.item(n)

and each of them may contain items of its own:

myDocument.XMLElements.item(1).XMLElements.item(3).XMLElements.item(1)

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.