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?
sttk3
March 6, 2023, 11:01pm
2
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]) ;
sttk3
March 7, 2023, 3:48am
3
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]) ;
Reference:
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.
sttk3
March 7, 2023, 10:07am
5
That’s fine. Now let’s pick the solution checkbox to announce the resolve.