After choosing "Select All Unused" from the Paragraph Styles panel, you should be able to click the trash can icon to delete those styles. The same should work for the Character Styles panel.
I wondered if ChatGPT was smart enough to figure this out. It helped, but couldn't deliver a full solution. We went through 10 different versions before getting close to the working version of the script below.
Here's my final version:
var myDocument = app.activeDocument;
// Get all the character styles in the document
var allCharacterStyles = myDocument.allCharacterStyles;
// Loop through each character style
for (var i = 0; i < allCharacterStyles.length; i++) {
var style = allCharacterStyles[i];
// Set the search criteria for the findText() method
app.findTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedCharacterStyle = style;
// Search entire document for instances of the character style
var found = myDocument.findText();
// If the style is not found in the document, delete it
if (found.length == 0) {
try {
style.remove();
} catch (e) {
// Ignore the error
}
}
// Reset the findTextPreferences object
app.findTextPreferences = NothingEnum.nothing;
}
// Repeat the above for Paragraph Styles
// Get all the paragraph styles in the document
var allParagraphStyles = myDocument.allParagraphStyles;
// Loop through each paragraph style
for (var i = 0; i < allParagraphStyles.length; i++) {
var style = allParagraphStyles[i];
// Set the search criteria for the findText() method
app.findTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = style;
// Search entire document for instances of the style
var found = myDocument.findText();
// If the style is not found in the document, delete it
if (found.length == 0) {
try {
style.remove();
} catch (e) {
// Ignore the errors
}
}
// Reset the findTextPreferences object
app.findTextPreferences = NothingEnum.nothing;
}
And here's a version that generates a report showing a list of unused paragraph styles. Again, ChatGPT can help with some, but it couldn't produce a final version. My edited and tested script appears below.
var myDocument = app.activeDocument;
// Create an array to store the names of unused styles
var unusedStyles = [];
// Get all the paragraph styles in the document
var allParagraphStyles = myDocument.allParagraphStyles;
// Loop through each paragraph style
for (var i = 0; i < allParagraphStyles.length; i++) {
var style = allParagraphStyles[i];
// Set the search criteria for the findText() method
app.findTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = style;
// Search for instances of the style in the document
var found = myDocument.findText();
// If the style is not found in the document, add it to the list of unused styles
if (found.length == 0) {
unusedStyles.push("Paragraph style: " + style.name);
}
// Reset the findTextPreferences object
app.findTextPreferences = NothingEnum.nothing;
}
// Create a new text frame to hold the report text
var textFrame = myDocument.pages[0].textFrames.add();
textFrame.geometricBounds = [3, 18, 36, 3];
// Insert the list of unused styles into the text frame
for (var i = 0; i < unusedStyles.length; i++) {
textFrame.contents += unusedStyles[i] + "\n";
}