如何使用 InDesign 脚本选择段落并更改其样式
How to choose a paragraph an change its style with InDesign script
我正在将 XML 文档解析到 InDesign 中,而不是尝试更改每个段落的样式。
如果我这样做:
myTextFrame.parentStory.texts.item(0).applyParagraphStyle(<<style>>);
它将更改整个文本,因为它是一个段落。
但是,此代码根本不起作用:
for (var i = 0; i < <<paragraphs.length>>; i++)
{
if (styles[i].isValid)
myTextFrame.parentStory.texts.item(i).applyParagraphStyle(styles[i]);
}
此外,我似乎无法在 'Paragraph' class 上调用 'applyParagraphStyle' 方法。
我的XML是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<book>
<authors>
<author>Lee Roy</author>
</authors>
<title>Name of the book</title>
<subtitle>How to XML</subtitle>
</book>
</root>
然后我这样解析它们:
var string = "";
for (var i = 0; i < wholeXML.length(); i++)
{
var book = wholeXML.child(i);
string += book.child("title").toString() + "\r";
string += book.child("subtitle").toString() + "\r";
}
输出看起来像这样:
Name of the book
How to XML
现在,我的问题是:如何更改单个段落的样式?
您可以通过设置 appliedParagraphStyle
属性.
来简单地更改单个段落的段落样式
myTextFrame.parentStory.paragraphs[0].appliedParagraphStyle = "nameOfParagraphStyle";
我正在将 XML 文档解析到 InDesign 中,而不是尝试更改每个段落的样式。
如果我这样做:
myTextFrame.parentStory.texts.item(0).applyParagraphStyle(<<style>>);
它将更改整个文本,因为它是一个段落。
但是,此代码根本不起作用:
for (var i = 0; i < <<paragraphs.length>>; i++)
{
if (styles[i].isValid)
myTextFrame.parentStory.texts.item(i).applyParagraphStyle(styles[i]);
}
此外,我似乎无法在 'Paragraph' class 上调用 'applyParagraphStyle' 方法。
我的XML是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<book>
<authors>
<author>Lee Roy</author>
</authors>
<title>Name of the book</title>
<subtitle>How to XML</subtitle>
</book>
</root>
然后我这样解析它们:
var string = "";
for (var i = 0; i < wholeXML.length(); i++)
{
var book = wholeXML.child(i);
string += book.child("title").toString() + "\r";
string += book.child("subtitle").toString() + "\r";
}
输出看起来像这样:
Name of the book
How to XML
现在,我的问题是:如何更改单个段落的样式?
您可以通过设置 appliedParagraphStyle
属性.
myTextFrame.parentStory.paragraphs[0].appliedParagraphStyle = "nameOfParagraphStyle";