InDesign itemByName not working in UXP

Hi, I’m trying to script InDesign. Maybe I misunderstand the functionality, but I have placed a TextFrame in my document, given it a script label like “LabelOne”, then it looks like I should be able to do:

let textFrame = app.activeDocument.textFrames.itemByName("LabelOne");

But it always comes back as undefined… Is it possible to find an item by script label?

itemByName is a method to retrieve an item by the name set in layer panel. You prefer to use the name of layer panel instead of script label.

If you must use script label, it looks like this.

const toArray = (collection) => {
  const res = [] ;
  for(let i = 0, len = collection.count() ; i < len ; i++) {
    res.push(collection.item(i)) ;
  return res ;
} ;

const doc = app.activeDocument ;
const targetTextFrames = toArray(doc.textFrames).filter((aFrame) => {
  return aFrame.label === 'LabelOne' ;
}) ;

console.log(targetTextFrames[0]) ;

A more InDesign-savvy @peprintenpa showed me a better way.

const targetTextFrames = app.activeDocument.textFrames.everyItem().getElements().filter((aFrame) => {
  return aFrame.label === 'LabelOne' ;
}) ;

console.log(targetTextFrames[0]) ;


Awesome - thank you so much for both the clarification and the code sample!

Retrieving by name in the layer panel works perfectly for my use case.

That’s fine. Now let’s pick the solution checkbox to announce the resolve.