Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Remove unused styles from book documents at once.

Participant ,
Dec 09, 2023 Dec 09, 2023

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

TOPICS
How to
535
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Dec 09, 2023 Dec 09, 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 
...
Translate
Engaged ,
Dec 09, 2023 Dec 09, 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");
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 09, 2023 Dec 09, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 09, 2023 Dec 09, 2023

"Excellent work, Anantha! I've tested it, and it worked perfectly. It removed unnecessary styles, and now both panels are clean.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2023 Dec 09, 2023
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2023 Dec 10, 2023
LATEST

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-thro...

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines