momin naveed
momin naveed

Reputation: 301

Indesign render script need to target TextFrames within Rectangle objects

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.

Upvotes: 0

Views: 255

Answers (2)

user1754036
user1754036

Reputation: 544

...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);

Upvotes: 0

Yuri Khristich
Yuri Khristich

Reputation: 14502

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

enter image description here

Upvotes: 1

Related Questions