Jos*_*h B 5 jsx indesign-server adobe-indesign
我正在尝试使用inDesign JSX脚本将以下数据插入到文档中:
data = [{heading:"Heading 1", content: ["Some content"]},
{heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]
Run Code Online (Sandbox Code Playgroud)
数据必须放在单个TextFrame中,但在标题和内容上具有不同的样式.
我可以看到添加文本的唯一方法是通过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
Run Code Online (Sandbox Code Playgroud)
然后,一旦数据进入,我迭代段落并添加一些样式标题:
for (var i = 0; i < textFrame.paragraphs.count(); i++) {
if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
textFrame.paragraphs[i].pointSize = 20;
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于适合一个页面的小数据集,但是一旦内容比框架大,paragraphs只返回可见的段落.如果我继续使用新的textFrame,则段落会被分割,并且headingParagraphs[]数组不再排列.
理想情况下,我想在添加下一个内容之前附加到内容并设置样式 - 但是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
Run Code Online (Sandbox Code Playgroud)
有没有办法在添加内容后使用追加功能或其他方式将标题指定到正确的位置?也许在定义样式的内容中有特殊字符?
您的较长文本会搞乱,因为您当前正在单个文本框架内工作.一旦文本用完这一帧,就不能再将它们称为此框架的"拥有"段落.使用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
}
}
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是,你不能暂时覆盖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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这里反转插入内容和应用格式是值得的.这是任何以前的'临时'格式被清除; 以这种方式应用段落样式会覆盖任何和所有本地覆盖.由于您必须将样式应用于上一段(刚刚插入的硬回车之前的那段),您可以insertionPoints[-2]在这里使用.
使用样式而不是本地格式的优点是无数的.您可以使用单个命令应用所需的所有格式,安全地删除所有本地覆盖的格式,并在不满意的情况下全局更改格式的任何部分,而不必使用稍微不同的设置重新运行脚本.
| 归档时间: |
|
| 查看次数: |
1817 次 |
| 最近记录: |