jsx script to take selected text and add a new page reference using drop-down list for topic

Participant ,
May 14, 2022 May 14, 2022

Copy link to clipboard

Copied

Hi all,

I'm writing yet another script (I keep thinking up new ways to avoid work)...to speed up the process of creating new page references for an index using an existing list of Topics (a list of Symbols(?)).

Usually, I'd select the text, e.g. Fred's FunShack, go to Index > New Page Reference... click the down arrow to move Fred's FunShack onto topic level 2, click the > arrow next to Symbols and double-click the Topic. Then click OK.

Screenshot 2022-05-14 at 19.57.55.png

 

I've made a drop-down list (based on stuff gleaned from the web) that uses the existing Topics (again, 'Symbols'),  but as always I'm struggling and going around in circles (slowly learning though). I can't seem to pass the drop-down list selection into the next bit of the script to fill out topic line 1.

Here's what I've managed to cobble together:

 

#target indesign
var myDoc = app.activeDocument;
var myIndex = myDoc.indexes[0];
var myTopics = myIndex.topics.everyItem().name;
var currSelection = app.selection[0];

var myDialog = app.dialogs.add({name:"Choose Category", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ('listbox', undefined, myTopics, {multiselect: false});
            var Topics = dropdowns.add({stringList:myTopics, selectedIndex:0});
            }

if(myDialog.show() == true){
  var myString;
  alert (myTopics [Topics.selectedIndex])
  myDialog.destroy();
  }
  

var currSelection = app.selection[0];
var myTopic = myTopics.selectedIndex;
var currTopic = currSelection.contents;
myTopic.pageReferences.add(currSelection,PageReferenceType.TO_END_OF_STORY);

 

Can anyone help? I've checked the reference guide https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Topics.html#d1e124842__d1e125032 but it doesn't really make much sense to me because examples in layman's terms aren't given.

TOPICS
Scripting , Type

Views

130

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , May 15, 2022 May 15, 2022
In your first post you were trying to add a page reference to the dialog’s selected topic, but it looks like you are trying to create new topics and add a page reference to the newly created topic. This does that for me: function main(){ //the selected text var currSelection = app.selection[0]; //add a new topic to the chosen dropdown topic (myTopics) var newTopic = myTopics.topics.add(currSelection.contents); //set the page reference for the new topic newTopic.pageRefe...

Likes

Translate

Translate
Adobe Community Professional ,
May 14, 2022 May 14, 2022

Copy link to clipboard

Copied

Hi @JustyR , Does this work for you?

 

 

 


if (app.activeDocument.indexes.length > 0) {
	makeDialog();
} else {
    alert("No Indexes")
}


var myTopics;
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Index", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Index Topics:"});
                }
                with(dialogColumns.add()){
                    //Collection
                    myTopics = dropdowns.add({stringList:getCollection(app.activeDocument.indexes[0].topics), selectedIndex:0, minWidth:80});
                } 
        }
        if(theDialog.show() == true){
            
            myTopics = app.activeDocument.indexes[0].topics[myTopics.selectedIndex];
            main();
            theDialog.destroy();
        }
    }
}

function main(){
    alert("\rChosen Topic: " + myTopics.name);
    return 
}


/**
* Returns a list of item names for the dropdown from the provided collection.
* @ return array of names 
* 
*/
function getCollection(o){
    return o.everyItem().name
}


 

 

 

 

Screen Shot 22.png

 

Screen Shot 23.png

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
community guidelines
Participant ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Hi, Rob, thanks for helping out. So that gets me out of the mud I was in with an error on line 26. Just got to work on making it generate the page reference. Thanks again.

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
community guidelines
Participant ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Somehow I managed to get it to enter the chosen topic into the top level (see the two extra lines in the main function. I can't work out how to take the currently selected text (Fred's FunShack) and add it to the second level.

 

Any ideas?

 

if (app.activeDocument.indexes.length > 0) {
	makeDialog();
} else {
    alert("No Indexes")
}


var myTopics;
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Index", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Index Topics:"});
                }
                with(dialogColumns.add()){
                    //Collection
                    myTopics = dropdowns.add({stringList:getCollection(app.activeDocument.indexes[0].topics), selectedIndex:0, minWidth:80});
                } 
        }
        if(theDialog.show() == true){
            
            myTopics = app.activeDocument.indexes[0].topics[myTopics.selectedIndex];
            main();
            theDialog.destroy();
        }
    }
}

function main(){

//adds menu selection to top level of index
	var currSelection = app.selection[0];
	myTopics.pageReferences.add(currSelection,PageReferenceType.TO_END_OF_STORY);

//need more code to add text selection to second level of index

    alert("\rChosen Topic: " + myTopics.name);
    return 
}


/**
* Returns a list of item names for the dropdown from the provided collection.
* @ return array of names 
* 
*/
function getCollection(o){
    return o.everyItem().name
}

 

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
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

I’ve never scripted indexing, but my dialog code is not picking up 2nd level topics. The index object also has the allTopics property, which is an array (not a collection) of all the levels. The array needs a different function to get the complete topics dropdown string list. See if this works:

 

 

 

if (app.activeDocument.indexes.length > 0) {
	makeDialog();
} else {
    alert("No Indexes")
}


var myTopics;
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Index", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Index Topics:"});
                }
                with(dialogColumns.add()){
                    //Array
                    myTopics = dropdowns.add({stringList:getArrayNames(app.activeDocument.indexes[0].allTopics), selectedIndex:0, minWidth:80});
                } 
        }
        if(theDialog.show() == true){
            
            myTopics = app.activeDocument.indexes[0].allTopics[myTopics.selectedIndex];
            main();
            theDialog.destroy();
        }
    }
}

function main(){
    alert("\rChosen Topic: " + myTopics.name);
    return 
}


/**
* Returns a list of topic names from the provided array of topics. 
* Note does not work with a collection allTopics is an array, while topics is a collection
* @ param the property array 
* @ return array of names 
*/
function getArrayNames(arr){
    var a = new Array;
    for(var i = 0; i < arr.length; i++){
        a.push(arr[i].name);
    }
    return a
}

 

 

 

 

Screen Shot 24.pngScreen Shot 25.png

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
community guidelines
Participant ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Unfortunately, that didn't do the trick. 

This script below goes four levels deep and I'm wondering if your knowledge might help decipher how to combine it into mine. It's actually the script that spurred me on to try and make my own with the drop-down list.

#target indesign
var debug=false;
try{
main();
}catch(e){log(e)};
function main()
{
var myDocument = app.activeDocument;
if(!myDocument.indexes.length) myDocument.indexes.add();
var myIndex = myDocument.indexes[0];
var myTopics = myIndex.topics;
var currSelection = app.selection[0];
if (!currSelection.hasOwnProperty("baseline"))
{
	alert("Please make text selection"); return;
}

var myTopic = myTopics.itemByName("Steamers");
if (!myTopic.isValid)
{
	myTopic=myTopics.add("Steamers"); //LEVEL 1
}

///////This section puts the text and current selection into the correct page reference order
var currTopic = myTopic.topics.add(currSelection.contents); //LEVEL 2
scheduleTopic = currTopic.topics.add("Schedules"); //LEVEL 3
yearTopic = scheduleTopic.topics.add("1950"); //LEVEL 4
yearTopic.pageReferences.add(currSelection,PageReferenceType.TO_END_OF_STORY);

}
function log(obj)
{
if (debug)
try{ $.writeln("------");
$.writeln(obj + " - "+obj.toSource());
$.writeln("------");
}catch(e){}
}

 

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
community guidelines
Adobe Community Professional ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

In your first post you were trying to add a page reference to the dialog’s selected topic, but it looks like you are trying to create new topics and add a page reference to the newly created topic. This does that for me:

 

function main(){
    //the selected text
    var currSelection = app.selection[0];
    //add a new topic to the chosen dropdown topic (myTopics)
    var newTopic = myTopics.topics.add(currSelection.contents);
    //set the page reference for the new topic
    newTopic.pageReferences.add(currSelection, PageReferenceType.TO_END_OF_STORY);
}

 

 

Screen Shot 29.png

 

Screen Shot 27.png

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
community guidelines
Participant ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

Ah, almost. 

The list of Topics are already set, so there's no need to create more. It's a case of taking some selected text, and pairing it with the Topic to create a new page reference.

 

Screenshot 2022-05-15 at 16.18.58.png

image.png

 

  

 

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
community guidelines
Participant ,
May 15, 2022 May 15, 2022

Copy link to clipboard

Copied

LATEST

Rob, IGNORE MY LAST POST!!! I was confusing myself looking at the list of Topics once the page reference had been made. I assumed your additional bit of script was creating a new Topic as well as the page reference.

Apologies, and a big thank you for all your efforts in getting this working for me. I shall invest in Peter Kahrel's book and see if I can start to learn all this properly.

 

The final code is...

 

if (app.activeDocument.indexes.length > 0) {
	makeDialog();
} else {
    alert("No Indexes")
}


var myTopics;
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Index", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Index Topics:"});
                }
                with(dialogColumns.add()){
                    //Array
                    myTopics = dropdowns.add({stringList:getArrayNames(app.activeDocument.indexes[0].allTopics), selectedIndex:0, minWidth:80});
                } 
        }
        if(theDialog.show() == true){
            
            myTopics = app.activeDocument.indexes[0].allTopics[myTopics.selectedIndex];
            main();
            theDialog.destroy();
        }
    }
}

function main(){
    //the selected text
    var currSelection = app.selection[0];
    //add a new topic to the chosen dropdown topic (myTopics)
    var newTopic = myTopics.topics.add(currSelection.contents);
    //set the page reference for the new topic
    newTopic.pageReferences.add(currSelection, PageReferenceType.TO_END_OF_STORY);

    alert("\rChosen Topic: " + myTopics.name);
    return 
}


/**
* Returns a list of topic names from the provided array of topics. 
* Note does not work with a collection allTopics is an array, while topics is a collection
* @ param the property array 
* @ return array of names 
*/
function getArrayNames(arr){
    var a = new Array;
    for(var i = 0; i < arr.length; i++){
        a.push(arr[i].name);
    }
    return a
}

 

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
community guidelines