5 Replies Latest reply on Dec 2, 2012 4:37 PM by SLTyPete

    Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?

    SLTyPete Level 1

      I have successfully used mapStylesToXMLTags() to apply tags to all styles.

       

      But two things I have utterly failed at:

       

      a) Adding attributes to the XML tags being applied

      b) Applying an XML tag directly to a paragraph without using the auto-style-to-tag system

       

      I can find few if any examples of this. And the documentation is quite opaque (or else I've not learned how to read it correctly. If so, I'd love a pointer to an InDesign Scripting API - reading - tutorial )

       

      ADDING ATTRIBUTES

      I have tried static attributes:

      myDoc.xmlTags.add(tagname, UIColors.RED, withProperties= {label:"test"});

       

      I have tried various forms of dynamic assignment

       

      tagname="foo";

      tagAttr = 'myAttr:"Test"';

      myDoc.xmlTags.add(tagname, UIColors.RED, withProperties= {tagAttr});

      tagname="bar";

      tagAttr = 'myAttr="Test"';

      myDoc.xmlTags.add(tagname, UIColors.RED, withProperties= {tagAttr});

      tagname="baz";

      tagAttr = 'LabelVal';

      myDoc.xmlTags.add(tagname, UIColors.RED, withProperties= {label: tagAttr});

       

      ...but no matter what I try, all I get after mapStylesToXMLTags() is the tag name and color. No attributes are set.

       

      So then I decided maybe mapStylesToXMLTags() doesn't support attributes and I would have to painfully apply the tags myself (not too horrible since later I want to do some fancy tagging anyway...)

       

      APPLY TAG DIRECTLY

       

      I'm simply stymied on this one. I have looked at various examples and they all seem to do the opposite of what I want: they add content to an XML structure rather than apply an XML tag to content!

       

      The closest thing I've seen is here, where he attempted to apply tags to paragraphs... but it was pretty much a total failure.

       

      Anybody got a clue?

       

      Thanks as always,

      Pete

        • 1. Re: Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?
          [Jongware] Most Valuable Participant

          When scripting, you need to take care to follow the same routine as in the UI itself. You do not add an attribute to an XML Tag (which is just a name in the current list of tags), you add it to the tag which is applied to some item -- text or otherwise.

           

          This example assumes you have tags called "Root" (a default name), "story', "par", and "emphasize". Make sure you have a single text frame containing at least two paragraphs. Then run this:

           

          frame = app.activeDocument.textFrames[0];
          // The story itself needs to be tagged.
          // There is already a "Root" element, by default, so we add to this:
          newXmlElem = app.activeDocument.xmlElements[0].xmlElements.add(app.activeDocument.xmlTags.item("story"), frame.parentStory);
          // Then tag the 2nd paragraph of the story
          aTaggedPar = newXmlElem.xmlElements.add(app.activeDocument.xmlTags.item("par"), frame.parentStory.paragraphs[1]);
          // .. and tag the 3rd word ..
          aTaggedWord = aTaggedPar.xmlElements.add(app.activeDocument.xmlTags.item("emphasize"), aTaggedPar.words[2]);
          // Now we have several handles to tagged items
          // so you can add attributes to each of them:
          aTaggedWord.xmlAttributes.add("priority", "1");
          

           

          As you see, you need to "walk" the XML Element tree down from the absolute Root item for every sub-item that you add. Since this example adds one item at a time, the return values -- a direct pointer to the just-added XML element -- can be stored into variables, but for anything longer than this you might want to utilize routines to locate the nth item in the entire tree, or find an item with a certain tag. You can search the forum for recursive functions, or use XML Rules, which utilize an XPath-like notation. XML Rules are discussed in Adobe's Scripting guides.

           

          As for documentation ... It's nigh on impossible to describe each and every function in InDesign's scripting interface, or to provide relevant examples for them. There are (wait--I'm going to count'em..)

          • 2. Re: Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?
            [Jongware] Most Valuable Participant

            That took a while.

             

            There are 8,053 "methods" (functions) in InDesign CS6, for a total of 684 different classes. The classes have 17,762 properties in total. (A number of these classes are shared among the entire CS suite, such as the ScriptUI interface, and some others are built-in classes from Javascript itself. Nevertheless, there should be still some 640-or-so classes unique to InDesign itself.)

             

            Rather than first programming the entire interface and only then writing ample documentation, the programmers took a different road: with each class, property, and method they added (and with each separate parameter of each method as well), they immediately added one or two lines of descriptive text to the same code. That way, the 'documentation' never lags behind the actual code. This is the Javascript Reference, which you can access through the ExtendScript Toolkit Editor, in the Help Menu, or through other ways.

             

            The contents of Adobe's Introduction To, and in-depth Scripting Tutorial have been largely unchanged for some years now, most probably because it would not be very useful to have newcomers start with the Very Latest New Scriptable Features.

             

            The very best resource for scripting just about anything is still this forum. Even more notable, since InDesign's scripting interface is by far the most advanced of all of the Creative Suite, I've seen posters being directed to this forum from both the Illustrator and Photoshop scripting sub-fora.

            • 3. Re: Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?
              SLTyPete Level 1

              Thank you SOOoooo much! Yes, this is very very helpful.

               

              I totally get the more or less self-documenting nature of the code/editor and your nicely formatted version. I suspect examples like what are being generated in this forum will be helpful to others for years to come... and perhaps also for whoever at Adobe updates the tutorials and introductory documents.

               

              Three questions about your example and statement.

               

              First, you coded:

               

              ...xmlElements[0].xmlElements.add...

               

              Why is this not

              xmlElements[0].add

              ...and how did you know to stick in the extra xmlElements reference?

               

              Next, you wrote

              .xmlTags.item("par")

              (and similar throughout). Do I understand that these could also have been written as

              .xmlTags["par"]

              ???

               

              Third, with respect to:

              As you see, you need to "walk" the XML Element tree down from the absolute Root item for every sub-item that you add.

              Since I can click anywhere in a doc, right click and "Select in structure", shouldn't there be one more way besides Root-based and XML Rules? Ie a direct way to:

              a) use search or any other function to get to a particular place in a document

              b) start in the DOM at a "character pointer" (not sure what to call it -- the object used for range start/end) and get to the associated XML object?

               

              I'm assuming this would have something to do with the associatedXMLElements property... but again I've seen people have trouble with this.

               

              I suspect you've given me almost enough to accelerate my own understanding. Thanks!

               

              Message was edited by: SLTyPete to add a third question

              • 4. Re: Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?
                [Jongware] Most Valuable Participant

                > Why is this not

                xmlElements[0].add

                ...and how did you know to stick in the extra xmlElements reference?

                 

                Because that gives an error :) Stuff like this I don't do from memory, I just try it out and inspect the result. You get an error because xmlElements[0] is the root, and you cannot add more elements to the root. As Connor Macleod said, there can be only one.

                 

                > (and similar throughout). Do I understand that these could also have been written as

                .xmlTags["par"]

                 

                It is proper Javascript syntax, but it does something else. The syntax you propose is to access properties of the item, whereas "item('x')" is actually a *function* that scans the list of items.

                 

                On your third question, yes, it's associatedXmlElements that you should be looking in to, but I cannot locate a good example on a short notice.

                • 5. Re: Noob help :) -- How to create XML tag w/ attrib's, how apply tag to selected para?
                  SLTyPete Level 1

                  Interesting! You talk about Root as if there's only one in a document. Which makes sense to me. But that doesn't match the reality I see.

                   

                  If I simply map styles to tags in a document that contains table(s)... then seemingly at random some cells still are structured as <Cell>->(potential sub-tags) while others have become <Root>->(potential sub-tags). I don't see a pattern:

                  * Some empty cells are <Cell> others are <Root>

                  * Some top-of-tree cells are <Cell> at the top, others are <Root>

                   

                  I didn't do anything fancy to get here.

                   

                  I assume somebody expects that and it is not a bug?