7 Replies Latest reply on Mar 16, 2011 4:14 AM by das_kaese

    Identify document elements, mark as XML?

    das_kaese Level 1

      Hi all,

       

      Currently working on a Web-to-print project using InDesign Server CS5 and JavaScript.

       

      What I'd like to do is enable the following to happen:

       

      • A user uploads an .indd document (OK with this!).

       

      • A JS script loops through the document and identifies all the elements of the document (text areas, images, etc).

       

      • The user is then prompted to select which elements of the document they would like to mark as 'editable'.

       

      • The elements that the user has selected as editable are tagged in the .indd as XML elements.

       

      • The .indd is saved and closed.

       

      I'm relatively new to InDesign scripting, and I'm hoping this isn't something too unusual that I'm trying to achieve! Does anyone have any advice/ideas as to how this could be achieved?

       

      Many thanks

        • 1. Re: Identify document elements, mark as XML?
          John Hawkinson Level 5

          Good news: This is all doable.

          Bad news: you'll have to do much of it yourself.

           

          Looping through th document is easy enough. You can iterate over pages and within pages iterate over pageItems or allPageItems depending on your view of the world. Or you could iterate over all the textFrames in your document. Or over all the pageItems in your document. All depends on how you want to organize it. Each is a 1-line for loop...easy.

           


          Having the user select...that sounds like a user interface. That could be a huge pain. Those are doable in JavaScript/ScriptUI, but they require a fair bit of experience and even then there's a lot of fiddling and adjustment...

           

          Unless maybe you mean having the script stop and having the user shift-click all the items in InDesign, and then restart the script? That's doable but I don't think users would like it.

           

          Once you have a set of elements identified to the script, tagging them is easy enough as well.

           


          Which part do you need help with, specifically?Doing them all at once is a bit much to bite off and chew.

          • 2. Re: Identify document elements, mark as XML?
            das_kaese Level 1

            Hi John,

             

            Thanks for getting back to me.

            I suppose there are two key areas that I'd like help in.

             

            Firstly, the foreach loop to identify pageItems or allPageItems. Ideally something like:

             

            foreach (pageElements as pageElement) {

              array_push(pageElement into elementArrayForUI);

            }

             

            Secondly, tagging the selected elements as XML elements once 'selected' by the user. Ideally something like:

             

            foreach (selectedElementsFromUI as selectedElement) {

               tagSelectedElementAsXML(selectedElement, XMLTagName);

            }

             

            And then I can sort out the UI 'conundrum' independently of these problems.

             

            Thanks again,

             

            Ben

            • 3. Re: Identify document elements, mark as XML?
              John Hawkinson Level 5

              Umm, Ben, have you looked at the InDesign Scripting Guide? This is pretty basic stuff and iterating over objects happens all the time -- all over Chapter Two, etc.

               

              Please do take a look at it. For instance, the example on page 17 iterates over page items.

               

              But in any case, here's how you would loop over allPageItems in JavaScript:

               

              var
                i,
                doc = app.activeDocument,
                pageElements=doc.allPageElements,
                pageElement,
                elementArrayForUI = [];
              
              for (i=0; i<=pageElements.length; i++) {
                pageElement = pageElements[i];
                elementArrayForUI.push(pageElement);
              }
              
              1 person found this helpful
              • 4. Re: Identify document elements, mark as XML?
                das_kaese Level 1

                Thanks for the advice John and I will take a detailed look at the InDesign scripting guide.

                 

                My main concern now is how to mark up existing page elements as XML. I'm assuming something like the below would work?

                 

                for (i=0; i<=pageElements.length; i++) {
                     var XMLTag = myDocument.xmlTags.add("XML_tag_" + i);
                     myDocument.XMLTag.markup(pageElements[i]);
                }
                    

                 

                Many thanks,

                 

                Ben

                • 5. Re: Identify document elements, mark as XML?
                  John Hawkinson Level 5

                  Well, yes, I think so (I don't have the XML syntax memorized...) but...that's not typically how you would want to tag your XML elements.

                  I mean, generally you'd want something like:

                   

                  <rect count="1">...</rect>
                  <rect count="2">...</rect>
                  <text count="3">...</text>
                  

                   

                  if you wanted counts. But you wouldn't create new tags for every object:

                   

                  <tag1>...my rectangel data</tag1>
                  <tag2>...rect2 data</tag2>
                  <tag3>...text data...</tag3>
                  

                   

                  It's certainly going to make it hard to parse your XML with standards tools. Instead use an attribute. Or why even make them unique like that? I'm not sure t's The XML Way?

                   

                  Anyhow, sure looks like you've found your footing.

                  • 6. Re: Identify document elements, mark as XML?
                    John Hawkinson Level 5

                    Oh, yes, a few points:

                     

                    1. You'd probably want to loop over your newly created elementArrayForUI instead of pageElements.

                    2. Javascript doesn't have block scope, so despite the appearance that "var XMLTag" is local to the for loop, it's not. variables are scoped only to the function they are in (in this case the whole program). That's why recommended style for JavaScript is to declare variables at the tops of functions, rather than at the point of need, as you might in many other languages that permit such.

                    3. I believe xmlTags.add() will throw an error if the tag already exists. You might want to check for that case, or wrap it in a try/catch block to cartch the exception.

                    4. Typo [or brain error?] in my example, and you copied it. The loop test should be for i<ARRAYNAME.length, not i<=ARRAYNAME.length, because arrays in javascript are zero-indexed. Not really sure how I did that. Maybe means it's time for bed.

                    1 person found this helpful
                    • 7. Re: Identify document elements, mark as XML?
                      das_kaese Level 1

                      Thanks John.

                       

                      Certainly am finding my feet with this! Thanks for your help, advice etc. Will keep you updated with how it goes.

                       

                      Ben