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

Remove unused styles from book documents at once.

Participant ,
Dec 09, 2023 Dec 09, 2023

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

TOPICS
How to

Views

270

Translate

Translate

Report

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

Votes

Translate

Translate
Engaged ,
Dec 09, 2023 Dec 09, 2023

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");
Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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