Script: Count number of occurrences of a style?

Community Beginner ,
Mar 13, 2021 Mar 13, 2021

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.

TOPICS
Print, Scripting

Views

415

Likes

translate

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
Adobe Community Professional ,
Mar 13, 2021 Mar 13, 2021

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)

Capture d’écran 2021-03-13 à 19.08.23.jpgCapture d’écran 2021-03-13 à 19.10.46.jpg

Likes

translate

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 Beginner ,
Mar 13, 2021 Mar 13, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
Adobe Community Professional ,
Mar 14, 2021 Mar 14, 2021

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

 

Likes

translate

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
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

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!

Likes

translate

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
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

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

Likes

translate

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
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

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.

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

@Peter Kahrel 

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.

Likes

translate

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
Adobe Community Professional ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

The line should have been

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

 

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

Thanks! That seems to work, after a quick test. Still no group though.

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

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?

Likes

translate

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
Adobe Community Professional ,
Mar 19, 2021 Mar 19, 2021

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

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

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?

Likes

translate

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
Adobe Community Professional ,
Mar 19, 2021 Mar 19, 2021

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 

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

Likes

translate

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 Beginner ,
Mar 19, 2021 Mar 19, 2021

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.)

Likes

translate

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
Adobe Community Professional ,
Mar 19, 2021 Mar 19, 2021

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

https://www.dropbox.com/s/1f6vwyoscxi8h75/screen%20recording%202021-03-20%20at%2010.02.47%20am.mov?d...

-Manan

Likes

translate

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 Beginner ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

The style is only used for index 'Number Style Override'.

Likes

translate

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
Adobe Community Professional ,
Mar 21, 2021 Mar 21, 2021

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

Likes

translate

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 Beginner ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Only the first name (the names in red) in each biography uses that style.

 

Skärmavbild 2021-03-22 kl. 13.03.32.png

Likes

translate

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