Copy link to clipboard
Copied
Hi,
I have a book file in InDesign 2020 containing around 200 documents. Removing certain character and paragraph styles requires selecting and removing them individually in each document, which is a time-consuming and tedious process. Is there a quick way to identify and remove unused character and paragraph styles for all documents in the book?
I am also interested in identifying and removing unnecessary colors and swatches. Could you provide information on this as well?
Thanks in advance
Please check below code:
var myFileLocation = Folder.selectDialog("Please select path to files"); // file path selection
var myFolder = new Folder(myFileLocation);
var myFolderContents = myFolder.getFiles("*.indb"); // array
var myFileAmount = myFolderContents.length;
// Check if any .indb files were found
if (myFileAmount === 0) {
alert("No INDB files found in the selected folder.");
} else {
// Loop through the INDB files
for (var i = 0; i < myFileAmount; i++) {
var inFile
...
Copy link to clipboard
Copied
Please check below code:
var myFileLocation = Folder.selectDialog("Please select path to files"); // file path selection
var myFolder = new Folder(myFileLocation);
var myFolderContents = myFolder.getFiles("*.indb"); // array
var myFileAmount = myFolderContents.length;
// Check if any .indb files were found
if (myFileAmount === 0) {
alert("No INDB files found in the selected folder.");
} else {
// Loop through the INDB files
for (var i = 0; i < myFileAmount; i++) {
var inFile = myFolderContents[i];
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
// Open the INDB file
var doc = app.open(inFile);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
// Access the book contents
var bookContents = doc.bookContents;
// Loop through the book contents (individual documents in the book)
for (var j = 0; j < bookContents.length; j++) {
var bookItem = bookContents[j];
var tempDoc = app.open(bookContents[j].fullName);
var myDocument = app.activeDocument;
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;
}
}
}
}
var documents = app.documents;
for (var i = documents.length - 1; i >= 0; i--) {
documents[i].close(SaveOptions.YES);
}
app.activeBook.close(SaveOptions.YES);
alert("Done");
Copy link to clipboard
Copied
How should I utilize this code? Where do I insert it, and how can I execute it to observe the results? I apologize for my lack of experience with coding.
Copy link to clipboard
Copied
"Excellent work, Anantha! I've tested it, and it worked perfectly. It removed unnecessary styles, and now both panels are clean.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
if the script is to be run on InDesign desktop only then you can use the approach of using the menu options to delete the unused styles, swatches etc. The approach used in the current script would be very slow for documents with large amount of styles. Check out the following discussion about what I am saying
-Manan