InDesign JSX script to add title and content to textFrame

I am trying to use inDesign JSX scripts to insert the following data into a document:

data = [{heading:"Heading 1", content: ["Some content"]},
 {heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]

Data should be placed in one TextFrame, but have different styles for the header and content.

The only way I can add text is in one pass through the variable textFrame.contents:

allContent = "";
headingParagraphs = []; // keep track of which paragraphs are headings
paragraph = 0;
for (var i = 0; i < data.length; i++) {
  allContent += data.heading + "\r"; // Use a newline to split the paragraph
  headingParagraphs.push(paragraph);
  paragraph++;
  for (var j = 0; j < data.content.length; j++) {
    allContent += data.content[j] + "\r"; // Use a newline to split the paragraph
    paragraph++;
  }
}
textFrame.contents = allContent; // all data is in, but all text is styled the same

Then, as soon as the data is entered, I repeat the paragraphs and add the style to the headings:

for (var i = 0; i < textFrame.paragraphs.count(); i++) {
  if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
    textFrame.paragraphs[i].pointSize = 20;
  }
}

This is great for small datasets that fit on one page, but as soon as the content becomes larger than the frame, it paragraphsonly returns visible paragraphs. And if I follow the new text frame, the paragraphs are divided, and the array headingParagraphs[]no longer lines up.

, API , ( )

// Pseudo code:
for all sections:
  append the heading to the frame, split to next page if needed
  style all the *new* paragraphs as headings
  for all section contents
    append the content to the frame, split to next page if needed
    style any *new* paragraphs as normal content

, , - ? , ?

+4
1

, . , , "". parentStory , , . , .

, textFrame, story textFrame.parentStory .

(/story): , . contents . , , - INX . , ( ) Javascript, ID - ... , , , "",.

. story.insertionPoints[-1]. , , . " "; , , , .

, data :

for (var i = 0; i < data.length; i++)
{
    story.insertionPoints[-1].pointSize = 20;
    story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
    story.insertionPoints[-1].pointSize = 10;
    for (var j = 0; j < data[i].content.length; j++)
    {
        story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
    }
}

, pointSize . , ( "10" ).

? -

hdrStyle = app.activeDocument.paragraphStyles.item("Header");
textStyle = app.activeDocument.paragraphStyles.item("Text");

for (var i = 0; i < data.length; i++)
{
    story.insertionPoints[-1].contents = data[i].heading + "\r"; // Use a newline to split the paragraph
    story.insertionPoints[-2].appliedParagraphStyle = hdrStyle;
    for (var j = 0; j < data[i].content.length; j++)
    {
        story.insertionPoints[-1].contents = data[i].content[j] + "\r"; // Use a newline to split the paragraph
        story.insertionPoints[-2].appliedParagraphStyle = textStyle;
    }
}

, . , "" ; . (, , ), insertionPoints[-2] .

. , , , , , script .

+5

Source: https://habr.com/ru/post/1542859/


All Articles