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

Automated Removal of Unused Swatches in Book File and Documents

  • December 10, 2023
  • 2 replies
  • 837 views

Hello,

 

I am working with a book file that contains 300 documents. I am looking for a script that can automatically clean up the Swatches panel for each document and remove any unused swatches. Is there a script available to handle this task without the need for manual intervention?

 

Thank you.

This topic has been closed for replies.
Correct answer Anantha Prabu G
Check this 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 id, sw;
 
    while (myDocument.unusedSwatches[0].name != "") {
 
            id = myDocument.unusedSwatches[0].id;
 
            sw = myDocument.swatches.itemByID(id);
 
            sw.remove();
 
 }   
}
}
}
 
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");

2 replies

Anantha Prabu G
Anantha Prabu GCorrect answer
Legend
December 20, 2023
Check this 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 id, sw;
 
    while (myDocument.unusedSwatches[0].name != "") {
 
            id = myDocument.unusedSwatches[0].id;
 
            sw = myDocument.swatches.itemByID(id);
 
            sw.remove();
 
 }   
}
}
}
 
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 20, 2023

Great Job Anantha, thank you!

Kasyan Servetsky
Legend
December 11, 2023

Check out the Delete unused swatches in the Clean up scripts section here. You can run it from the batch processor.

Masoud Moghaddam
Inspiring
December 11, 2023

Hi Kasyan,

 

I executed the batch processor while simultaneously browsing through the "unused swatches" script in the script folder of Adobe InDesign. After 40 minutes, I received the completion message and felt satisfied. However, upon opening a few documents by accident, I noticed that unused swatches were still present, and the script job hadn't deleted them. I'm using InDesign 2019, and I'm wondering if the script might not be compatible with this version and might require a more recent update.