6 Replies Latest reply on Oct 24, 2011 11:56 AM by KyranOB

    Inserting words to inDesign via XML STRUCTURE


      In the attached script I am walking through the XML structure and finding all instances of the "WebID" xml element. I am then trying to Add the words "Web ID" to the existing number that is in this element and then change the character style. Finally closing the structure window.


      I am successful at the step through, selection, the change of the paragraph style and closing the window but any code I try to add the words "Web ID' is unsuccessful. Usually I get the not permitted error.
      Any help would be greatly appreciated.




      tell application "Adobe InDesign CS4"
          tell active document
              set theRoot to (item 1 of XML elements)
          end tell
      end tell


      property myLoLoV : {}
      property myHoHoV : {}
      on myLoopLoop(myElement)
          tell application "Adobe InDesign CS4"
              tell active document
                  set moreElement to every XML element of myElement
                  repeat with x from 1 to (count of moreElement)
                      set em1 to item x of moreElement
                      select em1
                      if {name of markup tag of em1 starts with "WebID"} then
                          set em2 to em1
                          set properties of view preferences to {horizontal measurement units:inches, vertical measurement units:inches, ruler origin:page origin}
                          set transform reference point of layout window 1 to top left anchor
                          select em2
                          tell application "Adobe InDesign CS4"
                              add word "WEB ID"
                          end tell
                          tell application "Adobe InDesign CS4"
                              tell document 1
                                  set theStyle to character style "Bold lead-in/sale price"
                                  set applied character style of text of selection to theStyle
                              end tell
                          end tell
                      end if
                      tell me to myLoopLoop(item x of moreElement)
                  end repeat
              end tell
          end tell
      end myLoopLoop
      tell application "Adobe InDesign CS4"
          set myDocument to document 1
          tell myDocument
              tell XML view preferences
                  if show structure is equal to true then
                      set show structure to false
                      set show structure to true
                  end if
              end tell
          end tell
      end tell
      on «event coVScliI» theObject
          (*Add your script here.*)
      end «event coVScliI»

        • 1. Re: Inserting words to inDesign via XML STRUCTURE
          Cari Jansen Adobe Community Professional

          forgive me for wondering... but wouldn't it be easier/faster to make that change by applying an XSL on XML import?

          Loic Agon in a recent blog-post actually covers just that... http://www.loicaigon.com/blog/?p=1014

          • 2. Re: Inserting words to inDesign via XML STRUCTURE
            absqua Level 4

            Here is some JavaScript code that will do what you want:


            #include "/Applications/Adobe InDesign CS5/Scripts/XML Rules/glue code.jsx";
            function WebID(){
                this.name = "WebID";
                this.xpath = "//WebID";
                this.apply = function(element, ruleProcessor){
                    element.insertionPoints[-1].contents = " WEB ID";
                    return true;
            function main(){
                __processRuleSet(doc.xmlElements[0], [new WebID]);
            var doc = app.activeDocument;
            var charStyle = doc.characterStyleGroups.item("Bold lead-in").characterStyles.item("sale price");
            doc.xmlViewPreferences.showStructure = false;
            app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Update WebID");


            I struggle so much with Applescript that I didn't even try it that way. I'm sorry if using Applescript is part of your requirements.


            This uses xml rules, which walk the xml structure for you, and, in my experience, do it much, much faster than looping through it yourself. (You might need to modify the #include line if you're using a different version or a non-standard install location.)


            I'm not sure why you're selecting each element in turn, or why you're changing the measurement units or ruler origins. Selecting things is almost never necessary unless you're building a GUI script that interacts with the user's selection. Maybe this is part of a larger script that relies on the user to do certain things?


            Hope this helps.



            • 3. Re: Inserting words to inDesign via XML STRUCTURE
              KyranOB Level 1

              Jeff thanks so much. This is easier. I just know Applescript so much better than Java.

              This is what I needed. I will next have to figure out manipulating images via XML using Jave instead of applescript.

              Thanks Again.

              • 4. Re: Inserting words to inDesign via XML STRUCTURE
                KyranOB Level 1

                No it wouldn't help.

                We are using a propritory XML genreated from a merchant applicaton system that needs to be updated for pricing so manipulating the XML before input is out of the question.

                Thanks for your help

                • 5. Re: Inserting words to inDesign via XML STRUCTURE
                  absqua Level 4

                  I'm glad to help.


                  To be clear about a couple of things: Java and JavaScript are two entirely different, unrelated languages. And Cari's suggestion of transfoming the xml via xslt would not necessarily be ruled out by the scenario you describe: you can apply the transformation to the xml "on the fly," as it is imported into InDesign, leaving the original xml intact. That may still be a better (and certainly better-performing) option if you have to make a lot of these kinds of changes.


                  For more about xml rules in InDesign, see chapter 13 of the scripting guide.

                  • 6. Re: Inserting words to inDesign via XML STRUCTURE
                    KyranOB Level 1

                    Yes I know Java and Javascript are differant, just typing shorthand, sorry.

                    I would love to learn more about XSL. It sounds very interesting. Just not sure of the time/learning curve invovled.

                    Thanks again