11 Replies Latest reply on Jan 13, 2017 8:17 AM by Oriup

    Tagging in InDesign CS5, js

    Yuliaart Level 1

      Hi,

       

      I need to assign tags to objects in InDesign through script. I looked in OMV, but I couldn't find tags. I would assume it would be something like this:

       

      myFrame.tag = "picture1";

       

      I found autoTag, but it does not let me to assign my tag.

       

       

      Your help is highly appreciated.

       

      Yulia

        • 1. Re: Tagging in InDesign CS5, js
          Kasyan Servetsky Level 5
          Main();
          
          function Main() {
              var doc = app.activeDocument;
              var frame = app.selection[0];
              var xmlElement = doc.xmlElements[0].xmlElements.add("picture1");
              frame.markup(xmlElement);
          }
          
          • 2. Re: Tagging in InDesign CS5, js
            Yuliaart Level 1

            Thank you, Kasyan, it worked perfectly.

            • 3. Re: Tagging in InDesign CS5, js
              Oriup Level 1

              Dear Kasyan,

               

              Is it possible to complete document tagged once page wise?

               

              If my object text frame then use para tag and if my object images then use img tag.

               

              Suppose in my document 2 pages then structure look like attachment.

               

              Regards,

              Sumit

              Screen Shot 2016-12-23 at 5.32.05 PM.png

              • 4. Re: Tagging in InDesign CS5, js
                [Jongware] Most Valuable Participant

                You should not have clicked "correct answer" to your own reply!

                • 5. Re: Tagging in InDesign CS5, js
                  Kasyan Servetsky Level 5

                  From what I see on the screenshot, for each page you want to add an "Article" element and add "para" and "img" elements (for each text/image frame on the page) to it, skipping frames which are not on pages (e.g. on the pasteboard). Though I'm not quite sure that this is exactly what you want: can't make out the details on the screenshot.

                  Anyway here's the code illustrating how you can approach the task:

                  var scriptName = "Apply tags page-by-page",
                  doc;
                  
                  PreCheck();
                  
                  //===================================== FUNCTIONS ======================================
                  function Main() {
                      var page, xmlElementArticle, xmlElementPara, xmlElementImg, textFrame, rectangle,
                      pages = doc.pages;
                  
                      for (var i = 0; i < pages.length; i++) {
                          page = pages[i];
                          xmlElementArticle = doc.xmlElements[0].xmlElements.add("Article");
                          
                          for (var j = 0; j < page.textFrames.length; j++) {
                              textFrame = page.textFrames[j];
                              xmlElementPara = xmlElementArticle.xmlElements.add("para"); 
                              textFrame.markup(xmlElementPara); 
                          }
                      
                          for (var k = 0; k < page.rectangles.length; k++) {
                              rectangle = page.rectangles[k];
                              xmlElementImg = xmlElementArticle.xmlElements.add("img"); 
                              rectangle.markup(xmlElementImg); 
                          }
                      }
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function PreCheck() {
                      if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                      doc = app.activeDocument;
                      if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
                      if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                      Main();
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  function ErrorExit(error, icon) {
                      alert(error, scriptName, icon);
                      exit();
                  }
                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                  
                  1 person found this helpful
                  • 6. Re: Tagging in InDesign CS5, js
                    Oriup Level 1

                    Dear Kasyan,

                     

                    You are great. Your script nicely working and you understood my problem. My English is poor.

                     

                    Everything is okay except one thing. I need object in sequence like below:

                     

                    ksyan.PNG

                     

                    Regards,

                    Sumit

                    • 7. Re: Tagging in InDesign CS5, js
                      Kasyan Servetsky Level 5

                      Hi Sumit,

                       

                      My English is far from perfect too.

                       

                      What are you trying to achieve?

                      1. You want to write the script on your own but don't know how, or encountered a problem?
                      2. You're going to post a request-after-request until you have the script written for you by someone else?

                       

                      In case 1

                      Take a look at the geometricBounds property. (If some objects have strokes, use visibleBounds instead). This property is an array of 4 numbers which give you coordinates of the top, left, bottom and right edges of the frame. You can use them to sort frames on the page. How exactly to do this I have no idea because I don't know your situation: how to sort -- first horizontally or vertically, take into account the width and height or not, etc.

                      Search the forum; probably someone has  already done this before.

                       

                      In case 2

                      This forum is mostly for solving scripting problems. Also, people look for ready-to-use scripts here. If you want a script written for you, please describe ALL you need in the original post and maybe someone will honor your request.

                       

                      Regards,
                      Kasyan

                      1 person found this helpful
                      • 8. Re: Tagging in InDesign CS5, js
                        Oriup Level 1

                        Dear Kasyan,

                         

                        Thank you so much for your reply.

                         

                        I will use your last script that is working good and I will move element manually as I needed.

                         

                         

                        Thanks and Regards,

                        Sumit

                        • 9. Re: Tagging in InDesign CS5, js
                          Oriup Level 1

                          Dear Kasyan,

                           

                          When, I edit second loop for P element, then text frames comes in sequence like below.

                           

                                  for (var j = page.textFrames.length-1; j > 0; j--) {  
                                      textFrame = page.textFrames[j];  
                                      xmlElementPara = xmlElementArticle.xmlElements.add("para");   
                                      textFrame.markup(xmlElementPara);   
                                  } 
                          

                           

                          Screen Shot 2016-12-30 at 5.57.34 PM.png

                           

                          But problem is escaping last text frame of every pages.

                           

                          If I use below code:

                           

                                  for (var j = page.textFrames.length; j > 0; j--) {  
                                      textFrame = page.textFrames[j];  
                                      xmlElementPara = xmlElementArticle.xmlElements.add("para");   
                                      textFrame.markup(xmlElementPara);   
                                  }
                          

                           

                          Then get error massage.

                           

                          Screen Shot 2016-12-30 at 6.14.36 PM.png

                          Can you guide me once, please?

                           

                          Regards,

                          Sumit

                          • 10. Re: Tagging in InDesign CS5, js
                            Kasyan Servetsky Level 5

                            You should loop through ALL the objects either forwards

                            for (var i = 0; i < myCollection.length; i++) {
                            

                            or backwards

                            for (var i = myCollection.length - 1; i >= 0; i--) {
                            

                            As far as I understand, you're trying to skip the last element in collection. But there's no dependence between the object's index

                            myCollection[i]
                            

                            and it's position on the page. I guess you want to skip the bottom most frame on the page. In this case (the easiest way) you can check if the object is below a certain position from the top of the page (for simplicity, let's assume that zero point is in the default location: the top left corner of the page) using geometricBounds[0] (top of the frame). Something like this:

                            if (myCollection[i].geometricBounds[0] < 1000) {
                                // add xml element
                            }
                            else {
                                // skip it
                            }
                            

                             

                            Another -- a better and a little more complex way -- is to write a function which would get all frames and compare their top edges returning the bottom most one.

                             

                            — Kas

                            • 11. Re: Tagging in InDesign CS5, js
                              Oriup Level 1

                              Thank you Kasyan!