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

Getting undefined error for current document in script

Explorer ,
Jan 27, 2014 Jan 27, 2014

I'm getting a "Error Number: 2" "Error String: myDoc is undefined" in my script.

I tweaked some scripts so they traverse my entire book, and open up every section (document) in my book and run the code for each section.ext

The code runs great and opens and closes sections where it doesn't find text to add links to, but once it opens a document and finds text that it CAN add links to, it throws that error.  Below is my code:

main();

exit();

function main() {

    var myBook = app.activeBook,

            myDocs = myBook.bookContents.everyItem().getElements(),

            myDoc,

            myHyperlinkStyle,

            myCount = 0;

    for (var i=0; i< myDocs.length; i++) {

        myDoc = app.open(File("\\\\computerOnNetwork\\c$\\Folder\\" + myDocs.name));

        myHyperlinkStyle = myDoc.characterStyles.item("linkstyle");

        try {

            var script = app.activeScript;

        } catch(err) {

            var script = File(err.fileName);

        }

        var myScriptFolderPath = script.path;

        var myFindChangeFile = new File(myScriptFolderPath + "/SearchTextAndUrls.txt"); //mac path for users desktop //File.openDialog("Choose the file containing the tab separated list");

        //alert(myFindChangeFile)

        myFindChangeFile = File(myFindChangeFile);

        var myResult = myFindChangeFile.open("r", undefined, undefined);

        if(myResult == true){

            app.findTextPreferences = NothingEnum.nothing;

            app.changeTextPreferences = NothingEnum.nothing;

            //Loop through the find/change operations.

            do {

                //read 1 line into myLine

                myLine = myFindChangeFile.readln();

                myFindChangeArray = myLine.split("\t");

                //The first field in the line is the value to find

                myFindVal = myFindChangeArray[0];

                // second is the url

                myFindUrl = myFindChangeArray[1];

                doSearchAndReplace(myFindVal, myFindUrl, app.activeDocument);

            } while(myFindChangeFile.eof == false);

                myFindChangeFile.close();

                // reset search

                app.findTextPreferences = NothingEnum.nothing;

                app.changeTextPreferences = NothingEnum.nothing;

        }

        alert("Done! " + myCount + " hyperlinks have been added.");

        myDoc.close();

    }

}

function doSearchAndReplace(stringfind, urlstring, searchin) {

    app.findTextPreferences.findWhat = stringfind;

    //Set the find options.

    app.findChangeTextOptions.caseSensitive = false;

    app.findChangeTextOptions.includeFootnotes = false;

    app.findChangeTextOptions.includeHiddenLayers = false;

    app.findChangeTextOptions.includeLockedLayersForFind = false;

    app.findChangeTextOptions.includeLockedStoriesForFind = false;

    app.findChangeTextOptions.includeMasterPages = false;

    app.findChangeTextOptions.wholeWord = false;

    var myFoundItems = searchin.findText();

    for (i = 0; i < myFoundItems.length; i++) {

        var myHyperlinkDestination = myMakeURLHyperlinkDestination(urlstring);

        myMakeHyperlink(myFoundItems, myHyperlinkDestination);

        myFoundItems.applyCharacterStyle(myHyperlinkStyle, false);

        myCount++

    }

}

function myMakeHyperlink(myFoundItem, myHyperlinkDestination){

    try {

        var myHyperlinkTextSource = myDoc.hyperlinkTextSources.add(myFoundItem);

        var myHyperlink = myDoc.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);

        myHyperlink.visible = false;

    }

    catch(myError){

    }

}

function myMakeURLHyperlinkDestination(myURL){

    //If the hyperlink destination already exists, use it;

    //if it doesn't, then create it.

    try{

        var myHyperlinkDestination = myDoc.hyperlinkURLDestinations.item(myURL);

        myHyperlinkDestination.name;

    }

    catch(myError){

        myHyperlinkDestination = myDoc.hyperlinkURLDestinations.add(myURL);

    }

    myHyperlinkDestination.name = myURL;

    //Set other hyperlink properties here, if necessary.

    return myHyperlinkDestination;

}

Any and all help is greatly appreciated!

TOPICS
Scripting
2.3K
Translate
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

Explorer , Jan 27, 2014 Jan 27, 2014

This ended up being my fixed/final code:

main();

exit();

function main() {

    var myBook = app.activeBook,

            myDocs = myBook.bookContents.everyItem().getElements(),

            myDoc,

            myHyperlinkStyle;

    for (var i=0; i< myDocs.length; i++) {

        myDoc = app.open(File("\\\\computerOnNetwork\\c$\\Folder\\" + myDocs.name));

        myHyperlinkStyle = myDoc.characterStyles.item("linkstyle");

        try {

            var script = app.activeScript;

        } catch(err) {

            v

...
Translate
Community Expert ,
Jan 27, 2014 Jan 27, 2014

Hello Soteriologist,

try this:

for (var i=0; i<myDocs.length-1; i++) {…

Translate
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 ,
Jan 27, 2014 Jan 27, 2014

Thank you for the quick reply.

Sadly, it's not that I'm extending past my index/count of items.  I tried your change anyways, but it didn't fix the problem.  This is actually happening right in the middle of my book (like on my 8th document of a total of 26 sections).

Translate
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 ,
Jan 27, 2014 Jan 27, 2014

Oh! And I'm sorry the source line that is causing the error is...

Line: 94

Source: myHyperlinkDestination = myDoc.hyperlinkURLDestinations.add(myURL);

In the catch, to my try:
    catch(myError){
        myHyperlinkDestination = myDoc.hyperlinkURLDestinations.add(myURL);
    }

Translate
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
Community Expert ,
Jan 27, 2014 Jan 27, 2014

I think this is the same:

for (i = 0; i < myFoundItems.length-1; i++) {…

Please alert or $.writeln your myFoundItems.length

Translate
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 ,
Jan 27, 2014 Jan 27, 2014

I'm an idiot, and I figured out what it was.  I wasn't passing my variables to the functions that needed, thinking they were accessible when they were not. >_<

Translate
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 ,
Jan 27, 2014 Jan 27, 2014
LATEST

This ended up being my fixed/final code:

main();

exit();

function main() {

    var myBook = app.activeBook,

            myDocs = myBook.bookContents.everyItem().getElements(),

            myDoc,

            myHyperlinkStyle;

    for (var i=0; i< myDocs.length; i++) {

        myDoc = app.open(File("\\\\computerOnNetwork\\c$\\Folder\\" + myDocs.name));

        myHyperlinkStyle = myDoc.characterStyles.item("linkstyle");

        try {

            var script = app.activeScript;

        } catch(err) {

            var script = File(err.fileName);

        }

        var myScriptFolderPath = script.path;

        var myFindChangeFile = new File(myScriptFolderPath + "/SearchTextAndUrls.txt"); //mac path for users desktop //File.openDialog("Choose the file containing the tab separated list");

        //alert(myFindChangeFile)

        myFindChangeFile = File(myFindChangeFile);

        var myResult = myFindChangeFile.open("r", undefined, undefined);

        if(myResult == true){

            app.findTextPreferences = NothingEnum.nothing;

            app.changeTextPreferences = NothingEnum.nothing;

            //Loop through the find/change operations.

            do {

                //read 1 line into myLine

                myLine = myFindChangeFile.readln();

                myFindChangeArray = myLine.split("\t");

               

                //The first field in the line is the value to find

                myFindVal = myFindChangeArray[0];

               

                // second is the url

                myFindUrl = myFindChangeArray[1];

               

                doSearchAndReplace(myFindVal, myFindUrl, app.activeDocument, myDoc, myHyperlinkStyle);

                   

            } while(myFindChangeFile.eof == false);

                myFindChangeFile.close();

                // reset search

                app.findTextPreferences = NothingEnum.nothing;

                app.changeTextPreferences = NothingEnum.nothing;

        }

        alert("Done! Hyperlinks have been added.");

        myDoc.close();

    }

}

function doSearchAndReplace(stringfind, urlstring, searchin, myDoc, myHyperlinkStyle) {

    app.findTextPreferences.findWhat = stringfind;

   

    //Set the find options.

    app.findChangeTextOptions.caseSensitive = false;

    app.findChangeTextOptions.includeFootnotes = false;

    app.findChangeTextOptions.includeHiddenLayers = false;

    app.findChangeTextOptions.includeLockedLayersForFind = false;

    app.findChangeTextOptions.includeLockedStoriesForFind = false;

    app.findChangeTextOptions.includeMasterPages = false;

    app.findChangeTextOptions.wholeWord = false;

   

    var myFoundItems = searchin.findText();

    for (i = 0; i < myFoundItems.length; i++) {

        var myHyperlinkDestination = myMakeURLHyperlinkDestination(urlstring, myDoc);

        myMakeHyperlink(myFoundItems, myHyperlinkDestination, myDoc);

        myFoundItems.applyCharacterStyle(myHyperlinkStyle, false);

    }

}

function myMakeHyperlink(myFoundItem, myHyperlinkDestination, myDoc){

    try {

        var myHyperlinkTextSource = myDoc.hyperlinkTextSources.add(myFoundItem);

        var myHyperlink = myDoc.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);

        myHyperlink.visible = false;

    }

    catch(myError){

    }

}

function myMakeURLHyperlinkDestination(myURL, myDoc){

    //If the hyperlink destination already exists, use it;

    //if it doesn't, then create it.

    try{

        var myHyperlinkDestination = myDoc.hyperlinkURLDestinations.item(myURL);

        myHyperlinkDestination.name;

    }

    catch(myError){

        myHyperlinkDestination = myDoc.hyperlinkURLDestinations.add(myURL);

    }

    myHyperlinkDestination.name = myURL;

    //Set other hyperlink properties here, if necessary.

    return myHyperlinkDestination;

}

Translate
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