4 Replies Latest reply on Jun 22, 2010 3:33 AM by PankChat

    Selecting first frame on a page with same label

    PankChat Level 1

      Hello,
      How can I select a top level (which visually resides above all "sections" frame and not terms of their creations) textframe on a page with same "label".
      I am trying something below.
      if(app.activeDocument==0){
      alert("No File open. Please open a document and try again")
      exit();
      }
      var myDoc=app.activeDocument;
      var myTotalPage = myDoc.pages.length;
      for (var i=0; i<=myTotalPage-1; i++)
      {
      var total_sect_frames=0
      var totalTextFrames = myDoc.pages[i].textFrames.length;
      //Now look for the textFrames with label "sections"
      for (var t=0;t<totalTextFrames;t++)
      {
      //check for labels
      var frame_label = myDoc.pages[i].textFrames[t].label;
      if(frame_label==null)
      {
      $.writeln ("No Label");
      }
      if(frame_label=="sections")
      {
      total_sect_frames++;
      }
      }
      if(total_sect_frames>1)
      {
      //Something Like  to catch the top textFrame  ======================  Error   =====================================//
      myDoc.pages[i].textFrames[0].itemByName ("sections").contents="Sections frame";
      }
      }
      TIA,
      --PC

        • 1. Re: Selecting first frame on a page with same label
          Bal_Glen Level 1
          Hi,

          I'm not quite sure what you are trying to do. But i will try to help.

          I see that you want to select the textFrame "sections" if it is more than one time on the page (your code: total_sect_frames > 1 ). I don't know if that's what you want, but that's not why you get the error.

          Why don't you set the "if(total_sect_frames>1)" inside the "if(frame_label=="sections")", at this point you know the index of the textFrame is t.
          Something like:

          var frame_label = myDoc.pages[i].textFrames[t].label;
          if(frame_label==null)
          {
          $.writeln ("No Label");
          }
          if(frame_label=="sections")
          {
          total_sect_frames++;
          if(total_sect_frames>1)
          {
          myDoc.pages[i].textFrames[t].contents="Sections frame";
          }
          }
          }

          Hope this helps you on your way.
          Greetz,
          Glen

          • 2. Re: Selecting first frame on a page with same label
            PankChat Level 1

            Thanks Glen. I did tried that earlier also.

             

            Getting first textframe on page in terms of appearance on page is not an issue. I need to create a function, which will compare the top and left bounds of the frames. I was just looking for some other way around. Alas, I figure there is no other way.

             

            Anyway, I just modified and tested my script on a 2 page document and found something interesting, which I didn't expect.

             

            I created a 2 page document. 1st page two textframes out of which one is labeled as "sections" and second page contains 3 textframes out which 2 are labelled as "sections".

             

            As per rule defined below the 1st page will be skipped.

             

            For the second page while finding the geometric bounds of each "sections" frame it gives the GB of 1st (on extreme left) only, added swatch colour to this frame only but both of  frames gets the  "Sections frame". I assume why the second textframe with same label is ignored for "swatch" as well as for GB.

             

             

            for (var t=0;t<totalTextFrames;t++)
            {
            //check for labels
            var frame_label = myDoc.pages[i].textFrames[t].label;
            if(frame_label==null)
            {
            $.writeln ("No Label");
            }
            if(frame_label=="sections")
            {
            total_sect_frames++;
            }
            if(total_sect_frames>1)
            {
            myDoc.pages[i].textFrames.itemByName ("sections").contents="Sections frame";
            $.writeln("The Id of frame is : "+myDoc.pages[i].textFrames[t].id+"\n");
            $.writeln("GB of frame is : "+myDoc.pages[i].textFrames[t].geometricBounds+"\n");
            myDoc.pages[i].textFrames[t].fillColor="Red";
            }
            }
            // $.writeln ("Total number of sections frames on :"+myDoc.pages[i].name+ " are: "+total_sect_frames);
            }
            --PC

            • 3. Re: Selecting first frame on a page with same label
              Bal_Glen Level 1

              Hi,

               

              Do you want to compare the top and left of the textframes that are located on the same page?

               

               

               

              Just for giving a quick answer on the script:

              The result of the script seems correct according to the script.

               

              Explanation:

              When the second frame with name "sections" is encountered, you set the content of all textframes named "sections". (the itemByName selectes every textframe named "sections" on page i).

              Next you give the instruction the give the GB and color to the current textframe t (which can be the extreme left on the page, depending on the order  of the textframes). This will result in only 1 corresponding textframe in your example

               

              Your code:

              if(total_sect_frames>1)

              {
              myDoc.pages[i].textFrames.itemByName ("sections").contents="Sections frame";
              $.writeln("The Id of frame is : "+myDoc.pages[i].textFrames[t].id+"\n");
              $.writeln("GB of frame is : "+myDoc.pages[i].textFrames[t].geometricBounds+"\n");
              myDoc.pages[i].textFrames[t].fillColor="Red";
              }

              Greetz,
              Glen
              • 4. Re: Selecting first frame on a page with same label
                PankChat Level 1

                Do you want to compare the top and left of the textframes that are located

                on the same page?

                 

                 

                No, I know how to do that.

                 

                I am rushing for something right now I will get back to it soon.

                 

                Thanks,

                 

                --PC