Skip to main content
Inspiring
May 27, 2008
Question

Multiple Labeled PageItems

  • May 27, 2008
  • 5 replies
  • 771 views
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.
This topic has been closed for replies.

5 replies

Inspiring
June 1, 2008
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
Inspiring
May 28, 2008
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
Jongware
Community Expert
Community Expert
May 28, 2008
Does the reverse work as well?

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

That could be useful ... (thinking ...)
Peter Kahrel
Community Expert
Community Expert
May 27, 2008
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
Inspiring
May 27, 2008
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
Inspiring
May 27, 2008
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.