Skip to main content
Known Participant
September 7, 2022
Question

validate paragraph style names based on list

  • September 7, 2022
  • 5 replies
  • 304 views

I am attempting to modify a script I found that checks if style names in a supplied array exist in the active document. I wish to turn this around and report the styles in the active document that are not in the supplied list. 

 

I suspect the second for loop in the script below is not doing what I want it to do. The notFoundStyles array lists all the styles in the list as well as the one style not in the list. 

 

Here is my current script:

 

var pStyles = app.activeDocument.paragraphStyles.everyItem().name
var foundStyles = Array();
var notFoundStyles = Array();
var styles = Array();

var styles = {
	CompanyName: "CompanyName",
	Footnotes: "Footnotes",
	Head_Author: "Head_Author",
	Head_Chapter: "Head_Chapter",
	Head_Title: "Head_Title",
	TOCList: "TOCList",
	Text_Body: "Text_Body",
	Text_Body_InitialCap: "Text_Body_InitialCap",
	Text_Copyright: "Text_Copyright",
	poem: "poem"
  }

for (var key in styles ) {
          if (pStyles.join().indexOf(styles[key]) != -1){
            foundStyles.push (styles[key]);
		}
   }

for (var myP in pStyles) {
         if ((pStyles[myP].indexOf) ((styles[key])) == -1){
            notFoundStyles.push (pStyles[myP]);
		}
   }

 {alert('Not Found: ' + notFoundStyles + '\nFound: ' + foundStyles)}

 

This topic has been closed for replies.

5 replies

Community Expert
September 9, 2022

Another note:

If the parent of style is the document you can tell that the style is in the root of the Paragraph Styles panel.

If not, the parent of that style is a paragraph style group.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Kasyan Servetsky
Legend
September 9, 2022

Here's my approach.

Peter Kahrel
Community Expert
Community Expert
September 9, 2022

Yet another approach, but a small change to your original script.

 

1. Change the first line to

var pStyles = app.activeDocument.allParagraphStyles;

This creates an array of all paragraph styles, no matter how deeply they are embedded in style groups.

Then replace your two for-loops with this one:

for (var key in styles) {
  if (styles[key])) {
    foundStyles.push (key);
  } else {
    notFoundStyles.push (key);
	}
}

Peter

Community Expert
September 8, 2022

I have taken a different approach to the problem. The idea is to try and add the paragraphstyle to the document if we crash while doing so then the style is used(what else can possibly cause the crash) and if it did not crash then the style is unsed. Try the following code.

/*
https://community.adobe.com/t5/indesign-discussions/validate-paragraph-style-names-based-on-list/td-p/13184430

Script to get get a create a collection of used and not used paragraph styles for the input style name collection
To search for styles in groups use / as the path seperator
Pre Requisite:- The script needs an open document
Warning :- No elaborate error checking

Author:- Manan Joshi
Guthub Repo :- https://github.com/Manan-Joshi/InDesign-Scripts.git
*/

function getPStyleGroup(nmCol){
    var pg = app.documents[0]
    for(var i = 0 ; i < nmCol.length - 1; i++){
        pg = pg.paragraphStyleGroups.itemByName(nmCol[i])
        if(!pg.isValid)
            return ""
    }
    return pg
}

function getUsedUnusedStyleFromList(inStyleList, outUsed, outUnused){
    var styleCol = app.documents[0].paragraphStyles
    for(var i = 0; i < inStyleList.length; i++){
        var styleCol = app.documents[0].paragraphStyles
        try{
            var grps = inStyleList[i].split("/")
            if(grps.length > 1)
                styleCol = getPStyleGroup(grps)

            if(styleCol === ""){
                outUnused.push(inStyleList[i])
                continue;
            }
            styleCol.add({name:grps[grps.length - 1]})
            app.documents[0].undo()
            outUnused.push(inStyleList[i])
        }catch(e){
            outUsed.push(inStyleList[i])   
        }
    }
}

var styleList = ["abc/def/PS1", "abc/PS2", "PS3","PS8"]
var used = [], unused = []
getUsedUnusedStyleFromList(styleList, used, unused)
alert("Unused " + unused + " Used " + used)

It works for styles in groups as well. To mention style in group seperate each path with a /

-Manan

-Manan
brian_p_dts
Community Expert
Community Expert
September 7, 2022

Are your styles in groups/folders? The top line only gets toplevel paragraph styles. Also, just take out the second loop and amend the first as so: 

 

for (var key in styles ) {
          if (pStyles.join().indexOf(styles[key]) != -1){
            foundStyles.push (styles[key]);
 
	  } else {
             notFoundStyles.push(styles[key]);
          }
   }

 

 

brian_p_dts
Community Expert
Community Expert
September 7, 2022

Lastly, fwiw, your object of  paragraph styles could just be an array of strings, but this works.