Copy link to clipboard
Copied
Hello!
I find a fantastic script here:
How to find/change/delete index entries text in documents
to find and change index entries, but unfortunate my Indesign document have 4 levels of entries, and this script is only working in the first level.
Please, Do you know how can I find and replace for all levels?
#target "indesign";
(function () {
var doc = app.activeDocument,
indexIdx = doc.indexes.length - 1;
function removeNewEntriesFromIndex(index) {
var idx = index.topics.length - 1;
for (; idx >= 0; idx -= 1) {
// index.topics[idx].name = index.topics[idx].name.replace(/ *NEW$/, '');
index.topics[idx].name = index.topics[idx].name.replace(/ *Article 1.$/, 'Article One');
index.topics[idx].name = index.topics[idx].name.replace(/ *rights$/, 'human rights');
}
}
for (; indexIdx >= 0; indexIdx -= 1) {
removeNewEntriesFromIndex(doc.indexes[indexIdx]);
}
}());
Is important change exactly the correct word , for example In my document are many times the word "rights", but I only want change the "rights" placed in the four level:
Article 1. > persons > free > rights
Like you can see in the picture:
Thanks so much!
You've probably either figured this out or moved on by now, but I was having the same issue and came up with a fix, so I figured I'd share it in case anyone else needed it
...#target "indesign";
(function () {
var doc = app.activeDocument,
indexIdx = doc.indexes.length - 1;
function removeNewEntriesFromIndex(index) {
var idx = index.topics.length - 1;
topics = app.documents[0].indexes[0].allTopics;
for (idx = topic
Copy link to clipboard
Copied
You've probably either figured this out or moved on by now, but I was having the same issue and came up with a fix, so I figured I'd share it in case anyone else needed it
#target "indesign";
(function () {
var doc = app.activeDocument,
indexIdx = doc.indexes.length - 1;
function removeNewEntriesFromIndex(index) {
var idx = index.topics.length - 1;
topics = app.documents[0].indexes[0].allTopics;
for (idx = topics.length-1; idx >= 0; idx--) {
// index.topics[idx].name = index.topics[idx].name.replace(/ *NEW$/, '');
index.topics[idx].name = index.topics[idx].name.replace(/ *Article 1.$/, 'Article One');
index.topics[idx].name = index.topics[idx].name.replace(/ *rights$/, 'human rights');
}
}
for (; indexIdx >= 0; indexIdx -= 1) {
removeNewEntriesFromIndex(doc.indexes[indexIdx]);
}
}());
Copy link to clipboard
Copied
Hi ChocoBeast,
thanks for your script is just I was looking for.
I appreciatte, thanks for share!
Copy link to clipboard
Copied
Unfortunately this doesn't seem to be a "bullet-proof" solution - I think you were lucky 🙂 with your particular index.
It turns out that InDesign re-sorts the index entries (= the topics) instantly in the background as soon as you change their "name" ... and with this two things can and will happen:
1. You won't catch each index entry if you need to change one of the first letters - no matter if you iterate back or forth through the array "topics" or even "allTopics";
2. You can and will get an error message telling you about an invalid object which is no longer in place.
The real issue is the instant re-sorting done by InDesign - so far I haven't found a way to suppress that. It might be worth to investigate the following tracks:
a) completely duplicate the array "topics" (or "allTopics"), do the alterations there and then declare the new array the new index;
b) iterating multiple times through the topics/allTopics array;
c) iterate recursively through all four levels of the index
At the moment, I doubt if any of these could lead to a "bullet-proof" solution.
Copy link to clipboard
Copied
The API says "indexes - a collection of indexes" and offers a method "add" ... however, it seems that only one index is allowed. You can't add a second index behind the scenes - which might make a little sense because in the front end there is in fact only one index (in opposite to TOCs). However, this would have been a nice approach: to add a second index, copy all topics with changed names into it and then remove the first index, making the second the actual index.
Maybe it works with a "standard" array in which you can push all topics first ... might be worth to try.
Anyway, just looping through "topics" (only first level!) or "allTopics" (all 4 levels), changing and resaving names, will NOT bring the intended result.
Copy link to clipboard
Copied
I found a nice solution but still have to investigate whether this is really the solution.
What you have to do is to prevent the index from being re-sorted instantly after changing a topic's name. That can be done by first applying a specific "sortOrder" to each topic. Ideally you'd use the topic's name, complemented by something that you can easily find afterwards and delete this special sortOrder entry later on. For example something like:
for ( var i = 0; i < myIndex.allTopics.length; ++i ) {
// first replace each (empty) sortOrder by the topic's name complemented by some silly stuff:
if ( myIndex.allTopics[i].sortOrder == "" ) {
myIndex.allTopics[i].sortOrder = myIndex.allTopics[i].name + '####';
}
// now you can change the name as you like:
myIndex.allTopics[i].name = myIndex.allTopics[i].name.replace(/[aeiou]/gi, 'xyz');
}
... and finally, you just have to delete those sortOrder entries which contain those trailing "####".
Copy link to clipboard
Copied
Here's now the way along which you can change your index entries to literally anything:
1. Loop through "allTopics" from back to forth:
1a. save the allTopics[i].name into a variable X
1b. If there is no allTopics[i].sortOrder yet then set allTopics[i].sortOrder to allTopics[i].name + "####"
1c. replace whatever you need to replace within the variable X
1d. set allTopics[i].name to the variable X
The tricky part is to reset the sort order because InDesign resorts - and re-indexes! - the index array instantly after changing each sort order.
2. Loop through "allTopics" from back to forth:
2a. delete each allTopics[i].sortOrder if it contains "####"
However, there can arise a lot of situations where even this approach still brings "At least one index entry contains illegal characters ..." - I found, for example, that blank characters imported from a MS Word index are sometimes considered "illegal" ... so I replace all blanks by another character and re-replace it later on. Very strange, and unfortunately no real documentation from Adobe at this point ... like, for example: "Which characters are ever considered 'illegal' by the Index function"? Only in the property "name" or also in the property "sortOrder"? And more - hence you need to perform time-consuming, cumbersome investigations and are still unsure if you're doing the right thing. Not funny.
Good luck!
Copy link to clipboard
Copied
It's a very old thread! (4 years)
About this comment: "however, it seems that only one index is allowed."
… I let you take a look at this recent video on Youtube:
Best,
(^/) The Jedi
Copy link to clipboard
Copied
Nice to see, thanks for sharing.
I'm using InDesign 18.4 on Win11 hence I think I'm up-to-date 🙂
No doubt that you're showing a plug-in - which is still in a beta phase. I'm talking about standard InDesign scripting. Where trying to add a second index fails. Unless you can provide a script command for it - I didn't succeed. However, I wouldn't say that I'm a scripting wizard ... and I'm eager to learn everyday from the Pros.
Copy link to clipboard
Copied
Indeed InDesign documents can contain just one index. The YouTube video shows a script, it's not a plug-in. Looks pretty nifty, but you'll probably have to buy it. Since, as you say, it's in beta, maybe offer to test it for free 🙂
Alternatively, take a look at two suggestions here:
https://creativepro.com/files/kahrel/indesign/index-fixes-multiple-indexes.html
P.
Copy link to clipboard
Copied
As @Peter Kahrel suggested - you just need to sacrifice 1st level.
Copy link to clipboard
Copied
All this sounds like a wonderful work-around... my lame questions is, Why the hell does something so simple and basic require it? Why is it not PART of the S&R function???
Copy link to clipboard
Copied
Well, suggest it to Adobe: https://indesign.uservoice.com