1 Reply Latest reply on Jun 27, 2008 12:00 PM by (Olav_Kvern)

    Tag a single word?


      I have some words in a text frame and I would like to tag only the second word with an XML tag.
      In the UI I choose Edit with Story Editor, select the word and Tag it.

      How can I achieve the same result with scripting? Thank you

        • 1. Re: Tag a single word?
          Level 1
          Hi Pippo,

          There are really two ways to do it--if the XML element you want to associate with the text already exists, you use:

          //Where "myXMLElement" is the XML element and "myText" is the text...

          But if the XML element does not already exist, you can create the XML element and associate the text with it in a single action. Here's a longer example:

          function main(){
          function mySetup(){
          var myDocument = app.documents.add();
          var myStoryTag = myDocument.xmlTags.add("Story");
          var myTextTag = myDocument.xmlTags.add("Text");
          var myStoryElement = myDocument.xmlElements.item(0).xmlElements.add(myStoryTag);
          var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myDocument, myDocument.pages.item(0))});
          myTextFrame.parentStory.contents = "This XMLElement should be added to the structure.";
          function mySnippet(){
          var myDocument = app.documents.item(0);
          var myStory = myDocument.stories.item(0);
          var myWord = myStory.paragraphs.item(0).words.item(1);
          myWord.associatedXMLElements[0].xmlElements.add(myDocument.xmlTags.item("Text"), myWord);

          function myGetBounds(myDocument, myPage){
          var myPageWidth = myDocument.documentPreferences.pageWidth;
          var myPageHeight = myDocument.documentPreferences.pageHeight
          if(myPage.side == PageSideOptions.leftHand){
            var myX2 = myPage.marginPreferences.left;
            var myX1 = myPage.marginPreferences.right;
            var myX1 = myPage.marginPreferences.left;
            var myX2 = myPage.marginPreferences.right;
          var myY1 = myPage.marginPreferences.top;
          var myX2 = myPageWidth - myX2;
          var myY2 = myPageHeight - myPage.marginPreferences.bottom;
          return [myY1, myX1, myY2, myX2];

          The stuff in "mySnippet" is the part that's most important; the rest of it is just setting up the context.