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

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

Contributor ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

402

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

Community Expert , Aug 11, 2024 Aug 11, 2024

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. 

 

Votes

Translate

Translate
Community Expert ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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. 

 

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
Contributor ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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.

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 ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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. 

 

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 ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

Or you could go the "move pages" route - which would have a big benefit - House Keeping - it will remove ALL unused things from your document and rewrite the file completely, potentially getting rid of all the gremlins in the document.

 

And you would have a backup copy of your Book - as long as you'll be doing Save As with a new name for each INDD Document. Or you can overwrite your current document - as long as you'll be working on a copy - just in case. 

 

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 ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

Forgot to mention - you would have to use itemByRange() method to "select" all pages and move them all at once - not one-by-one. 

 

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
Contributor ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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

)

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 ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

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. 

 

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
Contributor ,
Aug 11, 2024 Aug 11, 2024

Copy link to clipboard

Copied

😊 Many thanks! Yes, I noticed something was not quite right with the ChatGPT script, but couldn't really understand what - your input was a great help.

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
Contributor ,
Aug 22, 2024 Aug 22, 2024

Copy link to clipboard

Copied

LATEST

It seems as the script does not manage to capture unused styles that are within groups (it works when I manually select "Select All Unused" in the respective Style menue). Is there a way to ensure that also any unused styles within groups are deleted with the script?

 

 

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

 

// 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);
}
// 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);
}
}

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

// Run the script
main();

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