Skip to main content
Inspiring
August 11, 2024
Answered

Script to delete all unused styles (object, paragraph and text) in all open documents?

  • August 11, 2024
  • 1 reply
  • 1810 views

I need to clean up my InDesign book filen and one think I would like to do is to delete all unused styles (object, paragraph and text) in all open documents (I will ensure the document which I use as a style master will be closed, then I will synchronise all documents)?

This is my idea how to clean up and also deal with figuring out which of the document still use styles which have not been grouped and I will then address these styles using the replace function.

I assume it is possible to write a script doing this, but am new to scripte in InDesign (use actions and scrips more frequently in Photoshop), so would be super happy if I can get som guidance in this.

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

Many thanks! This will be good practise 🙂

When trying to sort this out I also asked ChatGPT, who, after a number of iterations, wrote this script.
Cleaning up this doc takes so much longer than I had hoped. I now have to manually map each and every one of the 25 chapter files I have (I am seriuosly considering skipping the grouping of the styles in InDesign - all style names should by now be identical in the 25 word files (as they are all part of the same master document and using the same template.). I would have loved it if there was a setting at the InDesign import asking it to automatically map with styles of the identical name, ignoring if it is in a group (if there are multiple styles with identical names, in different groups, InDesign could ask me which one to pick in a pop.up)).
🙂
Anyway, I am learning loads and think that the end document will look very neat.

Again, many thanks!

(here is the ChatGPT script:

function deleteUnusedStyles(doc) {
// Activate the document
app.activeDocument = doc;

// Delete unused Paragraph Styles in all Open Documents
try {
app.menuActions.itemByID(8614).invoke(); // Select All Unused in Paragraph Styles
app.menuActions.itemByID(8536).invoke(); // Delete Selected Styles in Paragraph Styles
} catch (e) {
$.writeln("Error removing unused Paragraph Styles: " + e);
}

// Delete unused Character Styles
try {
app.menuActions.itemByID(8609).invoke(); // Select All Unused in Character Styles
app.menuActions.itemByID(8536).invoke(); // Delete Selected Styles in Character Styles
} catch (e) {
$.writeln("Error removing unused Character Styles: " + e);
}

// Delete unused Object Styles
try {
app.menuActions.itemByID(8619).invoke(); // Select All Unused in Object Styles
app.menuActions.itemByID(8536).invoke(); // Delete Selected Styles in Object Styles
} catch (e) {
$.writeln("Error removing unused Object Styles: " + e);
}
}

function main() {
var docs = app.documents;
for (var i = 0; i < docs.length; i++) {
deleteUnusedStyles(docs[i]);
}
}

// Run the script
main();

)


Almost - ChatGPT still doesn't have all the human experience. 

 

You should change the order - CharStyle can be used in ParaStyle - so won't be "unused" if you don't remove ParaStyle first - and the same goes for ObjectStyles that can use ParaStyles. 

 

So ObjectStyles 1st, then ParaStyles, then CharStyles. 

 

Then there is a case of Table and Cell Styles - unless you don't have any in your documents. 

 

1 reply

Robert at ID-Tasker
Legend
August 11, 2024

If you don't have too many documents to process - and you don't care about styles / colors that will be deleted - then you can move pages to a new document - make sure that the size of the pages is the same.

 

It will save you time on deleting each kind of styles one-by-one. 

 

Or if you work on a PC - my ID-Tasker (paid version) can process all files for you - delete unused. 

 

The free version will let you browse and compare styles structure - including BasedOn tree view - but won't process multiple files. 

 

Inspiring
August 11, 2024

Thanks! As this is an exercise that I may want to repeat from time to time (as I am exploring the Book feature I tend to mess the files up a bit so they will need cleaning to and fro) so will definitely explore you ID-Tasker.

Robert at ID-Tasker
Legend
August 11, 2024

ID-Tasker will replicate anything you can click in InDesign - and a lot more. 

Or you can use free version to just browse document's structure - and quickly access any element in the document and execute simple Tasks. 

 

But if you would like to expand your scripting knowledge of InDesign, here is a recipe how to achieve your goal:

1. You would need to either process all files in the specified folder - or iterate through Documents collection of the Book: 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Book.html

Documents are not available directly - so you would need to extract fullName from the BookContents collection:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#BookContent.html#d1e61925

 

2. Then, for each opened Document - execute invoke() twice for each Style and Swatches - first with the ID of "Select All Unused" then "Delete Styles". There are scripts to get the list of all IDs:

http://kasyan.ho.ua/tips/indesign_script/all/open_menu_item.html

 

Of course, you need to close & save your INDD document after deleting styles - to release system resources, not to mention, save the changes. 

 

I'm not JS guy and replying from my phone so can't give you working code - but if you can script Photoshop - InDesign isn't so much different.