2

I use Extendscript to help me review students' work in InDesign. I run a script that goes through a document and creates a report. One of the things I need to list on that report is wether or nor unused paragraph styles exist, preferably also listing their names.

I tried:

  • Searching the documentation for a property that might indicate if the paragraph style is being used or not.
  • Invoking (.invoke() method) the Select All Unused action from the Paragraph Styles panel. I explored adding event listeners and looking for any results, and also exploring the Panel documentation to check for a selection. According to an older topic here - InDesign scripting, get the items selected in a panel -, this is not possible.

I also considered looping through all stories and paragraphs, checking for the styles in use, and accounting for styles used inside other styles. However, I feel there should be a simpler alternative.

EDIT: The point is to avoid manual, tedious and error-prone work for about 5 exercises x 60 students per semester. The report already includes many other things, like page size, bleed, margins, parent page columns and application, text style options, baseline grid, etc. This script saves me a HUGE amount of time and makes it less likely that I'll forget to check for anything in particular. That's why I'm trying to integrate as many features as possible into it, so the individual manual work is reduced to the absolute minimum.

maguskrool
  • 57
  • 4
  • 1
    Try finding the "name" of the "applied character style" of "text style ranges" of all "stories" of the "document" then dedupe those values with a function like this: https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates Then repeat for the name of applied paragraph styles – user1754036 Dec 27 '22 at 13:08
  • 1
    Thank you, I didn't know about the "text style ranges", that might prove helpful. However, I would still have to loop through all stories, and would also have to include the style definitions. If a style is not applied on a paragraph/character, but is used in the "based on" or "next style", or character style for a bulleted list, etc., then it still counts as being used. – maguskrool Dec 27 '22 at 22:56

1 Answers1

1

After choosing "Select All Unused" from the Paragraph Styles panel, you should be able to click the trash can icon to delete those styles. The same should work for the Character Styles panel.

I wondered if ChatGPT was smart enough to figure this out. It helped, but couldn't deliver a full solution. We went through 10 different versions before getting close to the working version of the script below.

Here's my final version:

    var myDocument = app.activeDocument;
    
    // Get all the character styles in the document
    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;
    }

And here's a version that generates a report showing a list of unused paragraph styles. Again, ChatGPT can help with some, but it couldn't produce a final version. My edited and tested script appears below.

var myDocument = app.activeDocument;

// Create an array to store the names of unused styles
var unusedStyles = [];

// 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 for instances of the style in the document
  var found = myDocument.findText();
  // If the style is not found in the document, add it to the list of unused styles
  if (found.length == 0) {
    unusedStyles.push("Paragraph style: " + style.name);
  }
  // Reset the findTextPreferences object
  app.findTextPreferences = NothingEnum.nothing;
}

// Create a new text frame to hold the report text
var textFrame = myDocument.pages[0].textFrames.add();
textFrame.geometricBounds = [3, 18, 36, 3];

// Insert the list of unused styles into the text frame
for (var i = 0; i < unusedStyles.length; i++) {
  textFrame.contents += unusedStyles[i] + "\n";
}
user1754036
  • 396
  • 1
  • 6
  • 2
    https://stackoverflow.com/help/gpt-policy – Robert Longson Dec 31 '22 at 08:32
  • 1
    Thank you. My objective is to do everything through the script and just list unused styles, not remove them. I guess that right now the simplest solution is like the one you provided. I'll need to adapt it so it doesn't list styles that might not be applied, but that are parent styles. I think perhaps the best way is to first loop through all styles and check if they are based on others. If not, I'll add them to what would be the allParagraphStyles array, and then use that on your loop. – maguskrool Jan 02 '23 at 22:43
  • 1
    Oops! You are correct. I had the "based on" styles criteria working at some point but lost it somewhere along the line and wasn't testing for it in the end. – user1754036 Jan 03 '23 at 22:34