Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Expert ,
Jan 21, 2009 Jan 21, 2009
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
TOPICS
Scripting
618
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 21, 2009 Jan 21, 2009
allPageItems returns an array. What you need is probably:
>var tabFrame = spread.pageItems.item("TabLeft")

Peter
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 21, 2009 Jan 21, 2009
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 21, 2009 Jan 21, 2009
LATEST
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.label.match(/Tab(Right|Left)/)) {

// Do something here.
}

}


Thanks for the generous help.

Rick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines