Skip to main content
Inspiring
May 14, 2022
Answered

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

  • May 14, 2022
  • 1 reply
  • 1053 views

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.

 

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.

This topic has been closed for replies.
Correct answer rob day

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){}
}

 


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);
}

 

 

 

1 reply

rob day
Community Expert
May 14, 2022

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
}


 

 

 

 

 

JustyRAuthor
Inspiring
May 15, 2022

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
}

 

rob day
rob dayCorrect answer
Community Expert
May 15, 2022

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){}
}

 


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);
}