8 Replies Latest reply on Jan 15, 2010 6:31 AM by AdobeScripts

    Can anybody translate this one-line Visual Basic into AppleScript?

    jeffkr

      I have been pulling my hair out in trying to get InDesign CS4 to markup selected text with an XML tag. Supposedly this can be done using Visual Basic. Is there anybody out there that could possibly translate this seemingly simply code into AppleScript?

       

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

      Here is another example:

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

       

      I am still thinking that AS cannot do this… but it is worth a shot to ask.

       

      Thanks in advance,

      Jeff

        • 1. Re: Can anybody translate this one-line Visual Basic into AppleScript?
          Harbs. Level 6

          To convert to JS, just change capitals to lowercase and subtract 1 

          from the indexes:

           

          myText.associatedXMLElements.item(0).xmlElements.add("break", myText)

           

          Harbs

          1 person found this helpful
          • 2. Re: Can anybody translate this one-line Visual Basic into AppleScript?
            Harbs. Level 6

            jeffkr wrote:

             

            I am still thinking that AS cannot do this… but it is worth a shot to ask.


            The entire DOM is completely supported by all three scripting languages. I'm most comfortable in JS, but this can be done in VB, JS and AS.

             

            Harbs

            1 person found this helpful
            • 3. Re: Can anybody translate this one-line Visual Basic into AppleScript?
              jeffkr Level 1

              Thank you Harbs. I assume this will work, however I am not sure how to compile this into a working js. I thought what I have below might work, but I am receiving an error, after I save it and activate it from the "scripts" panel . I hate to beg, but can you tell me what I did wrong?

               

              {

               

              var myText = app.selection[0].contents;

               

              myText.associatedXMLElements.item(0).xmlElements.add("break", myText);

               

              }
              • 4. Re: Can anybody translate this one-line Visual Basic into AppleScript?
                [Jongware] Most Valuable Participant

                var myText = app.selection[0].contents;

                 

                This returns the selection contents -- a regular Javascript string of ASCII characters [*]. xmlElements can only be applied to InDesign text, which is a heavily formatted object. Fortunately, this is what "app.selection[0]" returns when you have selected some text. All you need is

                 

                var myText = app.selection[0];

                 

                 

                ---

                [*] Not always, though. If you accidentally (or on purpose!) selected just one special character, such as a Hard Page Break, it will be returned as a SpecialCharacter enumerated value. So don't do this. Or have your code check for this special case.

                • 5. Re: Can anybody translate this one-line Visual Basic into AppleScript?
                  jeffkr Level 1

                  Jongware, thank you for the info. This may cause an issue, but it will be rare. However, I am still receiving an error. But let me better explain what I am selecting. I create a text frame. Then inside the frame I type: "This word is the item I want to tag". Next I use the Type tool and select the item "word" in the sentence. I then run the script.

                   

                  {
                  var myText = app.selection[0];
                  myText.associatedXMLElement.item(0).xmlElement.add("break", myText);
                  }

                   

                  By running the script above I am receiving:

                   

                  Error Number:55
                  Error String: Object does not support the property or method 'associatedXMLElement'

                   

                  Line:3
                  Source:
                  myText.associatedXMLElement.item(0).xmlElement.add("break, myText);

                  • 6. Re: Can anybody translate this one-line Visual Basic into AppleScript?
                    jeffkr Level 1

                    If it makes any difference

                    This is what I have so far using AppleScript. It is very very close. But the result of running the script 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

                    • 7. Re: Can anybody translate this one-line Visual Basic into AppleScript?
                      jeffkr Level 1

                      My appologies, I did not see this post earlier, dispite searching?

                       

                      http://forums.adobe.com/message/2349302#2349302

                       

                      This code works with js:

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

                       

                      However, it sure would be nice to have an AppleScript version.

                       

                      Many thanks.

                      -Jeff

                      • 8. Re: Can anybody translate this one-line Visual Basic into AppleScript?
                        AdobeScripts Level 3

                        jeffkr wrote:

                         

                        By running the script above I am receiving:

                         

                        Error Number:55
                        Error String: Object does not support the property or method 'associatedXMLElement'

                         

                        Line:3
                        Source:
                        myText.associatedXMLElement.item(0).xmlElement.add("break, myText);

                        the "s" is missing:

                        myText.associatedXMLElements.item(0).xmlElement.add("break, myText);

                        robin

                        www.adobescripts.co.uk