I am working on InDesign rendering script (.jsx) for rendering template (.indd) using InDesign Server (Version: 17.2). In my InDesign templates, there are Rectangle ([object Rectangle]) items, that further includes some TextFrames (more than one). When I try to access those TextFrames using [object Rectangle].allpageItems, it gives and empty array. How can I access those TextFrames from the [object Rectangle]? Further, can I get/find the InDesign element/item (TextFrame) using xmlTag name? If yes, then please provide the example by which we can find the item.
Asked
Active
Viewed 119 times
0
-
text frames are not siblings of rectangles. It might be easier to have the InDesign user *group* the text frames with the rectangle using InDesign's *group* feature. With that in place, a script could find a rectangle's group and then search the group for text frames. XML Elements are siblings of the document and may be applied to stories and text in a story. Those stories may have one or more text frames. – user1754036 Jan 27 '23 at 14:14
-
@user1754036 typically that's the case, but I just learned that you can, in fact, paste a text frame into a selected rectangle making the text frame a sibling of the rectangle. – user1754036 Mar 09 '23 at 15:57
2 Answers
1
I suppose it should be something like this:
var doc = app.activeDocument;
var rectangle = doc.rectangles[0]; // get a first rectangle
var text_frame = rectangle.textFrames[0]; // get a textFrame inside of the rectangle
text_frame.select(); // select the text frame

Yuri Khristich
- 13,448
- 2
- 8
- 23
0
...can I get/find the InDesign element/item (TextFrame) using xmlTag name?
Yes, that is probably the best way to set up your automation. However, XML Elements are applied to stories and text ranges inside stories. To get the first text frame for each story tagged "test" you could use the following:
// Get the current InDesign document
var doc = app.activeDocument;
// Create an empty array to store the text frames
var frameArray = [];
// Loop through each story in the document
for (var i = 0; i < doc.stories.length; i++) {
// Check if the story has an associated XML element with a markup tag named "test"
if (doc.stories[i].associatedXMLElement !== null && doc.stories[i].associatedXMLElement.markupTag.name == "test") {
// Add the first text container of the story to the array
frameArray.push(doc.stories[i].textContainers[0]);
}
}
// Select the first text frame in the array
frameArray[0].select();
// Output the array to the console
alert(frameArray);
If your automation is designed to make changes to the text inside the text frame you'll want to build an array of stories instead:
// Get the current InDesign document
var doc = app.activeDocument;
// Create an empty array to store the text frames
var storyArray = [];
// Loop through each story in the document
for (var i = 0; i < doc.stories.length; i++) {
// Check if the story has an associated XML element with a markup tag named "test"
if (doc.stories[i].associatedXMLElement !== null && doc.stories[i].associatedXMLElement.markupTag.name == "test") {
// Add the first text container of the story to the array
storyArray.push(doc.stories[i]);
}
}
// Set the active story to the first story in the array and select its contents
if (storyArray.length > 0) {
var firstStory = storyArray[0];
app.select(firstStory.texts[0]);
}
// Output the array to the console for testing
alert(storyArray);

user1754036
- 396
- 1
- 6