3 Replies Latest reply on Dec 26, 2009 1:59 AM by AdobeScripts

    Tag multiple Items at Once?




      I'm trying to create separate XML tags for a list of items in a InDesign file without having to tag each one individually.  Each item is in is own paragraph.  Is there a way for me to select all of the items and tag them all at once?


      When I try this, all the items end up in one <bullet> tag:

      <bullet>Item 1 Item 2 Item 3</bullet>


      ...instead of:


      <bullet>Item 1</bullet>

      <bullet>Item 2</bullet>

      <bullet>Item 3</bullet>


      I apologize if this has been answered elsewhere, but I could not find a solution.





        • 1. Re: Tag multiple Items at Once?
          AdobeScripts Level 3

          Is there a way for me to select all of the items and tag them all at once?






          • 2. Re: Tag multiple Items at Once?
            Tom Tomasko Level 1

            Yes, you can tag multiple items at once.


            What you first need to do is to create an array of the objects. Then you can use the second function below to iterate thru that array, tagging as it goes along.


            The script here finds orphans, that is, last lines of a paragraph that are 4 or less characters in length, a typographical no-no. The function xmlTagIt() then goes thru the array of orphans and tags each one. The function also adds an attribute to the XML tag that denote the page number.




            var myDoc = app.activeDocument;
            var mainBodyFrames =findMainBodyFrames();


            var orphanLines = new Array;
            for(var j = 0; mainBodyFrames.length > j; j++){
                for(var k = 0; mainBodyFrames[j].paragraphs.everyItem().lines.lastItem().length.length
            > k;k++){
                        }//end  if
                    }//end k
                }//end for j
            if(orphanLines.length == 0){
                alert("Great news! There are no orphaned lines in this  document");
                }//end if
            else{//that is, if array length above is zero
                var userResponse = confirm("There are "+orphanLines.length+" instance(s) of orphaned lines i.e., last lines of paragraphs that have less than four characters. \n\nClick \"Yes\" and I will tag these lines.\n\nClick \"No\"\ to do nothing.",false);
                }//end else


            if(userResponse == true){
                }//end if


            function findMainBodyFrames(){
            //function makes an array of the main text body text frames.
            var myDoc = app.activeDocument;
            //arrStories1 is array of all stories whose elements are text frames
            var arrStories1 = myDoc.stories.everyItem().textContainers;
            //arrStories2 is array of all stories with more than one text frame. This assumes these  are main body text
            var arrStories2 = new Array;
            for(var i = 0; arrStories1.length > i; i++){
                    if(arrStories1[i].length > 1){
                        }//end if
                }//end for  i
            //mainBodyFrames is an array of all main body text frames
            var mainBodyFrames = new Array;
            for(var x = 0; arrStories2.length > x; x++){
                for(var y = 0; arrStories2[x].length>y; y++){
                    }//end for y
                }//end for x
            return mainBodyFrames;
            }//end findMainBodyFrames


            function xmlTagIt(myDoc, tagName,UIColors, arrToTag){
                myDoc.xmlViewPreferences.showStructure = false;
                myDoc.xmlViewPreferences.showTaggedFrames =true;
                myDoc.xmlViewPreferences.showTagMarkers = true;
                myDoc.xmlViewPreferences.showTextSnippets =false;
                var rootElement = myDoc.xmlElements.item(0);
                if (myDoc.xmlTags.item(tagName) == null) {
                    var tagRef = myDoc.xmlTags.add(tagName, UIColors);
               } else {
                    myDoc.xmlViewPreferences.showStructure = true;
                    alert("You have already run this script. I will not double tag the orphaned lines.");
                }//end else
                for (var i = 0; i < arrToTag.length; i++){
                    try{ //this needed if tags already exist
                        var elementRef = rootElement.xmlElements.add (tagRef,arrToTag[i].lines.lastItem());
                        var pageNumber = arrToTag[i].parentTextFrames[0].parent.name;
                  } catch (e) {/*do nothing*/}
                 }//end for     i
                myDoc.xmlViewPreferences.showStructure = true;
            }//end function xmlTagElement

            • 3. Re: Tag multiple Items at Once?
              AdobeScripts Level 3

              Tom Tomasko wrote:


              Yes, you can tag multiple items at once.

              The question was "without having to tag each one individually"


              And whole procedure could be shortened to one For...Next going thru Paragraphs collection of current selection.


              Something like this (VB):


              set mySel = myInDi.Selection.item(1).Texts.Item(1)

              set myParentXML = mySel.AssociatedXmlElements.Item(1)

              For a=1 to mySel.Paragraphs.Count

                 call myParentXML.XMLElements.add("bullet",mySel.Paragraphs.Item(a).Texts.Item(1))



              I can't check this now so maybe it should go backward.