Skip to main content
Known Participant
March 13, 2021
Question

Script: Count number of occurrences of a style?

  • March 13, 2021
  • 3 replies
  • 2088 views

How can I count the number of occurrences of a specific style? Primarily for character styles, but I will also need it for paragraph styles. Ideally for active document and also as a choice for all chapters/files in a book.

 

I guess a script is required for that, but I am not that familiar with JS scritping.

 

Any help is very welcome.

This topic has been closed for replies.

3 replies

Community Expert
March 19, 2021

To cater to groups as well, try the following code. It will show a drop-down with all the names of characterstyles in the document, you can choose one and then the occurrences of that style would be searched

 

var list = []
for(var i = 0; i < app.documents[0].allCharacterStyles.length; i++)
	list.push(app.documents[0].allCharacterStyles[i].name)
var dialog = app.dialogs.add({name:"Choose the Character Style", canCancel:false})
var column = dialog.dialogColumns.add()
var row = column.dialogRows.add()
row.staticTexts.add({staticLabel:"Character Style:"});
var dd = row.dropdowns.add({stringList:list, selectedIndex:0});
dialog.show();

app.findTextPreferences.appliedCharacterStyle = app.documents[0].allCharacterStyles[dd.selectedIndex]
var a = app.documents[0].findText()
alert(a.length)
app.findTextPreferences = null

 

-Manan

-Manan
Solo46Author
Known Participant
March 19, 2021

Wow, now we are getting close. Thanks!

The new script didn't report any errors but most styles where not counted correctly. Most styles just counted as "0".

Is it possbile to display the group name in the list as well?

Community Expert
March 19, 2021

I tested and it worked fine for me, can you send me a document with an example of the wrong count? It may be the case that styles within different groups have the same name and you choose the entry that corresponds to the wrong style(i.e. it is not the one you are trying to search).

-Manan 

-Manan
Community Expert
March 14, 2021

Try the following script to check the count of characterstyle named Test, change the name according to your document

app.findTextPreferences.appliedCharacterStyle = "Test"
var a = app.documents[0].findText()
alert(a.length)
app.findTextPreferences = null

for paragraphstyle change appliedCharacterStyle to appliedParagraphStyle in the code

-Manan

 

-Manan
Peter Kahrel
Community Expert
Community Expert
March 15, 2021

jmlevy's trick can be simplified: simply replace character style 'a' with character style 'a'.

 

But I really would like to count without using a trick

 

Why? And is a JavaScript not a trick?

 

If you use Manan's trick on a Mac, remember that you must set the style as an object, not as a string:

 

app.findTextPreferences.appliedCharacterStyle = app.characterStyles.item ("Test")

 

And if the style is in a group, you have to include the style group's name.

 

I'd go for jmlevy's trick!

Community Expert
March 15, 2021

Hi @Peter Kahrel, I just retested the code and I can verify that using just the string name of the style also works fine on a MAC. The warning for grouped styles though does stand, I did not cater to it to keep the example simple.

-Manan

-Manan
jmlevy
Community Expert
Community Expert
March 13, 2021

You could run a find-change:

find what: your style name

change: another style or leave empty

InDesign will change all and will display the number of changes. After that, undo (better to do it on a copy of the file)

Solo46Author
Known Participant
March 13, 2021

Thanks! Yes, I am aware of that trick. But I really would like to count without using a trick 😄