Wednesday, September 21, 2005
Text Processing
Here's an unusual need. I have a story that uses a bunch of styles. Some of them need to have the name of the style prefixed to every instance of a paragraph that has that style name, so, for example, the paragraph in the style Clone: rather than saying:
This is the kind of thing that doesn't come up very often, and in fact those style names didn't have colons on the end when I started this. The challenge was to find a way of indicating which styles should get the treatment. It was certainly quicker to edit their names and add the colons than to manually insert these labels.
Here's the script:
TKS-1instead should say:
Clone: TKS-1where the whitespace is a tab.
This is the kind of thing that doesn't come up very often, and in fact those style names didn't have colons on the end when I started this. The challenge was to find a way of indicating which styles should get the treatment. It was certainly quicker to edit their names and add the colons than to manually insert these labels.
Here's the script:
//DESCRIPTION: Prefix colonized styles
var myStyles = app.activeDocument.paragraphStyles.everyItem().name;
var myStory = app.selection[0].parentStory;
var appliedStyles = myStory.paragraphs.everyItem().appliedParagraphStyle;
for (var j = myStyles.length -1; j >= 0; j--) {
if (myStyles[j].slice(-1) == ":") {
var myStyle = app.activeDocument.paragraphStyles[j];
for (var k = appliedStyles.length - 1; k >= 0; k--) {
if (myStyles[j] == appliedStyles[k].name) {
myStory.paragraphs[k].insertionPoints[0].contents = myStyles[j] + "\t";
}
}
}
}