8 Replies Latest reply on Jun 28, 2008 11:17 PM by sotospeak (carlo bazzo)

    strange thing using markup()

    sotospeak (carlo bazzo)
      I make a search with grep:<br /><br />app.findGrepPreferences.findWhat = "(\\[)(.*)(\\])";<br />var myFoundItems = app.documents.item(0).findGrep();<br /><br />Now I want to markup any found item:<br /><br />for (myCounter=0;myCounter<myFoundItems.length; myCounter++) {<br /><br />myword=myFoundItems[myCounter];<br /><br />var lastindex=myword.contents.length-1;<br /><br />var myXMLTag=mydocument.xmlTags.add(myword.contents.substring(1,lastindex));<br /><br />//this is just to trim square brackets<br /><br />var myXMLElementA = <br />mydocument.xmlElements.item(0).xmlElements.add(myXMLTag);<br /><br />myXMLElementA.markup(myword);<br /><br />Well in one document I prepared for testing this is working fine. <br /><br />In my working document the following line:<br /><br />myXMLElementA.markup(myword);<br /><br />fails at once (one Tag+Elelment created but no markup) with error:<br />Error number 49079<br />Error String: Cannot change the element to be associated with this object. Try creating a new element.<br /><br />My only clue is that the error must be related to this:<br />1) In my test document<br /><br />myword=myFoundItems[myCounter];<br />alert(myword);<br /><br />gives me this is a Paragraph Object<br /><br />2) In my working document<br />myword=myFoundItems[myCounter];<br />alert(myword);<br /><br />gives me this is a Text Object.<br /><br />This should not be the key as I see from the Object Model you can markup both Paragraph and Texts, but this is the only difference I was able to find.<br /><br />Thanks for any help<br />carlo
        • 1. Re: strange thing using markup()
          srakete Level 1

          what are your findGrepPreferences Settings. Maybe you are finding something that is read-only. e.g. on a locked layer, locked story, something like this.

          • 2. Re: strange thing using markup()
            sotospeak (carlo bazzo) Level 1
            Thank you Stefan. My preferences:
            app.findChangeGrepOptions.includeFootnotes = true;
            app.findChangeGrepOptions.includeHiddenLayers = true;
            app.findChangeGrepOptions.includeLockedLayersForFind = true;
            app.findChangeGrepOptions.includeLockedStoriesForFind = true;
            app.findChangeGrepOptions.includeMasterPages = true;

            but no results. It seems you can not markup text which is absurd as I can manually select the same text and mark it up with no problem.
            • 3. Re: strange thing using markup()
              Level 1
              Hi Carlo,

              Actually, if you're creating the XML element at the same time, you don't need to use markup()--you can just add the text object as a parameter to xmlElements.add(). Here's an example:

              //An InDesign CS3 JavaScript
              function main(){
              function mySetup(){
              var myDocument = app.documents.add();
              var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myDocument, myDocument.pages.item(0))});
              myTextFrame.parentStory.contents = "This is a [textTagA] in an XML story.\rThis is the second [textTagA] in the story.\rThis is the first [textTagB] in a text frame.\rThis is the second [textTagB] in the story.\rThis is the third [textTagB] the story.\r";
              function mySnippet(){
              var myFoundItem, myText, myString, myError;
              var myDocument = app.documents.item(0);
              var myXMLTag = myDocument.xmlTags.add("Story");
              var myStoryElement = myDocument.xmlElements.item(0).xmlElements.add(myXMLTag)
              app.findGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences.findWhat = "(\\[)(.+?)(\\])";
              //Get the references (if any) in reverse order to avoid invalid references.
              var myFoundItems = myDocument.findGrep(true);
              if(myFoundItems.length != 0){
                for(var myCounter = 0; myCounter < myFoundItems.length; myCounter++){
                 myFoundItem = myFoundItems[myCounter];
                 myText = myFoundItem.texts.itemByRange(myFoundItem.characters.item(1),myFoundItem.characters.item(-2));
                 myString = myText.contents.toString();
                 //Create the xml tag if it does not already exist.
                  myXMLTag = myDocument.xmlTags.item(myString);
                 catch (myError){
                  myXMLTag = myDocument.xmlTags.add(myString);
                 //Create an XML element inside the story element.
                 myXMLElement = myStoryElement.xmlElements.add(myXMLTag, myText);
              app.findGrepPreferences = NothingEnum.nothing;

              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];


              • 4. Re: strange thing using markup()
                sotospeak (carlo bazzo) Level 1
                First of all, thanks Ole for you help. I found your post very
                interesting because points out some things I have
                never seen in the docs.

                Allow me some comments, I hope you can reply:

                >>var myXMLTag = myDocument.xmlTags.add("Story");

                >>var myStoryElement =

                >>myDocument.xmlElements.item(0).xmlElements.add(myXMLTag) ;


                Here you Tag the parent Story: is this compulsory?
                In my tests I think not.

                >>myFoundItem = myFoundItems[myCounter];

                >>myText =

                >>myFoundItem.texts.itemByRange(myFoundItem.characters.item(1),myFoundItem.characters.item (-2));

                >>myString = myText.contents.toString();

                This is the magical part. Why dont'y you put directly:


                isn't myFoundItems an array of text? I supposed it must contain:
                etc etc

                I have to try but I am sure my error was due to this:
                with reference to your example I was trying to markup myFoundItem
                and not myText.

                Do you agree?

                >>//Create the xml tag if it does not already exist.


                >>myXMLTag = myDocument.xmlTags.item(myString);

                >>myXMLTag.name; }

                Could you please explain the purpose of myXMLTag.name ?

                >>//Create an XML element inside the story element.

                >>myXMLElement = myStoryElement.xmlElements.add(myXMLTag, myText);

                Why do you prefer this alternative approach here and
                not the previous one:

                >>var myStoryElement =

                >>myDocument.xmlElements.item(0).xmlElements.add(myXMLTag) ;


                Is it just didactical or is it advisable ?

                Thank you,

                carlo bazzo
                • 5. Re: strange thing using markup()
                  sotospeak (carlo bazzo) Level 1
                  Hi Ole,
                  in addition to my above questions I tried your code but it is not working the way I expected. Please note that I executed the script without the part of your code which creates myStoryElement.
                  My test document is 5 textframes which contain this text:

                  This is the xml file your code created:


                  What I would like to get is:


                  The Tag contents is not important actually: what it matters is the Tag name equal to the square brackets content. My purpose is to  subsequently load an XML file like this:


                  so that my texts [AAA], [BBB] etc are automatically populated.

                  Thank you
                  • 6. Re: strange thing using markup()
                    sotospeak (carlo bazzo) Level 1
                    Sorry Ole,

                    despite my last post, your code, though the xml file has not the structure I expected, works perfectly and populates all my fields.

                    The only thing I will have to add an element Story to my xml file but as I create it myself with a Python script from an Excel spreadsheet this is not a real problem. Thanks again.

                    I remain with some doubts probably due to my ID's lack of competence:

                    -why is it created a Story tag?
                    -I tried to create in a cycle first all the XmlElements and then in another cycle to markup my text: even this way I get the error reported in my first post. It is not clear to me when I can use the markup method and when I must use your syntax
                    myXMLElement = myStoryElement.xmlElements.add(myXMLTag, myText);

                    • 7. Re: strange thing using markup()
                      Level 1
                      Hi Carlo,

                      I'll have to make time for a more detailed response later--but the quick response is: use the markup method when the XML element already exists, and use the add method when the XML element does not already exist.

                      I created the Story element to give the XML elements (the ones we create from the found text items) a parent element. You can't just tag the text without having a parent element. When you do so in the user interface, InDesign creates a parent element for you. Scripting might also do it--I'm not sure--but we have more control with the approach I took here.

                      If the story is already associated with an XML element, you can get a reference to that element using something like this:

                      //Given a reference to some text "myFoundItem"
                      var myParentElement = myFoundItem.associatedXMLElements[0];


                      • 8. Re: strange thing using markup()
                        sotospeak (carlo bazzo) Level 1
                        Hi Ole,

                        >>You can't just tag the text without having a parent element.
                        >>When you do so in the user interface, InDesign creates a parent
                        >>element for you. Scripting might also do it--I'm not sure-

                        I can more or less confirm this: I skipped the first part of your code when you create the Story Element but a Story element is nontheless created automatically the way I showed in one of previous post.

                        To be more precise: if your text (to be tagged) is the only element in a text frame (I mean also without any spaces before or after), the Story element is not created. For example:
                        I had [AAA] as the only text in a textframe: as your code tagged only AAA then a story element was created. When I changed the code in order to tag [AAA] the story element was created no more and the tagged text is appended down the Root element.

                        This way you have no control on this parent tag which is always called "Story".

                        As you suggested, creating one Story element gives you more control.

                        Thank you