Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Thanks! Yes, I am aware of that trick. But I really would like to count without using a trick 😄
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Aha, that has changed in some recent version of InDesign. It used to be the case that strings did not work. But it's always safer to use objects, not strings.
Copy link to clipboard
Copied
I don't see scripts as a trick, more as an extension of the software.
Thanks for the heads up. But when I repleaced the first line in Manan's script with your line it threw an error: appliedCharacterStyle error code 30477.
Copy link to clipboard
Copied
The line should have been
app.findTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item ("Test")
Copy link to clipboard
Copied
Thanks! That seems to work, after a quick test. Still no group though.
Copy link to clipboard
Copied
Thanks! That worked on macOS.
But most of my styles are located in folders. How do I specify that in the script?
Is it possiblt that the script shows a list of available styles. And can take one as input for the counting?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I made a reduced version of my document:
https://drive.google.com/file/d/1nOSlsqFZgAEZTj7ludlRONCMJCtBhXnr/view?usp=sharing
Copy link to clipboard
Copied
The style name I am primarly interested of is: "Index" > "Index huvudsida"
(The edit button on Adobes forum doesn't show.)
Copy link to clipboard
Copied
I tested the document you sent and it does not seem to have any text having this style applied. I tested with the native Find/Change dialog to demonstrate this, see the screengrab of the test at the following link
-Manan
Copy link to clipboard
Copied
The style is only used for index 'Number Style Override'.
Copy link to clipboard
Copied
I can't find that in the document, can you point that out via a screenshot of the demo document
-Manan
Copy link to clipboard
Copied
Only the first name (the names in red) in each biography uses that style.