Skip to main content
Masoud Moghaddam
Inspiring
December 9, 2023
Answered

Remove unused styles from book documents at once.

  • December 9, 2023
  • 3 replies
  • 818 views

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

This topic has been closed for replies.
Correct answer Anantha Prabu G

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");

3 replies

Community Expert
December 10, 2023

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

https://creativepro.com/topic/how-to-remove-all-unused-paragraph-styles-of-an-indesign-document-through-javascript/

-Manan

-Manan
Barb Binder
Community Expert
Community Expert
December 9, 2023
~Barb at Rocky Mountain Training
Anantha Prabu G
Anantha Prabu GCorrect answer
Legend
December 9, 2023

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");
Design smarter, faster, and bolder with InDesign scripting.
Masoud Moghaddam
Inspiring
December 9, 2023

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.