6 Replies Latest reply on Jun 1, 2008 2:05 AM by Dirk Becker 

    Multiple Labeled PageItems

    Level 1
      I was faced, this morning, with writing some code that would determine whether or not a reference obtained by:

      dataFrame = myDoc.textFrames.item("MyLabel");

      return 0, 1 or more items.

      0 is easy because dataFrame == null in that case

      1 is easy because things work just the way you want them. But how do you know if you have more than 1 returned?

      In my case, I was trying to get the contents of what was supposed to be a unique text frame. I decided to test to see if there might be (accidentally) more than one. I'll post my solution in the first message.
        • 1. Re: Multiple Labeled PageItems
          Level 1
          Here's what I (in effect) used:
          dataFrame = myDoc.textFrames.item("MyLabel");
          
          if (dataFrame != null) {
            try {
              dataFrame.absoluteFlip.length;
              alert("More than one frame; using the one on page " + dataFrame.parent[0].name);
              myData = dataFrame.parentStory[0].contents;
            } catch (e) {
              myData = dataFrame.parentStory.contents;
            }
            // process myData here
          }
          And while this works, I always feel like I cheated if I resort to using try/catch for this kind of thing.

          Oddly, even though dataFrame.absoluteFlip.length returns a valid value when more than one frame is returned (as a composite reference), dataFrame.absoluteFlip.hasOwnProperty("length") returns false -- which rather floored me.

          Dave
          • 2. Re: Multiple Labeled PageItems
            Peter Kahrel Adobe Community Professional & MVP
            I too have some intuitive reservation against try/catch, which I feel boils down to closing your eyes and hoping for the best. Somehow it seems better first to try whether it's safe to do something -- look before you leap, as it were. Still, try/catch is useful and I use it a lot, especially when it's not evident which exceptions to test for.

            As to "how do you know if you have more than 1 returned?", maybe you could use this:

            >if (myDoc.textFrames.item("MyLabel").getElements().length > 0)

            to see if there are more than one textframes labelled "MyLabel".

            Peter
            • 3. Re: Multiple Labeled PageItems
              Fred Goldman Level 3
              I was also rather surprised when I use this:

              app.activeDocument.textFrames.item("label").remove()

              It removed all 50 of them. I was expecting some kind of error if you had
              multiple text frames with the same label.
              • 4. Re: Multiple Labeled PageItems
                [Jongware] Most Valuable Participant
                Does the reverse work as well?

                >app.activeDocument.textFrames.item("label").contents = ...

                That could be useful ... (thinking ...)
                • 5. Re: Multiple Labeled PageItems
                  Level 1
                  Jongware,

                  Yes it does, and yes, there are times when that is useful.

                  Peter,

                  Thanks. I think I tried to actually do that but must have screwed up the syntax.

                  Dave
                  • 6. Re: Multiple Labeled PageItems
                    Dirk Becker  Level 4
                    For checking, you can use the instanceof operator:

                    var a = app.activeDocument.textFrames.item("bla");
                    if( a.contents instanceof Array ) ...

                    The following does not work, though:
                    a instanceof TextFrames

                    Dirk