13 Replies Latest reply on Jan 15, 2010 3:57 AM by AdobeScripts

    [CS3 AS] Tagging text (xml)

    Level 1
      Does anyone have any tricks to tag some text objects? I don't fully understand all the intricacies of xml items. I need to apply a tag to a text string in several text frames. The text is already there, and I want to tag it as an xml item, for exporting later. I can make the tag before hand, in fact I'd like to make the tag before hand, and be able to use the same tag on several objects.

      So for example we have a date "2-14-09" and want to tag it as "theDate". I've looked and XML suite and I'm seeing a "markup" command. If I have a tag "theDate" how would I apply that to some text in a frame?
      If I do it manually I can select some text, and select the tag from the pallet, and it puts a default "Story" object inside root.

      Thanks,
      Chris

      Here's a script I'm trying without success:
      tell application "Adobe InDesign CS3"
      tell document 1

      set myRootXMLElement to XML element 1
      set myXMLTag to (get XML tag "theDate")
      set myXMLStoryTag to (get XML tag "Story")
      tell myRootXMLElement
      set theStoryElement to make XML element with properties {markup tag:myXMLStoryTag}
      tell theStoryElement
      set myXMLElement to make XML element with properties {markup tag:myXMLTag}
      end tell
      end tell

      tell text frame 1
      tell word 2
      markup using myXMLElement

      end tell
      end tell
      end tell
      end tell
        • 1. Re: [CS3 AS] Tagging text (xml)
          Level 1
          Please help. Hasn't anyone ever tried this or had any success?
          I can tag a story but I can't get down to tagging a particular word in a story.

          semi working code:
          tell application "Adobe InDesign CS3"
          tell document 1

          set myRootXMLElement to XML element 1
          set myXMLTag to (get XML tag "theDate")
          set myXMLStoryTag to (get XML tag "Story")
          tell XML element 1
          set theStoryElement to make XML element with properties {markup tag:myXMLStoryTag}
          tell theStoryElement
          set myXMLElement to make XML element with properties {markup tag:myXMLTag}
          end tell
          end tell

          tell story 1 to markup using theStoryElement
          --breaks on next line:
          tell (word 2 of story 1) to markup using myXMLElement
          -->Adobe InDesign CS3 got an error: Invalid value for parameter 'using' of event 'markup'. Expected XML element, but received nothing.
          end tell
          end tell
          • 2. Re: [CS3 AS] Tagging text (xml)
            jeffkr Level 1

            Hello SuperMacGuy,

            I am wondering if you figured out how to accomplish this. I have been struggling over the same thing for soooo long. This was extremely simple with CS2, but with CS4 it is a nightmare… and I don't even know if it is possible?

             

            Any help would be immensely appreciated.

             

            -Jeff

            • 3. Re: [CS3 AS] Tagging text (xml)
              AdobeScripts Level 3

              hi

               

              from VB6 ObjectBrowser:

               

              Function Add(MarkupTag, [XMLContent]) As XMLElement
                  Member of InDesign.XMLElements
                  Creates a new XML element. / Return value: The new element. / MarkupTag: The XML tag used to identify the element. as String or XMLTag / XMLContent: The content to be marked up. as Text, Story, PageItem, Movie, Sound, Graphic, Table or Cell

               

              I don't know AS so I can show you only example in VB:

               

              myText.AssociatedXMLElements.Item(1).XMLElements.Add("break", myText1)

               

              this will tag your myText with break tag.

               

              robin

              www.adobescripts.co.uk

              • 4. Re: [CS3 AS] Tagging text (xml)
                jeffkr Level 1

                Thank you very much Robert for the response. I know nothing about Visual Basic, but that's okay. I am very interested to see that this can be scripted.

                 

                Just to be perfectly clear, did you test this in CS4? Likewise the result will yield the highlighted text of a story with surronding brackets. And the xml structure should appear with a "Story" tag and in this instance a "break" tag as its subelement?

                 

                A common misconception is that one seeks to tag the entire frame, that I can do with AS in CS4, but I cannot make it tag specific higlighted text the way it once did using CS2.

                • 5. Re: [CS3 AS] Tagging text (xml)
                  AdobeScripts Level 3

                  jeffkr wrote:

                   

                  Just to be perfectly clear, did you test this in CS4? Likewise the result will yield the highlighted text of a story with surronding brackets. And the xml structure should appear with a "Story" tag and in this instance a "break" tag as its subelement?

                  Yes - I have tested this only in ID CS3 and ID CS4 - but for ID CS2 description of .Add lokks the same.

                   

                  There is small error in my example:

                   

                  myText.AssociatedXMLElements.Item(1).XMLElements.Add("break", myText1)

                   

                  The last myText1 should be myText - without "1" at the end.

                   

                  A common misconception is that one seeks to tag the entire frame, that I can do with AS in CS4, but I cannot make it tag specific higlighted text the way it once did using CS2.

                  No - this example will mark only selected text with selected tag.

                   

                  Here is another example:

                   

                  With myTSRangeText.AssociatedXMLElements.Item(1).Parent.XMLElements.Add("znak")
                    Call .Markup(myTSRangeText)
                  End With

                   

                  First you're adding tag znak to parent tag of myTSRangeText, then you .Markup myTSRangeText "to" this tag.

                   

                  robin

                  www.adobescripts.co.uk

                  • 6. Re: [CS3 AS] Tagging text (xml)
                    jeffkr Level 1

                    Robin, this is so very helpful! Simply knowing that it can be done with another scripting language is refreshing . I was getting close with AS, but the best I could do was to have it add the tagged selected text to the end of the story, rather than tag the inline highlighted text itself. I wish there was some Applescript guru that could translate this? However maybe I can call the Visual Basic script from AS?

                     

                    Do you know anybody that could translate this in AS?

                     

                    Regardless of my last question, thank you very much.

                     

                    -Jeff

                    • 7. Re: [CS3 AS] Tagging text (xml)
                      AdobeScripts Level 3

                      jeffkr wrote:

                       

                      However maybe I can call the Visual Basic script from AS?

                       

                      Do you know anybody that could translate this in AS?

                      You can't call VB from AS - only JS from VB.

                       

                      It's almost 23:00 in europe and 17:00 in New York You need to wait few hours Or few minutes

                       

                      robin

                      www.adobescripts.com

                      • 8. Re: [CS3 AS] Tagging text (xml)
                        jeffkr Level 1

                        That's what I figured about the calling of VB

                        I can wait if you or others are willing to help more. I just assumed this thread was closed. But my God, if I was able to get this to work using AS and indd CS4, I would be EXTREMELY happy!

                        • 9. Re: [CS3 AS] Tagging text (xml)
                          sstanleyau Level 4

                          If I had to guess, and that's all it is, I'd suggest that when you make an XML element, you need to specify both the XML tag *and* the XML content. As in "make XML element with properties {markup tag:myXMLTag, XML content:<ref to content>}".

                          • 10. Re: [CS3 AS] Tagging text (xml)
                            jeffkr Level 1

                            Yes, you have to do this, I assume. This javascript

                            code works great:

                             

                            app.activeDocument.xmlElements.item(0).xmlElements.add ("subtitle", app.selection[0]);

                             

                            Sorry, I am not the best at searching this forum. I wish I would have seen this earlier.

                             

                            Nevertheless, my ultimate goal is to make an AppleScript version.

                             

                            I have come so very close but the result of running my script below appends the tagged text to the end of the text in the frame. In other words, it looks like this: (assuming I had the characters "word" highlighted). This is because I can only seem to tell the story to markup using the tag. I cannot tell the selection to markup using the tag?


                            This word is the item I want to tag.[word]

                             

                            I am wanting the result to tag the selected item. So it should look like this:

                             

                            This [word] is the item I want to tag.

                             

                            tell application "Adobe InDesign CS4"
                                tell document 1
                                    set selText to object reference of (text from character 1 to character -1) of selection
                                    set selTextString to selText as string
                                    display dialog selText as string
                                    set parentStoryID to parent story of selection
                                    set myXMLTag to (get XML tag "theDate")
                                    set myXMLStoryTag to (get XML tag "Story")
                                   
                                    tell XML element 1
                                        set theStoryElement to make XML element with properties {markup tag:"Story"}
                                        tell theStoryElement
                                            set myXMLElement to make XML element with properties {markup tag:"theDate", contents:selTextString}
                                            tell parentStoryID to markup using theStoryElement
                                        end tell
                                    end tell
                                end tell
                            end tell

                            • 11. Re: [CS3 AS] Tagging text (xml)
                              sstanleyau Level 4

                              Not "contents:selTextString", but "XML content:selTextString"

                              • 12. Re: [CS3 AS] Tagging text (xml)
                                jeffkr Level 1

                                THAT IS IT!!!!! WOW! You have no idea how grateful

                                I am for you shedding your knowledge on this post!

                                 

                                In case anybody else reads this thread, I have included the final script. This script will markup highlighted/selected text with an XML tag you choose. In my case I am creating an XML markup tag called "expirationdate", since the script will be running through text frames and finding date ranges in various text frames.

                                 

                                tell application "Adobe InDesign CS4"
                                    tell document 1
                                        set selText to object reference of (text from character 1 to character -1) of selection
                                        set parentStoryID to parent story of selection
                                        tell XML element 1
                                            set theStoryElement to make XML element with properties {markup tag:"Story"}
                                            tell theStoryElement
                                                set myXMLElement to make XML element with properties {markup tag:"expirationdate", XML content:selText}
                                                tell parentStoryID to markup using theStoryElement
                                            end tell
                                        end tell
                                    end tell
                                end tell

                                • 13. Re: [CS3 AS] Tagging text (xml)
                                  AdobeScripts Level 3

                                  jeffkr wrote:

                                   

                                  tell application "Adobe InDesign CS4"
                                      tell document 1
                                          set selText to object reference of (text from character 1 to character -1) of selection
                                          set parentStoryID to parent story of selection
                                          tell XML element 1
                                              set theStoryElement to make XML element with properties {markup tag:"Story"}
                                              tell theStoryElement
                                                  set myXMLElement to make XML element with properties {markup tag:"expirationdate", XML content:selText}
                                                  tell parentStoryID to markup using theStoryElement
                                              end tell
                                          end tell
                                      end tell
                                  end tell

                                  In VB6 you don't need to tag Story - when you mark text in untagged Story - this Story is automatically tagged with default tag.

                                   

                                  Or there is another way But this will delete already tagged contents of Stories!!

                                   

                                  You can set rules for MapStylesToXMLTags:

                                   

                                  Call myDoc.XMLExportMaps.Add("char_or_para_style_name", "tag_name")

                                   

                                  and then:

                                   

                                  Call myTempDoc.MapStylesToXMLTags

                                   

                                  InDesign will do "the hard work"

                                   

                                  robin

                                  www.adobescripts.co.uk