Highlighted

Generated index is NOT in alphabetical order

Community Beginner ,
Feb 06, 2019

Copy link to clipboard

Copied

Dear community,

To my surprise InDesign generates incorrect indexes. Se example below, every line starting with the surname “Dahl” should of course be kept together and be listed before any name with the same base/syllable. In this case before “Dahlbom”!

Skärmdump 2019-02-06 kl. 18.29.45.png

What to do?

Similar problem:

Wrong alphabetical order in index

I couldn't get @Peter Kahrels script to work in InDesign 14.0.1.

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

Solo -- InDesign's index sorting is indeed pretty awful much of the time, especially with name indexes. Please go to Adobe InDesign Feedback and tell Adobe about the problem and submit a request to improve index sorting (and tell them how).

In the meantime, your name sample can be sorted correctly by setting each topic's sort order to the topic's name with the separating comma+space replaced with any character that's naturally sorted before any letter. Digits do that, so let's use the zero:

if (app.documents[0].indexes.length == 0) exit();

topics = app.documents[0].indexes[0].allTopics;

for (i = topics.length-1; i >= 0; i--) {

  topics.sortOrder = topics.name.replace (', ', '0');

}

P.  (Thanks for the ping, BarbBinder​)

Views

743

Likes

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

Generated index is NOT in alphabetical order

Community Beginner ,
Feb 06, 2019

Copy link to clipboard

Copied

Dear community,

To my surprise InDesign generates incorrect indexes. Se example below, every line starting with the surname “Dahl” should of course be kept together and be listed before any name with the same base/syllable. In this case before “Dahlbom”!

Skärmdump 2019-02-06 kl. 18.29.45.png

What to do?

Similar problem:

Wrong alphabetical order in index

I couldn't get @Peter Kahrels script to work in InDesign 14.0.1.

Adobe Community Professional
Correct answer by Peter_Kahrel | Adobe Community Professional

Solo -- InDesign's index sorting is indeed pretty awful much of the time, especially with name indexes. Please go to Adobe InDesign Feedback and tell Adobe about the problem and submit a request to improve index sorting (and tell them how).

In the meantime, your name sample can be sorted correctly by setting each topic's sort order to the topic's name with the separating comma+space replaced with any character that's naturally sorted before any letter. Digits do that, so let's use the zero:

if (app.documents[0].indexes.length == 0) exit();

topics = app.documents[0].indexes[0].allTopics;

for (i = topics.length-1; i >= 0; i--) {

  topics.sortOrder = topics.name.replace (', ', '0');

}

P.  (Thanks for the ping, BarbBinder​)

Views

744

Likes

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
Adobe Community Professional ,
Feb 06, 2019

Copy link to clipboard

Copied

Hi Solo46,

Yes, and I remember another (different) post on this within the last few months with the same issue. Since Peter Kahrel has a script that addresses this, let's see if we can get him to swing by to help. 

~Barb

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 06, 2019

Copy link to clipboard

Copied

Solo -- InDesign's index sorting is indeed pretty awful much of the time, especially with name indexes. Please go to Adobe InDesign Feedback and tell Adobe about the problem and submit a request to improve index sorting (and tell them how).

In the meantime, your name sample can be sorted correctly by setting each topic's sort order to the topic's name with the separating comma+space replaced with any character that's naturally sorted before any letter. Digits do that, so let's use the zero:

if (app.documents[0].indexes.length == 0) exit();

topics = app.documents[0].indexes[0].allTopics;

for (i = topics.length-1; i >= 0; i--) {

  topics.sortOrder = topics.name.replace (', ', '0');

}

P.  (Thanks for the ping, BarbBinder​)

Likes

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
Reply
Loading...
Community Beginner ,
Feb 08, 2019

Copy link to clipboard

Copied

Thanks Peter and Barb!

Will that script edit the index data for just the active file?

Should the script file be named .JS or .JSX?

Just to see if I understand correctly. Will "name.replace (', ', '0')" change the sort order field from:

Smith, John

to

Smith0John

?

And one more detail. I have already used manual sorting for several topics by adding text to sort order field. It would be elegant if the script first checked if any data is present in sort order field. And if so, edits that string. If not, takes topic filed string and adds it to sort order field with comma space replaced with zero.

It seems like InDesign ignores punctuation marks when sorting. I.e. sort order field name "Smith0John" or "Smith,0John" gave the same result. So maybe the script can be edited to "name.replace (', ', ',0')".

Is it possible to add more strings to be replaced with the same replace command? Names that have no known first name in my index are instead separated with colon and then followed by title. In other words, could colon + space also be added to the replace command?

I am a bit anxious, because the index takes 30 min to generate, it contains over 7'000 lines/topics.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 08, 2019

Copy link to clipboard

Copied

> Will that script edit the index data for just the active file?

Yes.

> Should the script file be named .JS or .JSX?

Doesn't matter, both work.

> It would be elegant if the script first checked if any data is present in sort order field.

Well, you didn't mention that, I went by your sample and description. This changed code ignores topics that already have a sort field:

if (app.documents[0].indexes.length == 0) exit(); 

topics = app.documents[0].indexes[0].allTopics; 

for (i = topics.length-1; i >= 0; i--) {

  if (topics.sortOrder == '') {

    topics.sortOrder = topics.name.replace (/[,:]\s*/, '0');

  }

}

> And if so, edits that string.

Edits -- how?

> If not, takes topic field string and adds it to sort order field with comma space replaced with zero.

That's not possible. You can have only one sort-order string.

> Is it possible to add more strings to be replaced with the same replace command?

You can now add characters. In line 5 you see [,:] and you can add characters to that list in brackets.

> I am a bit anxious, because the index takes 30 min to generate, it contains over 7,000 lines/topics.

Try it on a small sample.

P.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 08, 2019

Copy link to clipboard

Copied

Thanks Peter!

Now I see that the replace function uses regex, great. I am familiar with regex, but not so good at writing JS.

> Edits -- how?

If a sort order field string already is present, then the script should prioritise sort order field string over topic field and replace the comma/colon and space with zero for sort order field string. And if sort order field string is empty the script should take the data from topic filed and replace comma/colon and space with zero and add it to sort order field (just as before).

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 09, 2019

Copy link to clipboard

Copied

Was it 'Duh!' or 'Doh!' ?

 

(function () {
	if (app.documents.length === 0 || app.documents[0].indexes.length == 0) {
		exit();
	}
	var re = /[,:]\s*/g;
	var topics = app.documents[0].indexes[0].allTopics;
	for (var i = topics.length-1; i >= 0; i--) {
		if (topics[i].sortOrder == '') {
			topics[i].sortOrder = topics[i].name.replace (re, '0');
		} else {
			topics[i].sortOrder = topics[i].sortOrder.replace (re, '0');
		}
	}
}());

 

P.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

Thank you so much for the new version with sortOrder check!
This is how InDesign should sort names alphabetically from the start.

I've tried you script on some test documents and it seems to solve the problem. I will be working with the index more during the week.

When cleaning up the index I found some unused topics and your excellent Index statistics script could count them. But is it possible to add a listing of all unused topics? That would be very useful as it takes quite a long time to find the unused topics. Maybe another script could delete all unused topics, or with a checkbox in the stat. script?

Out of curiosity:
I noticed that you used "i--", is it better to step backwards when scripting indexes in InDesign?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 11, 2019

Copy link to clipboard

Copied

Yes, a list of unused topics can be added. Looking through the code I notice that the script marks as unused a topic that is used as a cross-reference, that needs to be changed.

As to removing unused topics, doesn't InDesign's function work for you? It's in the Index panel's fly-out menu.

I used i-- because at some stage I got weird results iterating forward. But in addition, stepping backwards is usually quicker.

P.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

Great.

Looking through the code I notice that the script marks as unused a topic that is used as a cross-reference, that needs to be changed.

That explains the contradictory and odd results I've got when working with the index stat script. It would be fantastic if that could be fixed.

Re: removing unused topics.

Haha, yes the fly-out menu works fine. I forgot it and was thinking about JS. When your only tool is a hammer …

Edit: No, the fly out menu remove unused topics is greyed out even when there are unused topics present. But show unused topics in the same fly out panel was handy. Although not as handy as a condensed list with only unused topics.

Thanks for explaining about iteration.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 11, 2019

Copy link to clipboard

Copied

> the fly out menu remove unused topics is greyed out even when there are unused topics present.

Select any topic and 'remove unused topics' is available.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

Select any topic and 'remove unused topics' is available.

Thanks, yes that solved it. Odd interaction for such well made software, since there is only one index for each file.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 11, 2019

Copy link to clipboard

Copied

> But is it possible to add a listing of all unused topics?

I added a button to the window so that you can print the unused topics to a file. The file is saved on your desktop and opened.

P.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 12, 2019

Copy link to clipboard

Copied

Thanks. This will be very useful when troubleshooting.

> I added a button to the window so that you can print the unused topics to a file. The file is saved on your desktop and opened.

Superb. But I couldn't find it. Did you upload it to your site?

http://www.kahrel.plus.com/indesign/index_statistics.html

> the script marks as unused a topic that is used as a cross-reference

Did you look into this also?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Feb 12, 2019

Copy link to clipboard

Copied

> But I couldn't find it. Did you upload it to your site?

I didn't, but now I did. Sorry.

> Did you look into this also?

Yes, I did. If a topic is used only as a reference topic, it's not considered unused (as InDesign does and the script did earlier).

P.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 12, 2019

Copy link to clipboard

Copied

Great news Peter. Thanks. Your script is an elegant solution for working with large indexes!

I tested it on a large document and the new version seems to calculate correct.

I really appreciate your help in this subject.
And it's a bit of a mystery to me that these tools are note available as standard tools in InDesign 2019.

Likes

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
Reply
Loading...
Community Beginner ,
Feb 14, 2019

Copy link to clipboard

Copied

Well well, as usual it's more to this.

There where more special cases ahead so it's probably better to make a script that solves "all" index sorting problems. Previously I had some manual edited sortOrders (especially for W/V) that I wished to keep. Hence the previous care taken of topics already with sortOrder present, but it's much easier to accomplish it all in a script.

But I am beginner at scripting. My script draft only results in line 7 being noticeable affected in the document. And my addition to the script can probably be much more elegantly done.

Something is wrong, is topics.sortOrder is been overwritten for each line? A new variable might be needed or all regexes might be performed during a single replace call? But I don't know how it should be done.

if (app.documents[0].indexes.length == 0) exit(); 

topics = app.documents[0].indexes[0].allTopics; 

for (i = topics.length-1; i >= 0; i--) { 

  topics.sortOrder = topics.name.replace      (/(?-i)W/, 'V');  // Fix Swedish alphabetical order. Treat W as V

  topics.sortOrder = topics.sortOrder.replace (/(?-i)w/, 'v');  // Fix Swedish alphabetical order. Treat w as v

  topics.sortOrder = topics.sortOrder.replace (/['´]/, '');     // Exclude punctuation that InDesign sorts as normal characters

  topics.sortOrder = topics.sortOrder.replace (/[,:]\s*/, '0'); // Fix alphabetical order for names, in format "Smith, John"

And this reported as error 21: undefined is not an object.

if (app.documents[0].indexes.length == 0) exit(); 

topics = app.documents[0].indexes[0].allTopics; 

var myIndex;

for (i = topics.length-1; i >= 0; i--) { 

  myIndex = topics.name;

 

  myIndex.replace (/(?-i)W/, 'V');  // Fix Swedish alphabetical order. Treat W as V

  myIndex.replace (/(?-i)w/, 'v');  // Fix Swedish alphabetical order. Treat w as v

  myIndex.replace (/['\´]/, '');    // Exclude punctuation that InDesign sorts as normal characters

  myIndex.replace (/[,:]\s*/, '0'); // Fix alphabetical order for names, in format "Smith, John"

 

  topics.sortOrder = myIndex;

}

Likes

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
Reply
Loading...
Community Beginner ,
Feb 14, 2019

Copy link to clipboard

Copied

Ok, I've been reading and testing.
This seems to work.

if (app.documents[0].indexes.length == 0) exit(); 

topics = app.documents[0].indexes[0].allTopics; 

for (i = topics.length-1; i >= 0; i--) { 

  topics.sortOrder = topics.name.replace (/W/g, 'V')     

                                      .replace (/w/g, 'v')     

                                      .replace (/[\'\´]/g, '') 

                                      .replace (/[,:]\s*/, '0');

}

Any feedback?

It can probably be done in some much more clever way.

Likes

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
Reply
Loading...
New Here ,
Mar 14, 2020

Copy link to clipboard

Copied

Hello,

May be a little late but I am working already for some years on a book using Indesign CS 6 (about 800 pages). My index counts about 4000 entries and that all is still functioning. But the alphabetical order is - as we know - totally wrong as soon as spaces and "," are involved.

Example:

Braune, Werner (1909-1951)

Braun, Magnus Freiherr von (1878-1972)

Braun, Otto (1872-1955)

It shoud be:

Braun, Magnus Freiherr von (1878-1972)

Braun, Otto (1872-1955)

Braune, Werner (1909-1951)

There is a script on this site. But I know nothing about scripts ... is someone able to give me a "turn key" script I only have to paste in the scripts-menu. I can just handle that :(.

I am willing to give a few money for someones efforts. The script must be able to "eat" the 4000 entries.

Thanks, Peter.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Mar 17, 2020

Copy link to clipboard

Copied

Hi together,

in the meanwhile Peter's scripts moved to creativepro.com. So the updated script can be found at:

https://creativepro.com/files/kahrel/indesign/index_statistics.html

 

Regards,
Uwe Laubender

( ACP )

 

FWIW: No idea why this thread moved to the top of the list of InDesign discussions some days ago.

Seems, this all entirely took place in February 2019.

Likes

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
Reply
Loading...