3 Replies Latest reply on Jan 21, 2009 10:53 AM by frameexpert

    [CS3 JS] Getting a page item by its script label

    frameexpert Level 4
      I have a text frame on a master page with its script label set. In my script, I am trying to get a reference to the text frame with this:

      var doc = app.activeDocument;
      
      var spread = doc.masterSpreads[0];
      var tabFrame = spread.allPageItems.item("TabLeft");


      I get the error "spread.allPageItems.item is not a function"

      Any help will be appreciated. Thanks.

      Rick
        • 1. Re: [CS3 JS] Getting a page item by its script label
          Peter Kahrel Adobe Community Professional & MVP
          allPageItems returns an array. What you need is probably:
          >var tabFrame = spread.pageItems.item("TabLeft")

          Peter
          • 2. Re: [CS3 JS] Getting a page item by its script label
            Level 1
            Hi Rick,

            spread.allPageItems returns a flattened array of all of the page items in the spread, at any level of the hierarchy (i.e., inside groups and other page items). Is that what you want? Sadly, allPageItems does not benefit from the special case where we use the label as a name, so you'll have to iterate if that's what you want.

            As Peter says (thanks, Peter!), spread.pageItems.item("label") gives you all of the page items whose label matches the string, but it does so in sort of a weird way--you'll always get a page item object. If no page items match, you get an invalid page item; if one matched, you get a single page item, and if multiple page items matched, you get a page item object that actually contains multiple page items. It can take a little testing to figure out exactly what it is that was returned.

            Thanks,

            Ole
            • 3. Re: [CS3 JS] Getting a page item by its script label
              frameexpert Level 4
              OK, that explains the odd behavior I was seeing. I decided to use the code below. While not as elegant, it works!

              var doc = app.activeDocument;
              
              var spread = doc.masterSpreads[0];
              var oPageItems = spread.allPageItems;
              for (var i = 0; i < oPageItems.length; i+=1) {

                  if (oPageItems[i].label.match(/Tab(Right|Left)/)) {

                      // Do something here.
                  }

              }


              Thanks for the generous help.

              Rick