Justin,
When thinking about having ID search for a Pstyle in a style group, you should also consider what if it is in a style group within a style group, or nested even further? Here is a function that calls another function that takes care of this. I didn’t write this…I got it from another scripter (probably the very-smart Kris Coppieters years ago). But have used it in many, many scripts and it works great.
// Here is an example of how to call the function
app.changeGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.itemByID(locateParagraphStyle(“Table body, cond, center”));
// Function to find a paragraph style possibly contained inside a style group. Return id of the paragraph style.
function locateParagraphStyle(styleNameToLocate, pathSeparator) {
var myDoc = app.activeDocument;
var rootPath = “”;
if (pathSeparator !== undefined) {
rootPath = pathSeparator;
}
var retVal = locateParagraphStyleRecursive(myDoc, styleNameToLocate, pathSeparator, rootPath);
if (! retVal) {
return “not found”;
}
return retVal.id;
}
// Function to find paragraph styles that are in style groups. Returns the paragraph style or null if not found.
function locateParagraphStyleRecursive(paragraphStyleContainer, styleNameToLocate, pathSeparator, parentPath) {
var retVal = null;
do {
try {
var paragraphStyles = paragraphStyleContainer.paragraphStyles;
var numParagraphStyles = paragraphStyles.length;
for (
var paragraphStyleIdx = 0;
retVal == null && paragraphStyleIdx < numParagraphStyles;
paragraphStyleIdx++) {
var paragraphStyle = paragraphStyles.item(paragraphStyleIdx);
if (paragraphStyle.isValid) {
var paragraphStyleName = paragraphStyle.name;
if (pathSeparator) {
paragraphStyleName = parentPath + paragraphStyleName;
}
if (paragraphStyleName == styleNameToLocate) {
retVal = paragraphStyle;
}
}
}
if (retVal) {
break;
}
var paragraphStyleGroups = paragraphStyleContainer.paragraphStyleGroups;
var numParagraphStyleGroups = paragraphStyleGroups.length;
for (
var paragraphStyleGroupIdx = 0;
retVal == null && paragraphStyleGroupIdx < numParagraphStyleGroups;
paragraphStyleGroupIdx++) {
var paragraphStyleGroup = paragraphStyleGroups.item(paragraphStyleGroupIdx);
if (paragraphStyleGroup.isValid) {
var path = parentPath;
if (pathSeparator) {
var paragraphStyleGroupName = paragraphStyleGroup.name;
path += paragraphStyleGroupName + pathSeparator;
}
retVal = locateParagraphStyleRecursive(paragraphStyleGroup, styleNameToLocate, pathSeparator, path);
}
}
}
catch (err) {
}
}
while (false);
return retVal;
}