Skip to main content
ali u
Inspiring
Dec 18, 2024 7:23 AM UTC
Answered

Generating Specific Parts of an Index in InDesign

Hi everyone,

I've grouped my index entries under several topics in InDesign. For example, I have a "*People" topic that includes various name entries and a "*Books" topic that contains book name entries.

Is there a way to generate only the "*People" or "*Books" sections of the index, rather than the entire index?

Thanks in advance for your help!

This topic is closed to new replies. Start a new post to keep the conversation going.
ali u
ali uCorrect answer
Inspiring
Dec 19, 2024 6:47 AM UTC

Thank you for your response. I have a file where the entire index spans about 360 pages across 6 categories. I was hoping there might be a way to generate each category individually to make the process more manageable.
I managed to achive this, kind of, with this crude script but I have a feeling that I shouldn't be doing it this way, what do you think?

// Define the topic you want to filter (e.g., "*People" or "*Books").
var targetTopic = "*People"; // Change to "*Books" or any other topic as needed.

try {
    var doc = app.activeDocument;
    var index = doc.indexes[0];
    
    // Array to store entries with page numbers
    var entriesArray = [];

    // Function to collect all subtopic entries under a topic
    function collectEntries(topic) {
        // Add entries for the current topic
        var entries = topic.pageReferences;
        for (var i = 0; i < entries.length; i++) {
            var entry = entries[i];
            var entryText = topic.name; // Use the topic's name as the entry text
            
            // Create an entry object to store entryText and associated page numbers
            var entryObj = {
                text: entryText,
                pageNumbers: {} // Use an object to store page numbers as keys
            };

            // Check if sourceText is available
            if (entry.sourceText) {
                // Retrieve page number from the sourceText
                if (entry.sourceText.parentTextFrames.length > 0) {
                    var pageNumber = entry.sourceText.parentTextFrames[0].parentPage.name;
                    
                    // Add the page number to the entry's pageNumbers object (unique keys)
                    entryObj.pageNumbers[pageNumber] = true;
                }
            }
            
            // Check if entry already exists in the entriesArray
            var entryExists = false;
            for (var j = 0; j < entriesArray.length; j++) {
                if (entriesArray[j].text === entryObj.text) {
                    // If entry already exists, add the page numbers
                    for (var pageNum in entryObj.pageNumbers) {
                        entriesArray[j].pageNumbers[pageNum] = true;
                    }
                    entryExists = true;
                    break;
                }
            }

            if (!entryExists) {
                // If the entry does not exist, add it to the array
                entriesArray.push(entryObj);
            }
        }
        
        // Recursively process subtopics
        var subTopics = topic.topics;
        for (var j = 0; j < subTopics.length; j++) {
            collectEntries(subTopics[j]);
        }
    }

    // Loop through all the topics in the index
    var topics = index.topics;
    for (var i = 0; i < topics.length; i++) {
        var topic = topics[i];
        
        // Check if the topic matches the target topic
        if (topic.name === targetTopic) {
            collectEntries(topic);
        }
    }

    if (entriesArray.length === 0) {
        throw "No entries found for the topic: " + targetTopic;
    }

    // Array to store the formatted entries for output
    var formattedEntries = [];

    // Loop through entriesArray to format the output
    for (var i = 0; i < entriesArray.length; i++) {
        var entryObj = entriesArray[i];

        // Manually extract keys (page numbers) from the pageNumbers object
        var sortedPageNumbers = [];
        for (var pageNum in entryObj.pageNumbers) {
            if (entryObj.pageNumbers.hasOwnProperty(pageNum)) {
                sortedPageNumbers.push(pageNum);
            }
        }

        // Sort the page numbers in ascending order
        sortedPageNumbers.sort(function(a, b) {
            return a - b;
        });
        
        // Join the sorted page numbers as a string with Arabic comma
        var pageNumbersStr = sortedPageNumbers.join("، ");
        
        // Format entry as "Topic: Page Numbers"
        formattedEntries.push(entryObj.text + ": " + pageNumbersStr);
    }

    // Create a new document to display the filtered index
    var newDoc = app.documents.add();
    var textFrame = newDoc.pages[0].textFrames.add();
    textFrame.geometricBounds = [12, 12, 300, 200]; // Adjust as needed

    // Populate the text frame with the filtered index
    textFrame.contents = "Index Entries for " + targetTopic + ":\r" + formattedEntries.join("\r");
    
    alert("Partial index generated for topic: " + targetTopic);

} catch (e) {
    $.writeln("Error: " + e);
}

3 replies

Peter Kahrel
Community Expert
Community Expert
Dec 19, 2024 7:13 AM UTC

Looks pretty good, well done!

Translate
Peter Kahrel
Community Expert
Community Expert
Dec 18, 2024 3:05 PM UTC

You can't generate part of an index. But with your set-up it should be easy to generate the whole index and remove what you don't want.

Translate
ali u
ali uAuthorCorrect answer
Inspiring
Dec 19, 2024 6:47 AM UTC

Thank you for your response. I have a file where the entire index spans about 360 pages across 6 categories. I was hoping there might be a way to generate each category individually to make the process more manageable.
I managed to achive this, kind of, with this crude script but I have a feeling that I shouldn't be doing it this way, what do you think?

// Define the topic you want to filter (e.g., "*People" or "*Books").
var targetTopic = "*People"; // Change to "*Books" or any other topic as needed.

try {
    var doc = app.activeDocument;
    var index = doc.indexes[0];
    
    // Array to store entries with page numbers
    var entriesArray = [];

    // Function to collect all subtopic entries under a topic
    function collectEntries(topic) {
        // Add entries for the current topic
        var entries = topic.pageReferences;
        for (var i = 0; i < entries.length; i++) {
            var entry = entries[i];
            var entryText = topic.name; // Use the topic's name as the entry text
            
            // Create an entry object to store entryText and associated page numbers
            var entryObj = {
                text: entryText,
                pageNumbers: {} // Use an object to store page numbers as keys
            };

            // Check if sourceText is available
            if (entry.sourceText) {
                // Retrieve page number from the sourceText
                if (entry.sourceText.parentTextFrames.length > 0) {
                    var pageNumber = entry.sourceText.parentTextFrames[0].parentPage.name;
                    
                    // Add the page number to the entry's pageNumbers object (unique keys)
                    entryObj.pageNumbers[pageNumber] = true;
                }
            }
            
            // Check if entry already exists in the entriesArray
            var entryExists = false;
            for (var j = 0; j < entriesArray.length; j++) {
                if (entriesArray[j].text === entryObj.text) {
                    // If entry already exists, add the page numbers
                    for (var pageNum in entryObj.pageNumbers) {
                        entriesArray[j].pageNumbers[pageNum] = true;
                    }
                    entryExists = true;
                    break;
                }
            }

            if (!entryExists) {
                // If the entry does not exist, add it to the array
                entriesArray.push(entryObj);
            }
        }
        
        // Recursively process subtopics
        var subTopics = topic.topics;
        for (var j = 0; j < subTopics.length; j++) {
            collectEntries(subTopics[j]);
        }
    }

    // Loop through all the topics in the index
    var topics = index.topics;
    for (var i = 0; i < topics.length; i++) {
        var topic = topics[i];
        
        // Check if the topic matches the target topic
        if (topic.name === targetTopic) {
            collectEntries(topic);
        }
    }

    if (entriesArray.length === 0) {
        throw "No entries found for the topic: " + targetTopic;
    }

    // Array to store the formatted entries for output
    var formattedEntries = [];

    // Loop through entriesArray to format the output
    for (var i = 0; i < entriesArray.length; i++) {
        var entryObj = entriesArray[i];

        // Manually extract keys (page numbers) from the pageNumbers object
        var sortedPageNumbers = [];
        for (var pageNum in entryObj.pageNumbers) {
            if (entryObj.pageNumbers.hasOwnProperty(pageNum)) {
                sortedPageNumbers.push(pageNum);
            }
        }

        // Sort the page numbers in ascending order
        sortedPageNumbers.sort(function(a, b) {
            return a - b;
        });
        
        // Join the sorted page numbers as a string with Arabic comma
        var pageNumbersStr = sortedPageNumbers.join("، ");
        
        // Format entry as "Topic: Page Numbers"
        formattedEntries.push(entryObj.text + ": " + pageNumbersStr);
    }

    // Create a new document to display the filtered index
    var newDoc = app.documents.add();
    var textFrame = newDoc.pages[0].textFrames.add();
    textFrame.geometricBounds = [12, 12, 300, 200]; // Adjust as needed

    // Populate the text frame with the filtered index
    textFrame.contents = "Index Entries for " + targetTopic + ":\r" + formattedEntries.join("\r");
    
    alert("Partial index generated for topic: " + targetTopic);

} catch (e) {
    $.writeln("Error: " + e);
}
Translate