Skip to main content
Inspiring
July 13, 2022
Answered

How to access a textFrame with label?

  • July 13, 2022
  • 2 replies
  • 1313 views

hi All, I have approx 20 pages in a document and want to empty for a new monthly issue. I have assinged labels to each textframe e.g. One, Two, Three, Four etc. I am using following script but I want to access a particular textFrame using label directly without loop something like below. 

var myFrame = myDoc.pageItems.itemsByID(2034); // something for a textFrame with script label "One".

 

This is the script:

var myDoc = app.activeDocument;

var i;

var myFrame = myDoc.textFrames;

for (i=0; myFrame.length; i++)

{

Switch (myFrame[i].label)

case "One":

myFrame[i].characters.itemByRange(24, -1).remove(); // here i want few lines

case "Two"

myFrame[i].contents = "";  // no text in the frame

case "Three"

myFrame[i].contents = "";  // no text in the frame

and so on

}

This topic has been closed for replies.
Correct answer Laubender

Hi @BarlaeDC ,

itemByName refers to property "name" of that text frame and not to "label".

"label" means a script label by definition.

 

I'm not sure how @virender_CTS assigned a label value to a particular frame.

If he uses property label it must be a script label. It's value can be seen in the UI with the Scripts Label Panel.

 

Looking for labels and their value:

Iterate the pageItems collection or the allPageItems array of a page, a spread or the document to read out the value.

Or use the textFrames collection that would not include nested text frames like anchored or grouped ones.

 

Directly this cannot be done anymore.

Once that was possible with InDesign CS4, but this changed with InDesign CS5.

That all said, one could force InDesign to a script version 6 environment. And if done, one could get all the labeled text frames with: app.documents[0].textFrames.item("NameOfLabel").

 

Here some code:

var currentVersion = app.scriptPreferences.version;
// String "17.0"  for InDesign 2022 version 17.3.0

// Set version to OLD InDesign CS4:
app.scriptPreferences.version = "6.0";

// Address all text frames with label value "LabelOne" in one go:
app.documents[0].textFrames.item("LabelOne").fillColor = "Yellow";

// Reset version:
app.scriptPreferences.version = currentVersion;

 

Don't know if that approach will work for your purposes.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

 

 

2 replies

Kasyan Servetsky
Brainiac
July 13, 2022

Here are a few useful functions.

Adobe Expert
July 13, 2022

Hi Kasyan,

thank you for the link!

( Especially the function by Loic is interesting… )

 

Regards,
Uwe Laubender
( Adobe Community Professional )

BarlaeDC
Adobe Expert
July 13, 2022

Hi,

 

You should be able to use

 

 

var myFrame = app.activeDocument.textFrames.itemByName("ScriptLabel");

 

So the above was complete rubbish, although I knew you could do something like it. BUT you gotta go back to a previous version 😛

// Store the version of the current scripting environment:
var currentScriptingEnvironment = app.scriptPreferences.version ;
// Set the scripting environment to the version of InDesign CS4:
app.scriptPreferences.version = "6.0";
var allFrames = app.activeDocument.pageItems.item("frameOne").getElements();
// IMPORTANT: Set the scripting environment back:
app.scriptPreferences.version = currentScriptingEnvironment;

 

Inspiring
July 13, 2022

Hi, Thanks for the reply. I checked it twice but it says object is invvalid, however with loop I can do it.

var myFrame = app.activeDocument.textFrames.itemByName("One");
myFrame.contents = "";

brian_p_dts
Adobe Expert
July 13, 2022

itemByName only works on pageItems named in the layers panel. To access items with labels, you need to iterate through all text frames and operate. You could also write a helper function that converts the labels to names, but if two items have the same label, then you would still need to iterate, since itemByName would only find the first one (unless you were to iterate through pages and check, ie page[i].textFrames.itemByName("One");