1 Reply Latest reply on Oct 5, 2015 5:59 PM by BSKTCreation

    InDesign scripting error “ReferenceError: Object is invalid”

    igiveuprandom

      I'm trying to bring back to life and older script I had used that worked in the past. The script would download comics (that we have the rights to) using autohotkey and curl... Then in InDesign we would run the following from the javascript Scripts panel:

       

      #targetengine "session"
      var date, month, year, myDocument;
      var curDate = new Date();
      var myTemplatePath = "/c/Comic/ComicImport.indd";
      var myComicsPath = "/c/Comic/Comics/";
      var myTemplate = new File(myTemplatePath);
      
      
      
      
      
      if (myTemplate.exists) {
          try {
              myDocument = app.open(myTemplate);
          } catch (e) {
              alert("Could not open template, exiting\n" + e);
              exit();
          }
          var win = showDialog();
      } else {
          alert("Could not locate template at:\n" + myTemplatePath + "\nexiting");
      }
      
      
      
      
      
      function showDialog() {
          var win = new Window('palette');
          with(win){
              win.Pnl = add('panel', undefined, 'Date / Month / Year');
              win.Pnl.orientation = 'row';
              with(win.Pnl) {
                  win.Pnl.day = add('edittext');
                  win.Pnl.day.text = curDate.getDate();
                  win.Pnl.day.preferredSize = [30,20];
                 
                  win.Pnl.month = add('edittext');
                  win.Pnl.month.text = curDate.getMonth() + 1;
                  win.Pnl.month.preferredSize = [30,20];
                 
                  win.Pnl.year = add('edittext');
                  win.Pnl.year.text = curDate.getFullYear();
                  win.Pnl.year.preferredSize = [50,20];
              }
              win.btnOk = add('button', undefined, 'Import Comic');
              win.btnOk.onClick = setDate;
          };
          win.center();
          win.show();
          return win;
      }
      function setDate() {
          date = win.Pnl.day.text;
          month = win.Pnl.month.text;
          year = win.Pnl.year.text;
          // OK we close the window and do the import
          //win.close();
          importComics();
      }
      
      
      
      
      
      function importComics() {
          try {
              //set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
              var comics = new Array();
              // REPLACE with own filepaths, could be
              //comics.push(new File("/c/comics/" + year + month + date + "pzjud-a.tif"));
              comics.push(new File(myComicsPath + "comic1-" + year + "-" + month + "-" + date + ".tif"));
              comics.push(new File(myComicsPath + "comic2-" + year + "-" + month + "-" + date + ".tif"));
              comics.push(new File(myComicsPath + "comic3-" + year + "-" + month + "-" + date + ".tif"));
              comics.push(new File(myComicsPath + "comic4-" + year + "-" + month + "-" + date + ".tif"));
              comics.push(new File(myComicsPath + "comic5-" + year + "-" + month + "-" + date + ".tif"));
          } catch (e) {
              alert("Error assigning images for import, stopping script\n" + e);
              exit();
          }
         
          for (i = 1; i <= comics.length; i++) {
              // Script label of the rectangles/pageitems to place the graphics into
              var myRect  = myDocument.pageItems.item("comic" + i);
              try {
              myRect.place(comics[i-1]);
              } catch (e) {
                  alert(e);
              }
              myRect.fit(FitOptions.CONTENT_TO_FRAME);
          }
      }
      

       

      However as soon as I hit the Import Comic button, I get the "ReferenceError: Object is invalid" error. My directory structures look ok to me. Any ideas?

      thanks!

        • 1. Re: InDesign scripting error “ReferenceError: Object is invalid”
          BSKTCreation Level 2

          Hi there,

           

          Here is my solution to the issue (replace lines 82 to 91 in your code with this).

           

              for (i = 1; i <= comics.length; i++) { 
                  // Script label of the rectangles/pageitems to place the graphics into
                  var curVer = app.scriptPreferences.version;
                  app.scriptPreferences.version = "6.0";
                  try {
                      myDocument.pageItems.item("comic" + i).place(comics[i-1]);
                      }
                  catch (e) { 
                      alert(e);
                      } 
                  myDocument.pageItems.item("comic" + i).fit(FitOptions.CONTENT_TO_FRAME);
                  app.scriptPreferences.version = curVer;
                  } 
          
          

           

          What I've done is version wrap the item script label into the CS4 DOM (6.0) where script labels are usable. Then after it is done I've reverted the DOM to the current version.

           

          It ain't pretty but I think it'll get you going again.

           

          Brett