• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
7

Adding Cross reference for foundText

Explorer ,
Oct 05, 2023 Oct 05, 2023

Copy link to clipboard

Copied

Hi Everyone,

 

I need a help with this. I am trying to add crossReference to the text selection on the page, script will find the selected entries inside the text and will add PageNumber CrossReference. But it doesn't iterates the last entry.

 

Throws 'Object is invalid.' error. Can't find what is wrong with this. Please check and help me find what's wrong with this. Please refer the screenshots attached.

 

var myDoc = app.activeDocument;
var xRefForm = myDoc.crossReferenceFormats.item ('Page Number');
var tocEntriesPstyles = []
var CrossSource;
var CrossRefDest;

var tocEntries = app.selection[0].paragraphs;

for (var l = 0; l < tocEntries.length; l++) {
var eachentry = String(tocEntries[l].contents)
eachentry = eachentry.replace(/\t000\r/g, "")

CrossSource = myDoc.crossReferenceSources.add (tocEntries[l].paragraphs[0].insertionPoints[-2], xRefForm);

app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item("ah")
app.findTextPreferences.findWhat = eachentry;
var myFoundItems = myDoc.findText();

if (myFoundItems.length > 0) {
CrossRefDest = myDoc.hyperlinkTextDestinations.add (myFoundItems[0], {name: myFoundItems[0].contents});
myDoc.hyperlinks.add (CrossSource, CrossRefDest);
}
}

 

Thanks,

Sathish

TOPICS
Scripting

Views

320

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

Community Expert , Oct 05, 2023 Oct 05, 2023

Hi @SathishSekar_, I haven't tested your script, but somtimes it helps to reverse the direction of your loop, starting at the last and going backwards to the first. This will solve problems where a DOM reference becomes invalid by the actions inside the loop.

 

Please try replacing this line:

for (var l = 0; l < tocEntries.length; l++) {

with:

for (var l = tocEntries.length -1; l >= 0 ; l--) {

 

Let us know if it helps. If not, is it possible to share a small demo .indd file?

- Mark

Votes

Translate

Translate
Community Expert ,
Oct 05, 2023 Oct 05, 2023

Copy link to clipboard

Copied

Hi @SathishSekar_, I haven't tested your script, but somtimes it helps to reverse the direction of your loop, starting at the last and going backwards to the first. This will solve problems where a DOM reference becomes invalid by the actions inside the loop.

 

Please try replacing this line:

for (var l = 0; l < tocEntries.length; l++) {

with:

for (var l = tocEntries.length -1; l >= 0 ; l--) {

 

Let us know if it helps. If not, is it possible to share a small demo .indd file?

- Mark

Votes

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
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

Hi @m1b,

 

Thanks for your valuable response. 

Yes after reversing the loop it works. However, it leaves atleast one entry behind without adding cross-reference(throws error so it has to skip that entry).

 

Thanks,

Sathish

Votes

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
Guide ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

\t000$\r?

 

(^/)  The Jedi

Votes

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
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

Hi @FRIdNGE & @m1b ,

 

It didn't help.

Sample file attached.

 

This is the full script i am working on...

 

var myDoc = app.activeDocument;
var xRefForm = myDoc.crossReferenceFormats.item ('Page Number');
var tocEntriesPstyles = []
var CrossSource;
var CrossRefDest;


try {
    var tocEntries = app.selection[0].paragraphs;
} catch (error) {
    alert("please select the toc entries and try again.");
    exit(0)
}

if(tocEntries.length == 1){
    alert("Selection wasn't done. Tool exit!!!");
    exit(0)
}

var myCstyles = myDoc.characterStyles.everyItem().name;
var myPstyles = myDoc.paragraphStyles.everyItem().name;

myPstyles = myPstyles.sort()
myCstyles = myCstyles.sort()

// for (var i = tocEntries.length-1; i >= 0 ; i--){
for (var i = 0; i < tocEntries.length; i++) {
    if(tocEntries[i].paragraphs[0].words[-1].contents === '000'){
        tocEntriesPstyles.push(tocEntries[i].paragraphs[0].appliedParagraphStyle.name)
    }
}

tocEntriesPstyles = unique(tocEntriesPstyles);

for (var j = tocEntriesPstyles.length-1; j >= 0 ; j--) {
// for(var j=0; j < tocEntriesPstyles.length; j++){
    
    var eachentryPstyle = tocEntriesPstyles[j]

    //selecting corresponding PStyle for each style(User input)
    var w = new Window('dialog{text:" Select Style for: '+eachentryPstyle+' (multi select)", alignChildren: "fill"}')
    var picked = type_ahead1(myPstyles);

    // for (var l = 0; l < tocEntries.length; l++) {
    for (var l = tocEntries.length-1; l >= 0 ; l--) {
            try {
                $.writeln(tocEntries[l].contents+" -- "+l)
            } catch (error) {
                continue;
            }
        if (tocEntries[l].paragraphs[0].words[-1].contents === '000') {
            if (tocEntries[l].paragraphs[0].appliedParagraphStyle.name == eachentryPstyle) {
                var eachentry = String(tocEntries[l].contents)
                eachentry = eachentry.replace(/^[\t~m~>\s]*(\d+)*\.*[\t~m~>\s]*/g, "")
                eachentry = eachentry.replace(/\t000$\r/g, "")
                eachentry = eachentry.replace(/\n|\s/g, " ")
                eachentry = eachentry.replace(/  /g, " ")
                CrossSource = myDoc.crossReferenceSources.add (tocEntries[l].paragraphs[0].insertionPoints[-2], xRefForm);
                var foundDests = []

                app.findTextPreferences = NothingEnum.nothing;
                app.changeTextPreferences = NothingEnum.nothing;
                app.findChangeTextOptions.caseSensitive = false;
                app.findChangeTextOptions.includeFootnotes = true;
                app.findChangeTextOptions.includeHiddenLayers = false;
                app.findChangeTextOptions.includeLockedLayersForFind = false;
                app.findChangeTextOptions.includeLockedStoriesForFind = false;
                app.findChangeTextOptions.includeMasterPages = false;
                app.findChangeTextOptions.wholeWord = false;
                app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item(String(picked[0]))
                app.findTextPreferences.findWhat = eachentry;
                var myFoundItems = myDoc.findText();

                if (myFoundItems.length > 0) {
                    var myPgnum = myFoundItems[0].parentTextFrames[0].parentPage.name;
                    // tocEntries[l].paragraphs[0].words[-1].contents = myPgnum
                    CrossRefDest = myDoc.hyperlinkTextDestinations.add (myFoundItems[0], {name: 'dest_'+l});
                    myDoc.hyperlinks.add (CrossSource, CrossRefDest);
                }
                // continue;
                //if not found by above style
                if (myFoundItems.length == 0 && picked.length > 1) {
                    var foundDests1 = ["[NOT IN THIS LIST]"]
                    app.findTextPreferences = NothingEnum.nothing;
                    app.changeTextPreferences = NothingEnum.nothing;
                    app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item(String(picked[1]))
                    app.findTextPreferences.findWhat = eachentry;
                    var myFoundItems = myDoc.findText();

                    if (myFoundItems.length > 0) {
                        var myPgnum = myFoundItems[0].parentTextFrames[0].parentPage.name
                        // tocEntries[l].paragraphs[0].words[-1].contents = myPgnum
                        CrossRefDest = myDoc.hyperlinkTextDestinations.add (myFoundItems[0], {name: 'dest_'+l});
                        myDoc.hyperlinks.add (CrossSource, CrossRefDest);
                    }
                    
                    //if not found by any styles, get selected by the user,
                    if (myFoundItems.length == 0) {
                        alert("Not Found directly!\rSelect the style to find all occurences for '"+eachentry+"'.")

                        var w = new Window('dialog{text:"Select style from below", alignChildren: "fill"}')
                        var picked1 = type_ahead1(picked)

                        app.findTextPreferences = NothingEnum.nothing;
                        app.changeTextPreferences = NothingEnum.nothing;
                        app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item(String(picked1))
                        var myFoundItems = myDoc.findText();

                        for (var m = 0; m < myFoundItems.length; m++) {
                            foundDests1.push(myFoundItems[m].contents)
                        }
                        var w = new Window('dialog{text:" Select the interior text!", alignChildren: "fill"}')
                        var myDest = type_ahead1(foundDests1);
                        if(String(myDest) == "[NOT IN THIS LIST]"){
                            continue
                        }else{
                            app.findTextPreferences = NothingEnum.nothing;
                            app.changeTextPreferences = NothingEnum.nothing;
                            app.findTextPreferences.appliedParagraphStyle = myDoc.paragraphStyles.item(String(picked1))
                            app.findTextPreferences.findWhat = String(myDest);
                            var myFoundItems = myDoc.findText();
                            var myPgnum = myFoundItems[0].parentTextFrames[0].parentPage.name
                            // tocEntries[l].paragraphs[0].words[-1].contents = myPgnum
                            CrossRefDest = myDoc.hyperlinkTextDestinations.add (myFoundItems[0], {name: 'dest_'+l});
                            myDoc.hyperlinks.add (CrossSource, CrossRefDest);
                            }
                    }
                }

            }
        }
    }
}

alert("Process Completed!!!")

function unique(reportPages){
    newArray=new Array();
    label:for(var i=0;i<reportPages.length;i++){
        for(var x=0;x<newArray.length;x++){
            if(newArray[x]==reportPages[i])continue label;
        }
    newArray[newArray.length]=reportPages[i];
    }
    return newArray;
}

function type_ahead1(styles){
    // var w = new Window('dialog{text:"EndnoteStyle", alignChildren: "fill"}')
    var entry = w.add('edittext{active:true}')
    var list = w.add('listbox',[0,0,250,350], styles, {multiselect: true});
    list.selection = 0;
    entry.onChanging = function(){
        var temp = this.text;
        list.removeAll();
        for(var k =0; k<styles.length; k++){
            if(styles[k].toLowerCase().indexOf(temp)==0){
                list.add('item', styles[k])
            }
        }
        if(list.items.length > 0){
            list.selection = 0;
        }
    }
    w.add('button',undefined,'Ok',{name:'Ok'})
    if(w.show()!=2){
        selected = [];
        for (var i = 0; i < list.selection.length; i++) {
            selected.push (list.selection[i].text);
        }
        return selected;
        // $.writeln(list.selection.text)
        // return list.selection.text;
    }else{alert("Process Cancelled.");exit(0)}
    w.close();
}

 

 

 

Thanks,

Sathish

Votes

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
Guide ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

I wrote:

 

\t000$\r?

 

with a "?"

 

(^/)

Votes

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
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

Okay.

It's no..!

 

Sathish

Votes

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
Guide ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

Take a look at this line:

 

CrossSource = myDoc.crossReferenceSources.add (tocEntries[l].paragraphs[0].insertionPoints[-2], xRefForm);
 
To fix your problem, add a carriage-return at the end of the TOC story.
 
(^/)

Votes

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
Explorer ,
Oct 06, 2023 Oct 06, 2023

Copy link to clipboard

Copied

LATEST

Hi @FRIdNGE,

Chaper 6 and last entry was left behind.

 

Screenshot 2023-10-06 at 4.27.54 pm.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Votes

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