7 Replies Latest reply on Aug 16, 2011 8:35 AM by csm_phil

    [ID 5.5, JS] Un/group text frames in document

    Boesiger Pia

      I have a document of several hundred pages (ID 5.5, JS). I need to set new coordinates to the textframes. This works fine as long as there is only one textframe per page. That's my code:


          var myStory = myDoc.textFrames;
      for (var i = 0; i< myStory.length; i++) {
          var myPage = myStory[i];
                 myPage.geometricBounds = [30,23.5,267,186.5]


      Obviously this doesn't work for pages where I have more than one textframe. My theoretical approach is to first group these and then set the new coordinates to the grouped textframes and, after the job is done, ungroup them.


      I found the following script by Tomaxxi which works fine for the active page, but I need the job done for the whole document.

      var myObj = new Array; myObj = app.activeWindow.activePage.pageItems; app.activeWindow.activePage.groups.add(myObj);

      After some fruitless effort I must admit I have no idea how to adapt it to my specific needs. Any help is much appreciated.



        • 1. Re: [ID 5.5, JS] Un/group text frames in document
          John Hawkinson Level 5

          By the way, this code is bad:

           

          var myStory = myDoc.textFrames;
          for (var i = 0; i< myStory.length; i++) {
              var myPage = myStory[i];
                     myPage.geometricBounds = [30,23.5,267,186.5]

           

          It is bad because Stories, TextFrames, and Pages are all diffrent kinds of objects that cannot be freely interchanged. But you have a variable called myStory that contains multiple TextFrames (plural vs. singular problem, as well as Story vs. TextFrame problem), a variable called myPage that contains a single TextFrame, yet is assigned to from myStory.

           

          This makes your script incredibly confusing. Both for you, because it'll cause confusion as you learn the DOM, and definitely confusing for anyone who knows the DOM. You're much better off with:

           

          var i, myTextFrames = myDoc.textFrames, myTextFrame;
          for (i=0; i<myTextFrames.length; i++) {
            myTextFrame = myTextFrames[i];
            myTextFrame.geometricBounds = [30,23.5,267,186.5];
          }
          

           

          Also, there I move your var statements out of the loop, because in JavaScript, there is no such thing as block scope. Even if you declare your myPage/myTextFrame variable inside the loop, it is not scoped to the loop. It is scoped to the entire function() it resides in (or the whole program). Placing the var inside makes you think the scope is limited, but its not.

           

          On to your question.

          My theoretical approach is to first group these and then set the new coordinates to the grouped textframes and, after the job is done, ungroup them.

          It seems odd to me that this meets your requirements (what are they?), but OK...

          If you want to adapt tomaxxi's script, you'd just loop over every page, like this:

           

          var i, myObj = [];
          for (i=0; i<myDoc.pages.length; i++) {
            myObj = myDoc.pages[i].pageItems;
            myDoc.pages[i].groups.add(myObj);
          }
          

           

          However this groups all items on a page, not just text frames.  And of course, you'll then need to modify your initial script to operate on groups in the document. And some of them will be groups of one.

          It would probably run faster if you just operated on the textframes directly rather than the groups, but I suppose you are optimizing for writing the script and not running the script, which seems reasonable.

          • 2. Re: [ID 5.5, JS] Un/group text frames in document
            Boesiger Pia Level 1

            Thanks for your help and instructive explanation.

            Unfortunately the loop generates the error message "invalid parameter". It does group the frames on the first page, but not further.

            Why does this happen?

            • 3. Re: [ID 5.5, JS] Un/group text frames in document
              John Hawkinson Level 5

              I imagine it fails because of something about your second page. Does it have no page items? Or page items that cannot be grouped for some reason?

              Is it the second page or a later one?

               

              If you add either alert("I am on page"+i); or $.writeln("I am on page "+i) to your loop, then it will either pop up a dialog box or print a line in the JavaScript console, each time it move sto a new page. This should help you track down the problem. (As would querying the value of i in the JavaScript console when the error comes up).

               

              Does tomaxxi's script work on your second page? If it does, and this one fails, then I find that fairly mysterious.

              If it does fail, though, then you're just getting what you asked for...

              1 person found this helpful
              • 4. Re: [ID 5.5, JS] Un/group text frames in document
                Boesiger Pia Level 1

                It fails when there's only one item per page.

                Now, how can I make the script choose only the pages with more than one item?

                • 5. Re: [ID 5.5, JS] Un/group text frames in document
                  csm_phil Level 4

                  Hi Pia,

                   

                  I have checkd your requirement in the John js code.

                   

                  Just i have modified your more than 1 items select

                   

                  var myDoc = app.activeDocument;
                  var i, myObj = [];
                  for (i=myDoc.pages.length-1; i>=0; i--) {
                    myObj = myDoc.pages[i].pageItems;
                    if(myObj.length >1){
                         myDoc.pages[i].groups.add(myObj);
                         }
                  }
                  

                   

                  thx

                  csm_phil

                  • 6. Re: [ID 5.5, JS] Un/group text frames in document
                    Boesiger Pia Level 1

                    Hi csm_phil

                    This is exactly what I needed.

                    Thanks a lot.

                    Pia

                    • 7. Re: [ID 5.5, JS] Un/group text frames in document
                      csm_phil Level 4

                      Hi Pia,

                       

                      If you dont mine please select the help full answer or correct answer becasue the question is one in the thread.

                       

                      thx

                      csm_phil