Eugene, thanks so much for that script. I tried to modify it to assign a style to the second paragraph when a match is found, but I think I lack knowledge of the object structures or syntax. Here is what I tried to use: // Check if a document is open
if (app.documents.length === 0) {
alert("Please open a document before running this script.");
} else {
var doc = app.activeDocument;
// Try to get the "Pink" swatch or create it if it doesn't exist
var PinkSwatch;
try {
PinkSwatch = doc.swatches.itemByName("Pink");
// If it exists, just skip the creation part
PinkSwatch.name; // Access to confirm existence (does nothing if it exists)
} catch (e) {
// If "Pink" swatch doesn't exist, create it
PinkSwatch = doc.colors.add({
name: "Pink",
model: ColorModel.process,
colorValue: [0, 100, 0, 0] // CMYK value for Pink (Magenta)
});
// We won't add it to the swatches, just use it directly
}
// Create a dialog window
var dlg = new Window("dialog", "Change Para Style based on Previous");
dlg.orientation = "column";
dlg.alignChildren = ["fill", "top"];
// Add dropdowns for the first and second style selection
dlg.add("statictext", undefined, "Select the first paragraph style:");
var firstStyleDropdown = dlg.add("dropdownlist");
dlg.add("statictext", undefined, "Select the following paragraph style:");
var secondStyleDropdown = dlg.add("dropdownlist");
dlg.add("statictext", undefined, "Select the paragraph style you want to change it to:");
var newStyleDropdown = dlg.add("dropdownlist");
// Populate dropdowns with paragraph styles from the active document
var paraStyles = doc.allParagraphStyles;
for (var i = 0; i < paraStyles.length; i++) {
var item1 = firstStyleDropdown.add("item", paraStyles[i].name);
var item2 = secondStyleDropdown.add("item", paraStyles[i].name);
var item3 = newStyleDropdown.add("item", paraStyles[i].name);
}
// Optionally, set defaults (for example, if you know the names)
firstStyleDropdown.selection = firstStyleDropdown.find("Heading");
secondStyleDropdown.selection = secondStyleDropdown.find("Main text");
newStyleDropdown.selection = newStyleDropdown.find("Main - no indent");
// Add the action buttons
var btnGroup = dlg.add("group");
btnGroup.alignment = "center";
var findBtn = btnGroup.add("button", undefined, "Find", {name:"ok"});
var cancelBtn = btnGroup.add("button", undefined, "Cancel", {name:"cancel"});
// Run the dialog
if (dlg.show() == 1) {
var styleOne = firstStyleDropdown.selection.text;
var styleTwo = secondStyleDropdown.selection.text;
var styleNew = newStyleDropdown.selection.text;
var results = [];
// Loop through all stories and their paragraphs
for (var s = 0; s < doc.stories.length; s++) {
var story = doc.stories[s];
for (var p = 0; p < story.paragraphs.length - 1; p++) {
var para1 = story.paragraphs[p];
var para2 = story.paragraphs[p + 1];
// Check if para1 uses the first style and para2 uses the second style
if (para1.appliedParagraphStyle.name === styleOne &&
para2.appliedParagraphStyle.name === styleTwo) {
results.push("Story " + (s+1) + ", Paragraphs " + (p+1) + " and " + (p+2));
// For a visual cue, apply the PinkSwatch colour directly
//para1.fillColor = PinkSwatch;
para2.appliedParagraphStyle.name = styleNew;
para2.fillColor = PinkSwatch;
}
}
}
// Provide feedback to the user
if (results.length > 0) {
alert("Found " + results.length + " occurrence(s):\n" + results.join("\n"));
} else {
alert("No occurrences found where '" + styleOne + "' is immediately followed by '" + styleTwo + "'.");
}
}
} When I ran it, I got the following error: So apparently giving the `appliedParagraphStyle.name` property a new value is not how to change the style applied to the paragraph. Do you know how that is supposed to be done?
... View more