6 Replies Latest reply on Jan 27, 2014 4:12 PM by Soteriologist

    Getting undefined error for current document in script

    Soteriologist

      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!

        • 1. Re: Getting undefined error for current document in script
          pixxxel schubser Level 5

          Hello Soteriologist,

           

          try this:

           

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

          • 2. Re: Getting undefined error for current document in script
            Soteriologist Level 1

            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).

            • 3. Re: Getting undefined error for current document in script
              Soteriologist Level 1

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

              • 4. Re: Getting undefined error for current document in script
                pixxxel schubser Level 5

                I think this is the same:

                 

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

                 

                Please alert or $.writeln your myFoundItems.length

                • 5. Re: Getting undefined error for current document in script
                  Soteriologist Level 1

                  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. >_<

                  1 person found this helpful
                  • 6. Re: Getting undefined error for current document in script
                    Soteriologist Level 1

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