Quantcast
Channel: Adobe Community : Popular Discussions - InDesign Scripting
Viewing all articles
Browse latest Browse all 15932

Getting undefined error for current document in script

$
0
0

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[i].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[i], myHyperlinkDestination);        myFoundItems[i].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!


Viewing all articles
Browse latest Browse all 15932

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>