InDesign: How can I get paragraph contents with its characterStyles?

前提是你 提交于 2019-12-11 10:48:55

问题


I'm trying to write a very custom export script for InDesign (I have CC 9.2, but my target is CS6).

When I process a paragraph, I can get its paragraphStyle and its content. What I don't understand is: how can I get the content and the characterStyles of the content?

I'm looking at the docs, but I don't understand how can I descend into a paragraph and find all characterStyles and the text parts to which they are applied.

If, for instance, I have a paragraph like the following:

My nice paragraph.

I want to know that "My " and " paragraph." have style1, while "nice" have style2.

The end result should be something like:

[
  {
    text: "My ",
    style: "style1"
  },
  {
    text: "Nice",
    style: "style2"
  },
  {
    text: " paragraph.",
    style: "style1"
  }
]

How can I obtain that information?


回答1:


Use the TextStyleRange property of a paragraph.

A TextStyleRange is one single continuous range of text with the same formatting. It does not matter to InDesign whether the formatting is 'local' or applied through a character style.

Obligatory Caveats:

  1. TextStyleRanges see all local formatting, whether or not applied through a Char Style.
  2. TextStyleRange formatting does not see GREP styles.
  3. The last text range in a paragraph may (and usually will) happily contain the final return, as well as any and all text in following paragraphs that still have the exact same formatting. If you retrieve textStyleRanges per paragraph, check each returned text contents for the presence of text after a paragraph return. If it does contain a paragraph return, optionally clip off the excess text, and exit the loop for the current paragraph.

very Minimal example code:

tsr = app.selection[0].paragraphs[0].textStyleRanges;

text = [];
for (i=0; i<tsr.length; i++)
    text.push ('text: "'+tsr[i].contents+'", style: "'+tsr[i].appliedCharacterStyle.name+'"');
alert (text.join ('\r'));


来源:https://stackoverflow.com/questions/23057648/indesign-how-can-i-get-paragraph-contents-with-its-characterstyles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!